Skip to content

Commit

Permalink
Pull in main
Browse files Browse the repository at this point in the history
  • Loading branch information
erlend-aasland committed Apr 14, 2024
2 parents d8e8cc5 + 8fc953f commit 95fdad1
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 23 deletions.
24 changes: 11 additions & 13 deletions Lib/configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,18 @@
between keys and values are surrounded by spaces.
"""

from collections.abc import MutableMapping
# Do not import dataclasses; overhead is unacceptable (gh-117703)

from collections.abc import Iterable, MutableMapping
from collections import ChainMap as _ChainMap
import contextlib
from dataclasses import dataclass, field
import functools
import io
import itertools
import os
import re
import sys
from typing import Iterable
import types

__all__ = ("NoSectionError", "DuplicateOptionError", "DuplicateSectionError",
"NoOptionError", "InterpolationError", "InterpolationDepthError",
Expand Down Expand Up @@ -538,29 +539,26 @@ def _interpolate_some(self, parser, option, accum, rest, section, map,
"found: %r" % (rest,))


@dataclass
class _ReadState:
elements_added : set[str] = field(default_factory=set)
elements_added : set[str]
cursect : dict[str, str] | None = None
sectname : str | None = None
optname : str | None = None
lineno : int = 0
indent_level : int = 0
errors : list[ParsingError] = field(default_factory=list)

errors : list[ParsingError]

@dataclass
class _Prefixes:
full : Iterable[str]
inline : Iterable[str]
def __init__(self):
self.elements_added = set()
self.errors = list()


class _Line(str):

def __new__(cls, val, *args, **kwargs):
return super().__new__(cls, val)

def __init__(self, val, prefixes: _Prefixes):
def __init__(self, val, prefixes):
self.prefixes = prefixes

@functools.cached_property
Expand Down Expand Up @@ -653,7 +651,7 @@ def __init__(self, defaults=None, dict_type=_default_dict,
else:
self._optcre = re.compile(self._OPT_TMPL.format(delim=d),
re.VERBOSE)
self._prefixes = _Prefixes(
self._prefixes = types.SimpleNamespace(
full=tuple(comment_prefixes or ()),
inline=tuple(inline_comment_prefixes or ()),
)
Expand Down
27 changes: 24 additions & 3 deletions Lib/test/test_json/test_decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,34 @@
class TestDecode:
def test_decimal(self):
rval = self.loads('1.1', parse_float=decimal.Decimal)
self.assertTrue(isinstance(rval, decimal.Decimal))
self.assertIsInstance(rval, decimal.Decimal)
self.assertEqual(rval, decimal.Decimal('1.1'))

def test_float(self):
rval = self.loads('1', parse_int=float)
self.assertTrue(isinstance(rval, float))
self.assertIsInstance(rval, float)
self.assertEqual(rval, 1.0)

def test_bytes(self):
self.assertEqual(self.loads(b"1"), 1)

def test_parse_constant(self):
for constant, expected in [
("Infinity", "INFINITY"),
("-Infinity", "-INFINITY"),
("NaN", "NAN"),
]:
self.assertEqual(
self.loads(constant, parse_constant=str.upper), expected
)

def test_constant_invalid_case(self):
for constant in [
"nan", "NAN", "naN", "infinity", "INFINITY", "inFiniTy"
]:
with self.assertRaises(self.JSONDecodeError):
self.loads(constant)

def test_empty_objects(self):
self.assertEqual(self.loads('{}'), {})
self.assertEqual(self.loads('[]'), [])
Expand Down Expand Up @@ -88,7 +108,8 @@ def test_string_with_utf8_bom(self):
self.json.load(StringIO(bom_json))
self.assertIn('BOM', str(cm.exception))
# make sure that the BOM is not detected in the middle of a string
bom_in_str = '"{}"'.format(''.encode('utf-8-sig').decode('utf-8'))
bom = ''.encode('utf-8-sig').decode('utf-8')
bom_in_str = f'"{bom}"'
self.assertEqual(self.loads(bom_in_str), '\ufeff')
self.assertEqual(self.json.load(StringIO(bom_in_str)), '\ufeff')

Expand Down
3 changes: 1 addition & 2 deletions Lib/test/test_json/test_encode_basestring_ascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ def test_encode_basestring_ascii(self):
for input_string, expect in CASES:
result = self.json.encoder.encode_basestring_ascii(input_string)
self.assertEqual(result, expect,
'{0!r} != {1!r} for {2}({3!r})'.format(
result, expect, fname, input_string))
f'{result!r} != {expect!r} for {fname}({input_string!r})')

def test_ordered_dict(self):
# See issue 6105
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_json/test_fail.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def test_failures(self):
except self.JSONDecodeError:
pass
else:
self.fail("Expected failure for fail{0}.json: {1!r}".format(idx, doc))
self.fail(f"Expected failure for fail{idx}.json: {doc!r}")

def test_non_string_keys_dict(self):
data = {'a' : 1, (1, 2) : 2}
Expand Down
13 changes: 9 additions & 4 deletions Lib/test/test_json/test_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@ def test_encoding4(self):
def test_encoding5(self):
u = '\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
j = self.dumps(u, ensure_ascii=False)
self.assertEqual(j, '"{0}"'.format(u))
self.assertEqual(j, f'"{u}"')

def test_encoding6(self):
u = '\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
j = self.dumps([u], ensure_ascii=False)
self.assertEqual(j, '["{0}"]'.format(u))
self.assertEqual(j, f'["{u}"]')

def test_encoding7(self):
u = '\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
j = self.dumps(u + "\n", ensure_ascii=False)
self.assertEqual(j, f'"{u}\\n"')

def test_big_unicode_encode(self):
u = '\U0001d120'
Expand All @@ -34,13 +39,13 @@ def test_big_unicode_encode(self):

def test_big_unicode_decode(self):
u = 'z\U0001d120x'
self.assertEqual(self.loads('"' + u + '"'), u)
self.assertEqual(self.loads(f'"{u}"'), u)
self.assertEqual(self.loads('"z\\ud834\\udd20x"'), u)

def test_unicode_decode(self):
for i in range(0, 0xd7ff):
u = chr(i)
s = '"\\u{0:04x}"'.format(i)
s = f'"\\u{i:04x}"'
self.assertEqual(self.loads(s), u)

def test_unicode_preservation(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Largely restored import time performance of configparser by avoiding
dataclasses.

0 comments on commit 95fdad1

Please sign in to comment.