summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorHugo van Kemenade <hugovk@users.noreply.github.com>2022-02-02 16:59:39 (GMT)
committerGitHub <noreply@github.com>2022-02-02 16:59:39 (GMT)
commite8659b47dece5a272111c0af5e340c364a9f807b (patch)
tree5721d26933066ef1da3f04c5863955926b057fca /Lib
parent38e0b9efdf164be656782db0f969fb536cfbcaf1 (diff)
downloadcpython-e8659b47dece5a272111c0af5e340c364a9f807b.zip
cpython-e8659b47dece5a272111c0af5e340c364a9f807b.tar.gz
cpython-e8659b47dece5a272111c0af5e340c364a9f807b.tar.bz2
bpo-45173: Keep configparser deprecations until Python 3.12 (GH-30952)
* Revert "bpo-45173 Remove configparser deprecations" This reverts commit df2284bc416dcccba1125b12af4499c45baabe4f. * bpo-45173: Note these configparser deprecations will be removed in 3.12
Diffstat (limited to 'Lib')
-rw-r--r--Lib/configparser.py45
-rw-r--r--Lib/test/test_configparser.py28
2 files changed, 72 insertions, 1 deletions
diff --git a/Lib/configparser.py b/Lib/configparser.py
index c10309a..3470624 100644
--- a/Lib/configparser.py
+++ b/Lib/configparser.py
@@ -146,12 +146,13 @@ import itertools
import os
import re
import sys
+import warnings
__all__ = ["NoSectionError", "DuplicateOptionError", "DuplicateSectionError",
"NoOptionError", "InterpolationError", "InterpolationDepthError",
"InterpolationMissingOptionError", "InterpolationSyntaxError",
"ParsingError", "MissingSectionHeaderError",
- "ConfigParser", "RawConfigParser",
+ "ConfigParser", "SafeConfigParser", "RawConfigParser",
"Interpolation", "BasicInterpolation", "ExtendedInterpolation",
"LegacyInterpolation", "SectionProxy", "ConverterMapping",
"DEFAULTSECT", "MAX_INTERPOLATION_DEPTH"]
@@ -311,6 +312,26 @@ class ParsingError(Error):
self.errors = []
self.args = (source, )
+ @property
+ def filename(self):
+ """Deprecated, use `source'."""
+ warnings.warn(
+ "The 'filename' attribute will be removed in Python 3.12. "
+ "Use 'source' instead.",
+ DeprecationWarning, stacklevel=2
+ )
+ return self.source
+
+ @filename.setter
+ def filename(self, value):
+ """Deprecated, user `source'."""
+ warnings.warn(
+ "The 'filename' attribute will be removed in Python 3.12. "
+ "Use 'source' instead.",
+ DeprecationWarning, stacklevel=2
+ )
+ self.source = value
+
def append(self, lineno, line):
self.errors.append((lineno, line))
self.message += '\n\t[line %2d]: %s' % (lineno, line)
@@ -733,6 +754,15 @@ class RawConfigParser(MutableMapping):
elements_added.add((section, key))
self.set(section, key, value)
+ def readfp(self, fp, filename=None):
+ """Deprecated, use read_file instead."""
+ warnings.warn(
+ "This method will be removed in Python 3.12. "
+ "Use 'parser.read_file()' instead.",
+ DeprecationWarning, stacklevel=2
+ )
+ self.read_file(fp, source=filename)
+
def get(self, section, option, *, raw=False, vars=None, fallback=_UNSET):
"""Get an option value for a given section.
@@ -1195,6 +1225,19 @@ class ConfigParser(RawConfigParser):
self._interpolation = hold_interpolation
+class SafeConfigParser(ConfigParser):
+ """ConfigParser alias for backwards compatibility purposes."""
+
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ warnings.warn(
+ "The SafeConfigParser class has been renamed to ConfigParser "
+ "in Python 3.2. This alias will be removed in Python 3.12."
+ " Use ConfigParser directly instead.",
+ DeprecationWarning, stacklevel=2
+ )
+
+
class SectionProxy(MutableMapping):
"""A proxy for a single section from a parser."""
diff --git a/Lib/test/test_configparser.py b/Lib/test/test_configparser.py
index cedf505..e9b03e6 100644
--- a/Lib/test/test_configparser.py
+++ b/Lib/test/test_configparser.py
@@ -1612,6 +1612,13 @@ class CoverageOneHundredTestCase(unittest.TestCase):
"and `source'. Use `source'.")
error = configparser.ParsingError(filename='source')
self.assertEqual(error.source, 'source')
+ with warnings.catch_warnings(record=True) as w:
+ warnings.simplefilter("always", DeprecationWarning)
+ self.assertEqual(error.filename, 'source')
+ error.filename = 'filename'
+ self.assertEqual(error.source, 'filename')
+ for warning in w:
+ self.assertTrue(warning.category is DeprecationWarning)
def test_interpolation_validation(self):
parser = configparser.ConfigParser()
@@ -1630,6 +1637,27 @@ class CoverageOneHundredTestCase(unittest.TestCase):
self.assertEqual(str(cm.exception), "bad interpolation variable "
"reference '%(()'")
+ def test_readfp_deprecation(self):
+ sio = io.StringIO("""
+ [section]
+ option = value
+ """)
+ parser = configparser.ConfigParser()
+ with warnings.catch_warnings(record=True) as w:
+ warnings.simplefilter("always", DeprecationWarning)
+ parser.readfp(sio, filename='StringIO')
+ for warning in w:
+ self.assertTrue(warning.category is DeprecationWarning)
+ self.assertEqual(len(parser), 2)
+ self.assertEqual(parser['section']['option'], 'value')
+
+ def test_safeconfigparser_deprecation(self):
+ with warnings.catch_warnings(record=True) as w:
+ warnings.simplefilter("always", DeprecationWarning)
+ parser = configparser.SafeConfigParser()
+ for warning in w:
+ self.assertTrue(warning.category is DeprecationWarning)
+
def test_sectionproxy_repr(self):
parser = configparser.ConfigParser()
parser.read_string("""