Skip to content

Commit aa35271

Browse files
committed
extract UNDERSCORE_LITERALS from test_grammar
1 parent 6e83d44 commit aa35271

File tree

6 files changed

+102
-90
lines changed

6 files changed

+102
-90
lines changed

Lib/test/support/numbers.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# These are shared with test_tokenize and other test modules.
2+
#
3+
# Note: since several test cases filter out floats by looking for "e" and ".",
4+
# don't add hexadecimal literals that contain "e" or "E".
5+
VALID_UNDERSCORE_LITERALS = [
6+
'0_0_0',
7+
'4_2',
8+
'1_0000_0000',
9+
'0b1001_0100',
10+
'0xffff_ffff',
11+
'0o5_7_7',
12+
'1_00_00.5',
13+
'1_00_00.5e5',
14+
'1_00_00e5_1',
15+
'1e1_0',
16+
'.1_4',
17+
'.1_4e1',
18+
'0b_0',
19+
'0x_f',
20+
'0o_5',
21+
'1_00_00j',
22+
'1_00_00.5j',
23+
'1_00_00e5_1j',
24+
'.1_4j',
25+
'(1_2.5+3_3j)',
26+
'(.5_6j)',
27+
]
28+
INVALID_UNDERSCORE_LITERALS = [
29+
# Trailing underscores:
30+
'0_',
31+
'42_',
32+
'1.4j_',
33+
'0x_',
34+
'0b1_',
35+
'0xf_',
36+
'0o5_',
37+
'0 if 1_Else 1',
38+
# Underscores in the base selector:
39+
'0_b0',
40+
'0_xf',
41+
'0_o5',
42+
# Old-style octal, still disallowed:
43+
'0_7',
44+
'09_99',
45+
# Multiple consecutive underscores:
46+
'4_______2',
47+
'0.1__4',
48+
'0.1__4j',
49+
'0b1001__0100',
50+
'0xffff__ffff',
51+
'0x___',
52+
'0o5__77',
53+
'1e1__0',
54+
'1e1__0j',
55+
# Underscore right before a dot:
56+
'1_.4',
57+
'1_.4j',
58+
# Underscore right after a dot:
59+
'1._4',
60+
'1._4j',
61+
'._5',
62+
'._5j',
63+
# Underscore right after a sign:
64+
'1.0e+_1',
65+
'1.0e+_1j',
66+
# Underscore right before j:
67+
'1.4_j',
68+
'1.4e5_j',
69+
# Underscore right before e:
70+
'1_e1',
71+
'1.4_e1',
72+
'1.4_e1j',
73+
# Underscore right after e:
74+
'1e_1',
75+
'1.4e_1',
76+
'1.4e_1j',
77+
# Complex cases with parens:
78+
'(1+1.5_j_)',
79+
'(1+1.5_j)',
80+
]
81+

Lib/test/test_complex.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
import sys
33
from test import support
44
from test.support.testcase import ComplexesAreIdenticalMixin
5-
from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
6-
INVALID_UNDERSCORE_LITERALS)
5+
from test.support.numbers import (
6+
VALID_UNDERSCORE_LITERALS,
7+
INVALID_UNDERSCORE_LITERALS,
8+
)
79

810
from random import random
911
from math import isnan, copysign

Lib/test/test_float.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99

1010
from test import support
1111
from test.support.testcase import FloatsAreIdenticalMixin
12-
from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
13-
INVALID_UNDERSCORE_LITERALS)
12+
from test.support.numbers import (
13+
VALID_UNDERSCORE_LITERALS,
14+
INVALID_UNDERSCORE_LITERALS,
15+
)
1416
from math import isinf, isnan, copysign, ldexp
1517
import math
1618

Lib/test/test_grammar.py

Lines changed: 4 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -17,88 +17,10 @@
1717
import typing
1818
from test.typinganndata import ann_module2
1919
import test
20-
21-
# These are shared with test_tokenize and other test modules.
22-
#
23-
# Note: since several test cases filter out floats by looking for "e" and ".",
24-
# don't add hexadecimal literals that contain "e" or "E".
25-
VALID_UNDERSCORE_LITERALS = [
26-
'0_0_0',
27-
'4_2',
28-
'1_0000_0000',
29-
'0b1001_0100',
30-
'0xffff_ffff',
31-
'0o5_7_7',
32-
'1_00_00.5',
33-
'1_00_00.5e5',
34-
'1_00_00e5_1',
35-
'1e1_0',
36-
'.1_4',
37-
'.1_4e1',
38-
'0b_0',
39-
'0x_f',
40-
'0o_5',
41-
'1_00_00j',
42-
'1_00_00.5j',
43-
'1_00_00e5_1j',
44-
'.1_4j',
45-
'(1_2.5+3_3j)',
46-
'(.5_6j)',
47-
]
48-
INVALID_UNDERSCORE_LITERALS = [
49-
# Trailing underscores:
50-
'0_',
51-
'42_',
52-
'1.4j_',
53-
'0x_',
54-
'0b1_',
55-
'0xf_',
56-
'0o5_',
57-
'0 if 1_Else 1',
58-
# Underscores in the base selector:
59-
'0_b0',
60-
'0_xf',
61-
'0_o5',
62-
# Old-style octal, still disallowed:
63-
'0_7',
64-
'09_99',
65-
# Multiple consecutive underscores:
66-
'4_______2',
67-
'0.1__4',
68-
'0.1__4j',
69-
'0b1001__0100',
70-
'0xffff__ffff',
71-
'0x___',
72-
'0o5__77',
73-
'1e1__0',
74-
'1e1__0j',
75-
# Underscore right before a dot:
76-
'1_.4',
77-
'1_.4j',
78-
# Underscore right after a dot:
79-
'1._4',
80-
'1._4j',
81-
'._5',
82-
'._5j',
83-
# Underscore right after a sign:
84-
'1.0e+_1',
85-
'1.0e+_1j',
86-
# Underscore right before j:
87-
'1.4_j',
88-
'1.4e5_j',
89-
# Underscore right before e:
90-
'1_e1',
91-
'1.4_e1',
92-
'1.4_e1j',
93-
# Underscore right after e:
94-
'1e_1',
95-
'1.4e_1',
96-
'1.4e_1j',
97-
# Complex cases with parens:
98-
'(1+1.5_j_)',
99-
'(1+1.5_j)',
100-
]
101-
20+
from test.support.numbers import (
21+
VALID_UNDERSCORE_LITERALS,
22+
INVALID_UNDERSCORE_LITERALS,
23+
)
10224

10325
class TokenTests(unittest.TestCase):
10426

Lib/test/test_int.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import unittest
44
from unittest import mock
55
from test import support
6-
from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
7-
INVALID_UNDERSCORE_LITERALS)
6+
from test.support.numbers import (
7+
VALID_UNDERSCORE_LITERALS,
8+
INVALID_UNDERSCORE_LITERALS,
9+
)
810

911
try:
1012
import _pylong

Lib/test/test_tokenize.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
from textwrap import dedent
88
from unittest import TestCase, mock
99
from test import support
10-
from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
11-
INVALID_UNDERSCORE_LITERALS)
1210
from test.support import os_helper
1311
from test.support.script_helper import run_test_script, make_script, run_python_until_end
12+
from test.support.numbers import (
13+
VALID_UNDERSCORE_LITERALS,
14+
INVALID_UNDERSCORE_LITERALS,
15+
)
16+
1417

1518
# Converts a source string into a list of textual representation
1619
# of the tokens such as:

0 commit comments

Comments
 (0)