https://github.com/python/cpython/commit/a60520da3cb0c571a361b9d0b8d998e78f38b836
commit: a60520da3cb0c571a361b9d0b8d998e78f38b836
branch: main
author: Victor Stinner <[email protected]>
committer: encukou <[email protected]>
date: 2026-04-30T18:25:32+02:00
summary:

gh-147991: Speed up tomllib import time (GH-147992)

- Use lazy import for regular expressions.
- Use frozendict for string escapes

Co-authored-by: Taneli Hukkinen <[email protected]>
Co-authored-by: Petr Viktorin <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-04-02-05-06-34.gh-issue-147991.2ANtR5.rst
M Lib/test/test_tomllib/test_misc.py
M Lib/tomllib/_parser.py

diff --git a/Lib/test/test_tomllib/test_misc.py 
b/Lib/test/test_tomllib/test_misc.py
index 118fde24d88521..abd0842d10b254 100644
--- a/Lib/test/test_tomllib/test_misc.py
+++ b/Lib/test/test_tomllib/test_misc.py
@@ -9,8 +9,10 @@
 from pathlib import Path
 import sys
 import tempfile
+import textwrap
 import unittest
 from test import support
+from test.support.script_helper import assert_python_ok
 
 from . import tomllib
 
@@ -124,3 +126,20 @@ def test_types_import(self):
         never imported by tests.
         """
         importlib.import_module(f"{tomllib.__name__}._types")
+
+    def test_lazy_import(self):
+        # Test the TOML file can be parsed without importing regular
+        # expressions (tomllib._re)
+        code = textwrap.dedent("""
+            import sys, tomllib, textwrap
+            document = textwrap.dedent('''
+                [metadata]
+                key = "text"
+                array = ["array", "of", "text"]
+                booleans = [true, false]
+            ''')
+            tomllib.loads(document)
+            print("lazy import?", 'tomllib._re' not in sys.modules)
+        """)
+        proc = assert_python_ok("-c", code)
+        self.assertIn(b"lazy import? True", proc.out)
diff --git a/Lib/tomllib/_parser.py b/Lib/tomllib/_parser.py
index b59d0f7d54bdc3..8aa01301dcea32 100644
--- a/Lib/tomllib/_parser.py
+++ b/Lib/tomllib/_parser.py
@@ -4,7 +4,11 @@
 
 from __future__ import annotations
 
-from types import MappingProxyType
+# Defer loading regular expressions until we actually need them in
+# parse_value().
+__lazy_modules__ = ["tomllib._re"]
+
+import sys
 
 from ._re import (
     RE_DATETIME,
@@ -15,6 +19,9 @@
     match_to_number,
 )
 
+if sys.version_info < (3, 15):
+    from types import MappingProxyType as frozendict
+
 TYPE_CHECKING = False
 if TYPE_CHECKING:
     from collections.abc import Iterable
@@ -42,7 +49,7 @@
 KEY_INITIAL_CHARS: Final = BARE_KEY_CHARS | frozenset("\"'")
 HEXDIGIT_CHARS: Final = frozenset("abcdef" "ABCDEF" "0123456789")
 
-BASIC_STR_ESCAPE_REPLACEMENTS: Final = MappingProxyType(
+BASIC_STR_ESCAPE_REPLACEMENTS: Final = frozendict(
     {
         "\\b": "\u0008",  # backspace
         "\\t": "\u0009",  # tab
diff --git 
a/Misc/NEWS.d/next/Library/2026-04-02-05-06-34.gh-issue-147991.2ANtR5.rst 
b/Misc/NEWS.d/next/Library/2026-04-02-05-06-34.gh-issue-147991.2ANtR5.rst
new file mode 100644
index 00000000000000..581c52926c3565
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-04-02-05-06-34.gh-issue-147991.2ANtR5.rst
@@ -0,0 +1,2 @@
+Improve :mod:`tomllib` import time (up to 10x faster). Patch by Victor
+Stinner.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to