Skip to content

Commit 4656c6f

Browse files
committed
Update from tomli
1 parent 8ef9b4b commit 4656c6f

File tree

2 files changed

+14
-100
lines changed

2 files changed

+14
-100
lines changed

Lib/test/test_tomllib/test_misc.py

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import textwrap
1313
import unittest
1414
from test import support
15-
from test.support import os_helper
1615
from test.support.script_helper import assert_python_ok
1716

1817
from . import tomllib
@@ -128,54 +127,19 @@ def test_types_import(self):
128127
"""
129128
importlib.import_module(f"{tomllib.__name__}._types")
130129

131-
def test_try_simple_decimal(self):
132-
try_simple_decimal = tomllib._parser.try_simple_decimal
133-
self.assertEqual(try_simple_decimal("123", 0), (3, 123))
134-
self.assertEqual(try_simple_decimal("123\n", 0), (3, 123))
135-
self.assertEqual(try_simple_decimal("123 456", 0), (3, 123))
136-
self.assertEqual(try_simple_decimal("+123\n", 0), (4, 123))
137-
self.assertEqual(try_simple_decimal("-123\n", 0), (4, -123))
138-
self.assertEqual(try_simple_decimal("0\n", 0), (1, 0))
139-
self.assertEqual(try_simple_decimal("+0\n", 0), (2, 0))
140-
self.assertEqual(try_simple_decimal("-0\n", 0), (2, 0))
141-
self.assertEqual(try_simple_decimal("[23]\n", 1), (3, 23))
142-
self.assertEqual(try_simple_decimal("[23, 24]\n", 1), (3, 23))
143-
self.assertEqual(try_simple_decimal("{x = 42}\n", 5), (7, 42))
144-
145-
self.assertIsNone(try_simple_decimal("+", 0), None)
146-
self.assertIsNone(try_simple_decimal("-", 0), None)
147-
self.assertIsNone(try_simple_decimal("+\n", 0), None)
148-
self.assertIsNone(try_simple_decimal("-\n", 0), None)
149-
self.assertIsNone(try_simple_decimal("+inf\n", 0), None)
150-
self.assertIsNone(try_simple_decimal("-nan\n", 0), None)
151-
self.assertIsNone(try_simple_decimal("0123\n", 0))
152-
self.assertIsNone(try_simple_decimal("1979-05-27\n", 0))
153-
self.assertIsNone(try_simple_decimal("12:32:00\n", 0))
154-
self.assertIsNone(try_simple_decimal("1.0\n", 0))
155-
self.assertIsNone(try_simple_decimal("1_000\n", 0))
156-
self.assertIsNone(try_simple_decimal("0x123\n", 0))
157-
self.assertIsNone(try_simple_decimal("0o123\n", 0))
158-
self.assertIsNone(try_simple_decimal("0b100\n", 0))
159-
160130
def test_lazy_import(self):
161-
# Test that try_simple_decimal() can parse the TOML file without
162-
# importing regular expressions (tomllib._re)
163-
filename = os_helper.TESTFN
164-
self.addCleanup(os_helper.unlink, filename)
165-
toml = textwrap.dedent("""
166-
[metadata]
167-
int = 123
168-
list = [+1, -2, 3]
169-
table = {x=1, y=2}
170-
""")
171-
with open(filename, "w") as fp:
172-
fp.write(toml)
173-
174-
code = textwrap.dedent(f"""
175-
import sys, tomllib
176-
with open({filename!a}, "rb") as fp:
177-
tomllib.load(fp)
131+
# Test the TOML file can be parsed without importing regular
132+
# expressions (tomllib._re)
133+
code = textwrap.dedent("""
134+
import sys, tomllib, textwrap
135+
document = textwrap.dedent('''
136+
[metadata]
137+
key = "text"
138+
array = ["array", "of", "text"]
139+
booleans = [true, false]
140+
''')
141+
tomllib.loads(document)
178142
print("lazy import?", 'tomllib._re' not in sys.modules)
179143
""")
180-
proc = assert_python_ok('-c', code)
181-
self.assertIn(b'lazy import? True', proc.out)
144+
proc = assert_python_ok("-c", code)
145+
self.assertIn(b"lazy import? True", proc.out)

Lib/tomllib/_parser.py

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
from __future__ import annotations
66

77
# Defer loading regular expressions until we actually need them in
8-
# parse_value(). Before that, use try_simple_decimal() to parse simple
9-
# decimal numbers.
8+
# parse_value().
109
__lazy_modules__ = ["tomllib._re"]
1110

1211
from ._re import (
@@ -45,17 +44,6 @@
4544
KEY_INITIAL_CHARS: Final = BARE_KEY_CHARS | frozenset("\"'")
4645
HEXDIGIT_CHARS: Final = frozenset("abcdef" "ABCDEF" "0123456789")
4746

48-
# If one of these follows a "simple decimal" it could mean that
49-
# the value is actually something else (float, datetime...), so
50-
# optimized parsing should be abandoned.
51-
ILLEGAL_AFTER_SIMPLE_DECIMAL: Final = frozenset(
52-
"eE." # decimal
53-
"xbo" # hex, bin, oct
54-
"-" # datetime
55-
":" # localtime
56-
"_0123456789" # complex decimal
57-
)
58-
5947
BASIC_STR_ESCAPE_REPLACEMENTS: Final = frozendict( # type: ignore[name-defined]
6048
{
6149
"\\b": "\u0008", # backspace
@@ -679,37 +667,6 @@ def parse_basic_str(src: str, pos: Pos, *, multiline: bool) -> tuple[Pos, str]:
679667
pos += 1
680668

681669

682-
def try_simple_decimal(
683-
src: str, pos: Pos
684-
) -> None | tuple[Pos, int]:
685-
"""Parse a "simple" decimal integer.
686-
687-
An optimization that tries to parse a simple decimal integer
688-
without underscores. Returns `None` if there's any uncertainty
689-
on correctness.
690-
"""
691-
start_pos = pos
692-
693-
if src.startswith(("+", "-"), pos):
694-
pos += 1
695-
696-
if src.startswith("0", pos):
697-
pos += 1
698-
elif src.startswith(("1", "2", "3", "4", "5", "6", "7", "8", "9"), pos):
699-
pos = skip_chars(src, pos, "0123456789")
700-
else:
701-
return None
702-
703-
try:
704-
next_char = src[pos]
705-
except IndexError:
706-
next_char = None
707-
if next_char in ILLEGAL_AFTER_SIMPLE_DECIMAL:
708-
return None
709-
710-
return pos, int(src[start_pos:pos])
711-
712-
713670
def parse_value(
714671
src: str, pos: Pos, parse_float: ParseFloat
715672
) -> tuple[Pos, Any]:
@@ -748,13 +705,6 @@ def parse_value(
748705
if char == "{":
749706
return parse_inline_table(src, pos, parse_float)
750707

751-
# Try a simple parser for decimal numbers. If it's able to parse all
752-
# numbers, it avoids importing tomllib._re which has an impact on
753-
# the tomllib startup time.
754-
number = try_simple_decimal(src, pos)
755-
if number is not None:
756-
return number
757-
758708
# Dates and times
759709
datetime_match = RE_DATETIME.match(src, pos)
760710
if datetime_match:

0 commit comments

Comments
 (0)