[Python-checkins] bpo-45173 Remove configparser deprecations (GH-28292)

ambv webhook-mailer at python.org
Mon Sep 13 13:12:45 EDT 2021


https://github.com/python/cpython/commit/1fc41ae8709e20d741bd86c2345173688a5e84b0
commit: 1fc41ae8709e20d741bd86c2345173688a5e84b0
branch: main
author: Hugo van Kemenade <hugovk at users.noreply.github.com>
committer: ambv <lukasz at langa.pl>
date: 2021-09-13T19:12:36+02:00
summary:

bpo-45173 Remove configparser deprecations (GH-28292)

In the configparser module, these have been deprecated since Python 3.2:

* the SafeConfigParser class,
* the filename property of the ParsingError class,
* the readfp method of the ConfigParser class,

files:
A Misc/NEWS.d/next/Library/2021-09-11-17-46-20.bpo-45173.UptGAn.rst
M Doc/library/configparser.rst
M Doc/whatsnew/3.11.rst
M Lib/configparser.py
M Lib/test/test_configparser.py

diff --git a/Doc/library/configparser.rst b/Doc/library/configparser.rst
index 2bb425930bba7..1ec0fa1586afe 100644
--- a/Doc/library/configparser.rst
+++ b/Doc/library/configparser.rst
@@ -1200,28 +1200,6 @@ ConfigParser Objects
       names is stripped before :meth:`optionxform` is called.
 
 
-   .. method:: readfp(fp, filename=None)
-
-      .. deprecated:: 3.2
-         Use :meth:`read_file` instead.
-
-      .. versionchanged:: 3.2
-         :meth:`readfp` now iterates on *fp* instead of calling ``fp.readline()``.
-
-      For existing code calling :meth:`readfp` with arguments which don't
-      support iteration, the following generator may be used as a wrapper
-      around the file-like object::
-
-         def readline_generator(fp):
-             line = fp.readline()
-             while line:
-                 yield line
-                 line = fp.readline()
-
-      Instead of ``parser.readfp(fp)`` use
-      ``parser.read_file(readline_generator(fp))``.
-
-
 .. data:: MAX_INTERPOLATION_DEPTH
 
    The maximum depth for recursive interpolation for :meth:`get` when the *raw*
@@ -1359,6 +1337,9 @@ Exceptions
       The ``filename`` attribute and :meth:`__init__` argument were renamed to
       ``source`` for consistency.
 
+   .. versionchanged:: 3.11
+      The deprecated ``filename`` attribute was removed.
+
 
 .. rubric:: Footnotes
 
diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst
index 7619e48103bc7..fbc5fa3d286a5 100644
--- a/Doc/whatsnew/3.11.rst
+++ b/Doc/whatsnew/3.11.rst
@@ -284,6 +284,13 @@ Removed
   the ``l*gettext()`` functions.
   (Contributed by Dong-hee Na and Serhiy Storchaka in :issue:`44235`.)
 
+* Remove from the :mod:`configparser` module:
+  the :class:`SafeConfigParser` class,
+  the :attr:`filename` property of the :class:`ParsingError` class,
+  the :meth:`readfp` method of the :class:`ConfigParser` class,
+  deprecated since Python 3.2.
+  (Contributed by Hugo van Kemenade in :issue:`45173`.)
+
 
 Optimizations
 =============
diff --git a/Lib/configparser.py b/Lib/configparser.py
index 042a5c74b696f..c10309acf1ab4 100644
--- a/Lib/configparser.py
+++ b/Lib/configparser.py
@@ -146,13 +146,12 @@
 import os
 import re
 import sys
-import warnings
 
 __all__ = ["NoSectionError", "DuplicateOptionError", "DuplicateSectionError",
            "NoOptionError", "InterpolationError", "InterpolationDepthError",
            "InterpolationMissingOptionError", "InterpolationSyntaxError",
            "ParsingError", "MissingSectionHeaderError",
-           "ConfigParser", "SafeConfigParser", "RawConfigParser",
+           "ConfigParser", "RawConfigParser",
            "Interpolation", "BasicInterpolation",  "ExtendedInterpolation",
            "LegacyInterpolation", "SectionProxy", "ConverterMapping",
            "DEFAULTSECT", "MAX_INTERPOLATION_DEPTH"]
@@ -312,26 +311,6 @@ def __init__(self, source=None, filename=None):
         self.errors = []
         self.args = (source, )
 
-    @property
-    def filename(self):
-        """Deprecated, use `source'."""
-        warnings.warn(
-            "The 'filename' attribute will be removed in future versions.  "
-            "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 future versions.  "
-            "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)
@@ -754,15 +733,6 @@ def read_dict(self, dictionary, source='<dict>'):
                 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 future versions.  "
-            "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.
 
@@ -1225,19 +1195,6 @@ def _read_defaults(self, defaults):
             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 future versions."
-            " 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 e9b03e6c62ef1..cedf505122d13 100644
--- a/Lib/test/test_configparser.py
+++ b/Lib/test/test_configparser.py
@@ -1612,13 +1612,6 @@ def test_parsing_error(self):
                                             "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()
@@ -1637,27 +1630,6 @@ def test_interpolation_validation(self):
         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("""
diff --git a/Misc/NEWS.d/next/Library/2021-09-11-17-46-20.bpo-45173.UptGAn.rst b/Misc/NEWS.d/next/Library/2021-09-11-17-46-20.bpo-45173.UptGAn.rst
new file mode 100644
index 0000000000000..0b29ec2364edc
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-09-11-17-46-20.bpo-45173.UptGAn.rst
@@ -0,0 +1,7 @@
+Remove from the :mod:`configparser` module:
+the :class:`SafeConfigParser` class,
+the :attr:`filename` property of the :class:`ParsingError` class,
+the :meth:`readfp` method of the :class:`ConfigParser` class,
+deprecated since Python 3.2.
+
+Patch by Hugo van Kemenade.



More information about the Python-checkins mailing list