Skip to content

Commit

Permalink
gh-118761: Improve import time of tomllib (#128907)
Browse files Browse the repository at this point in the history
Improve import time of `tomllib`  (in sync with upstream)
  • Loading branch information
hukkin authored Jan 17, 2025
1 parent c879de7 commit ea6cc26
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
9 changes: 9 additions & 0 deletions Lib/test/test_tomllib/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import copy
import datetime
from decimal import Decimal as D
import importlib
from pathlib import Path
import sys
import tempfile
Expand Down Expand Up @@ -113,3 +114,11 @@ def test_inline_table_recursion_limit(self):
nest_count=nest_count):
recursive_table_toml = nest_count * "key = {" + nest_count * "}"
tomllib.loads(recursive_table_toml)

def test_types_import(self):
"""Test that `_types` module runs.
The module is for type annotations only, so it is otherwise
never imported by tests.
"""
importlib.import_module(f"{tomllib.__name__}._types")
31 changes: 19 additions & 12 deletions Lib/tomllib/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@

from __future__ import annotations

from collections.abc import Iterable
import string
from types import MappingProxyType
from typing import Any, BinaryIO, NamedTuple
import warnings

from ._re import (
RE_DATETIME,
Expand All @@ -18,7 +14,13 @@
match_to_localtime,
match_to_number,
)
from ._types import Key, ParseFloat, Pos

TYPE_CHECKING = False
if TYPE_CHECKING:
from collections.abc import Iterable
from typing import IO, Any

from ._types import Key, ParseFloat, Pos

ASCII_CTRL = frozenset(chr(i) for i in range(32)) | frozenset(chr(127))

Expand All @@ -34,9 +36,11 @@

TOML_WS = frozenset(" \t")
TOML_WS_AND_NEWLINE = TOML_WS | frozenset("\n")
BARE_KEY_CHARS = frozenset(string.ascii_letters + string.digits + "-_")
BARE_KEY_CHARS = frozenset(
"abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "0123456789" "-_"
)
KEY_INITIAL_CHARS = BARE_KEY_CHARS | frozenset("\"'")
HEXDIGIT_CHARS = frozenset(string.hexdigits)
HEXDIGIT_CHARS = frozenset("abcdef" "ABCDEF" "0123456789")

BASIC_STR_ESCAPE_REPLACEMENTS = MappingProxyType(
{
Expand Down Expand Up @@ -80,6 +84,8 @@ def __init__(
or not isinstance(doc, str)
or not isinstance(pos, int)
):
import warnings

warnings.warn(
"Free-form arguments for TOMLDecodeError are deprecated. "
"Please set 'msg' (str), 'doc' (str) and 'pos' (int) arguments only.",
Expand Down Expand Up @@ -115,7 +121,7 @@ def __init__(
self.colno = colno


def load(fp: BinaryIO, /, *, parse_float: ParseFloat = float) -> dict[str, Any]:
def load(fp: IO[bytes], /, *, parse_float: ParseFloat = float) -> dict[str, Any]:
"""Parse TOML from a binary file object."""
b = fp.read()
try:
Expand All @@ -139,7 +145,7 @@ def loads(s: str, /, *, parse_float: ParseFloat = float) -> dict[str, Any]: # n
f"Expected str object, not '{type(s).__qualname__}'"
) from None
pos = 0
out = Output(NestedDict(), Flags())
out = Output()
header: Key = ()
parse_float = make_safe_parse_float(parse_float)

Expand Down Expand Up @@ -290,9 +296,10 @@ def append_nest_to_list(self, key: Key) -> None:
cont[last_key] = [{}]


class Output(NamedTuple):
data: NestedDict
flags: Flags
class Output:
def __init__(self) -> None:
self.data = NestedDict()
self.flags = Flags()


def skip_chars(src: str, pos: Pos, chars: Iterable[str]) -> Pos:
Expand Down
7 changes: 5 additions & 2 deletions Lib/tomllib/_re.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
from datetime import date, datetime, time, timedelta, timezone, tzinfo
from functools import lru_cache
import re
from typing import Any

from ._types import ParseFloat
TYPE_CHECKING = False
if TYPE_CHECKING:
from typing import Any

from ._types import ParseFloat

# E.g.
# - 00:32:00.999999
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve import time of :mod:`tomllib` by removing ``typing``, ``string``,
and ``tomllib._types`` imports. Patch by Taneli Hukkinen.

0 comments on commit ea6cc26

Please sign in to comment.