summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_grammar.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2018-07-09 12:09:35 (GMT)
committerGitHub <noreply@github.com>2018-07-09 12:09:35 (GMT)
commitcf7303ed2aa19fb48687d7140dbc86fc23c9fca4 (patch)
tree201461fe60186fd1ceca58573eacf76fd4e8aa16 /Lib/test/test_grammar.py
parent2a9b8babf0d09946ebebfdb2931cc0d3db5a1d3d (diff)
downloadcpython-cf7303ed2aa19fb48687d7140dbc86fc23c9fca4.zip
cpython-cf7303ed2aa19fb48687d7140dbc86fc23c9fca4.tar.gz
cpython-cf7303ed2aa19fb48687d7140dbc86fc23c9fca4.tar.bz2
bpo-33305: Improve SyntaxError for invalid numerical literals. (GH-6517)
Diffstat (limited to 'Lib/test/test_grammar.py')
-rw-r--r--Lib/test/test_grammar.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py
index ee41362..78918ae 100644
--- a/Lib/test/test_grammar.py
+++ b/Lib/test/test_grammar.py
@@ -100,6 +100,8 @@ INVALID_UNDERSCORE_LITERALS = [
class TokenTests(unittest.TestCase):
+ check_syntax_error = check_syntax_error
+
def test_backslash(self):
# Backslash means line continuation:
x = 1 \
@@ -184,6 +186,28 @@ class TokenTests(unittest.TestCase):
# Sanity check: no literal begins with an underscore
self.assertRaises(NameError, eval, "_0")
+ def test_bad_numerical_literals(self):
+ check = self.check_syntax_error
+ check("0b12", "invalid digit '2' in binary literal")
+ check("0b1_2", "invalid digit '2' in binary literal")
+ check("0b2", "invalid digit '2' in binary literal")
+ check("0b1_", "invalid binary literal")
+ check("0b", "invalid binary literal")
+ check("0o18", "invalid digit '8' in octal literal")
+ check("0o1_8", "invalid digit '8' in octal literal")
+ check("0o8", "invalid digit '8' in octal literal")
+ check("0o1_", "invalid octal literal")
+ check("0o", "invalid octal literal")
+ check("0x1_", "invalid hexadecimal literal")
+ check("0x", "invalid hexadecimal literal")
+ check("1_", "invalid decimal literal")
+ check("012",
+ "leading zeros in decimal integer literals are not permitted; "
+ "use an 0o prefix for octal integers")
+ check("1.2_", "invalid decimal literal")
+ check("1e2_", "invalid decimal literal")
+ check("1e+", "invalid decimal literal")
+
def test_string_literals(self):
x = ''; y = ""; self.assertTrue(len(x) == 0 and x == y)
x = '\''; y = "'"; self.assertTrue(len(x) == 1 and x == y and ord(x) == 39)