Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9aa70d9

Browse files
committed
SF bug [#455775] float parsing discrepancy.
PyTokenizer_Get: error if exponent contains no digits (3e, 2.0e+, ...).
1 parent de1d495 commit 9aa70d9

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

Lib/test/test_compile.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,18 @@ def comp_args(a=2, (b, c)=(3, 4)):
5050
raise TestFailed, "non-default args after default"
5151
except SyntaxError:
5252
pass
53+
54+
if verbose:
55+
print "testing bad float literals"
56+
57+
def expect_error(s):
58+
try:
59+
eval(s)
60+
raise TestFailed("%r accepted" % s)
61+
except SyntaxError:
62+
pass
63+
64+
expect_error("2e")
65+
expect_error("2.0e+")
66+
expect_error("1e-")
67+
expect_error("3-4e/21")

Parser/tokenizer.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -756,9 +756,7 @@ PyTokenizer_Get(register struct tok_state *tok, char **p_start,
756756
if (c == 'l' || c == 'L')
757757
c = tok_nextc(tok);
758758
else {
759-
/* Accept floating point numbers.
760-
XXX This accepts incomplete things like
761-
XXX 12e or 1e+; worry run-time */
759+
/* Accept floating point numbers. */
762760
if (c == '.') {
763761
fraction:
764762
/* Fraction */
@@ -771,9 +769,14 @@ PyTokenizer_Get(register struct tok_state *tok, char **p_start,
771769
c = tok_nextc(tok);
772770
if (c == '+' || c == '-')
773771
c = tok_nextc(tok);
774-
while (isdigit(c)) {
775-
c = tok_nextc(tok);
772+
if (!isdigit(c)) {
773+
tok->done = E_TOKEN;
774+
tok_backup(tok, c);
775+
return ERRORTOKEN;
776776
}
777+
do {
778+
c = tok_nextc(tok);
779+
} while (isdigit(c));
777780
}
778781
#ifndef WITHOUT_COMPLEX
779782
if (c == 'j' || c == 'J')

0 commit comments

Comments
 (0)