Skip to content

Commit d297e73

Browse files
authored
Merge pull request RustPython#5172 from Blues-star/main
Update `configparser.py` and `test_configparser.py` from CPython v3.12
2 parents 97a0705 + d061837 commit d297e73

File tree

2 files changed

+13
-96
lines changed

2 files changed

+13
-96
lines changed

Lib/configparser.py

Lines changed: 6 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
instance. It will be used as the handler for option value
6060
pre-processing when using getters. RawConfigParser objects don't do
6161
any sort of interpolation, whereas ConfigParser uses an instance of
62-
BasicInterpolation. The library also provides a ``zc.buildbot``
62+
BasicInterpolation. The library also provides a ``zc.buildout``
6363
inspired ExtendedInterpolation implementation.
6464
6565
When `converters` is given, it should be a dictionary where each key
@@ -149,14 +149,14 @@
149149
import sys
150150
import warnings
151151

152-
__all__ = ["NoSectionError", "DuplicateOptionError", "DuplicateSectionError",
152+
__all__ = ("NoSectionError", "DuplicateOptionError", "DuplicateSectionError",
153153
"NoOptionError", "InterpolationError", "InterpolationDepthError",
154154
"InterpolationMissingOptionError", "InterpolationSyntaxError",
155155
"ParsingError", "MissingSectionHeaderError",
156-
"ConfigParser", "SafeConfigParser", "RawConfigParser",
156+
"ConfigParser", "RawConfigParser",
157157
"Interpolation", "BasicInterpolation", "ExtendedInterpolation",
158158
"LegacyInterpolation", "SectionProxy", "ConverterMapping",
159-
"DEFAULTSECT", "MAX_INTERPOLATION_DEPTH"]
159+
"DEFAULTSECT", "MAX_INTERPOLATION_DEPTH")
160160

161161
_default_dict = dict
162162
DEFAULTSECT = "DEFAULT"
@@ -298,41 +298,12 @@ def __init__(self, option, section, rawval):
298298
class ParsingError(Error):
299299
"""Raised when a configuration file does not follow legal syntax."""
300300

301-
def __init__(self, source=None, filename=None):
302-
# Exactly one of `source'/`filename' arguments has to be given.
303-
# `filename' kept for compatibility.
304-
if filename and source:
305-
raise ValueError("Cannot specify both `filename' and `source'. "
306-
"Use `source'.")
307-
elif not filename and not source:
308-
raise ValueError("Required argument `source' not given.")
309-
elif filename:
310-
source = filename
311-
Error.__init__(self, 'Source contains parsing errors: %r' % source)
301+
def __init__(self, source):
302+
super().__init__(f'Source contains parsing errors: {source!r}')
312303
self.source = source
313304
self.errors = []
314305
self.args = (source, )
315306

316-
@property
317-
def filename(self):
318-
"""Deprecated, use `source'."""
319-
warnings.warn(
320-
"The 'filename' attribute will be removed in Python 3.12. "
321-
"Use 'source' instead.",
322-
DeprecationWarning, stacklevel=2
323-
)
324-
return self.source
325-
326-
@filename.setter
327-
def filename(self, value):
328-
"""Deprecated, user `source'."""
329-
warnings.warn(
330-
"The 'filename' attribute will be removed in Python 3.12. "
331-
"Use 'source' instead.",
332-
DeprecationWarning, stacklevel=2
333-
)
334-
self.source = value
335-
336307
def append(self, lineno, line):
337308
self.errors.append((lineno, line))
338309
self.message += '\n\t[line %2d]: %s' % (lineno, line)
@@ -769,15 +740,6 @@ def read_dict(self, dictionary, source='<dict>'):
769740
elements_added.add((section, key))
770741
self.set(section, key, value)
771742

772-
def readfp(self, fp, filename=None):
773-
"""Deprecated, use read_file instead."""
774-
warnings.warn(
775-
"This method will be removed in Python 3.12. "
776-
"Use 'parser.read_file()' instead.",
777-
DeprecationWarning, stacklevel=2
778-
)
779-
self.read_file(fp, source=filename)
780-
781743
def get(self, section, option, *, raw=False, vars=None, fallback=_UNSET):
782744
"""Get an option value for a given section.
783745
@@ -1240,19 +1202,6 @@ def _read_defaults(self, defaults):
12401202
self._interpolation = hold_interpolation
12411203

12421204

1243-
class SafeConfigParser(ConfigParser):
1244-
"""ConfigParser alias for backwards compatibility purposes."""
1245-
1246-
def __init__(self, *args, **kwargs):
1247-
super().__init__(*args, **kwargs)
1248-
warnings.warn(
1249-
"The SafeConfigParser class has been renamed to ConfigParser "
1250-
"in Python 3.2. This alias will be removed in Python 3.12."
1251-
" Use ConfigParser directly instead.",
1252-
DeprecationWarning, stacklevel=2
1253-
)
1254-
1255-
12561205
class SectionProxy(MutableMapping):
12571206
"""A proxy for a single section from a parser."""
12581207

Lib/test/test_configparser.py

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def basic_test(self, cf):
114114

115115
# The use of spaces in the section names serves as a
116116
# regression test for SourceForge bug #583248:
117-
# http://www.python.org/sf/583248
117+
# https://bugs.python.org/issue583248
118118

119119
# API access
120120
eq(cf.get('Foo Bar', 'foo'), 'bar1')
@@ -934,7 +934,7 @@ def test_items(self):
934934
('name', 'value')])
935935

936936
def test_safe_interpolation(self):
937-
# See http://www.python.org/sf/511737
937+
# See https://bugs.python.org/issue511737
938938
cf = self.fromstring("[section]\n"
939939
"option1{eq}xxx\n"
940940
"option2{eq}%(option1)s/xxx\n"
@@ -1614,23 +1614,12 @@ def test_interpolation_depth_error(self):
16141614
self.assertEqual(error.section, 'section')
16151615

16161616
def test_parsing_error(self):
1617-
with self.assertRaises(ValueError) as cm:
1617+
with self.assertRaises(TypeError) as cm:
16181618
configparser.ParsingError()
1619-
self.assertEqual(str(cm.exception), "Required argument `source' not "
1620-
"given.")
1621-
with self.assertRaises(ValueError) as cm:
1622-
configparser.ParsingError(source='source', filename='filename')
1623-
self.assertEqual(str(cm.exception), "Cannot specify both `filename' "
1624-
"and `source'. Use `source'.")
1625-
error = configparser.ParsingError(filename='source')
1619+
error = configparser.ParsingError(source='source')
1620+
self.assertEqual(error.source, 'source')
1621+
error = configparser.ParsingError('source')
16261622
self.assertEqual(error.source, 'source')
1627-
with warnings.catch_warnings(record=True) as w:
1628-
warnings.simplefilter("always", DeprecationWarning)
1629-
self.assertEqual(error.filename, 'source')
1630-
error.filename = 'filename'
1631-
self.assertEqual(error.source, 'filename')
1632-
for warning in w:
1633-
self.assertTrue(warning.category is DeprecationWarning)
16341623

16351624
def test_interpolation_validation(self):
16361625
parser = configparser.ConfigParser()
@@ -1649,27 +1638,6 @@ def test_interpolation_validation(self):
16491638
self.assertEqual(str(cm.exception), "bad interpolation variable "
16501639
"reference '%(()'")
16511640

1652-
def test_readfp_deprecation(self):
1653-
sio = io.StringIO("""
1654-
[section]
1655-
option = value
1656-
""")
1657-
parser = configparser.ConfigParser()
1658-
with warnings.catch_warnings(record=True) as w:
1659-
warnings.simplefilter("always", DeprecationWarning)
1660-
parser.readfp(sio, filename='StringIO')
1661-
for warning in w:
1662-
self.assertTrue(warning.category is DeprecationWarning)
1663-
self.assertEqual(len(parser), 2)
1664-
self.assertEqual(parser['section']['option'], 'value')
1665-
1666-
def test_safeconfigparser_deprecation(self):
1667-
with warnings.catch_warnings(record=True) as w:
1668-
warnings.simplefilter("always", DeprecationWarning)
1669-
parser = configparser.SafeConfigParser()
1670-
for warning in w:
1671-
self.assertTrue(warning.category is DeprecationWarning)
1672-
16731641
def test_legacyinterpolation_deprecation(self):
16741642
with warnings.catch_warnings(record=True) as w:
16751643
warnings.simplefilter("always", DeprecationWarning)
@@ -1843,7 +1811,7 @@ def test_parsingerror(self):
18431811
self.assertEqual(e1.source, e2.source)
18441812
self.assertEqual(e1.errors, e2.errors)
18451813
self.assertEqual(repr(e1), repr(e2))
1846-
e1 = configparser.ParsingError(filename='filename')
1814+
e1 = configparser.ParsingError('filename')
18471815
e1.append(1, 'line1')
18481816
e1.append(2, 'line2')
18491817
e1.append(3, 'line3')

0 commit comments

Comments
 (0)