changeset 149:614a0a648f48

Work around the deprecation of SafeConfigParser in Python 3.2+
author Franz Glasner <hg@dom66.de>
date Sat, 14 Apr 2018 12:52:15 +0200
parents be352645871c
children 0ac6ffae969f
files configmix/ini.py
diffstat 1 files changed, 13 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/configmix/ini.py	Sat Apr 14 12:44:42 2018 +0200
+++ b/configmix/ini.py	Sat Apr 14 12:52:15 2018 +0200
@@ -14,9 +14,16 @@
 import io
 import locale
 try:
-    from configparser import SafeConfigParser, NoSectionError, NoOptionError
+    from configparser import ConfigParser, NoSectionError, NoOptionError
+    # SafeConfigParser is deprecated in Python 3.2+ (together with "readfp()")
+    if hasattr(ConfigParser, "read_file"):
+        _ConfigParserBase = ConfigParser
+    else:
+        from configparser import SafeConfigParser
+        _ConfigParserBase = SafeConfigParser
 except ImportError:
     from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError
+    _ConfigParserBase = SafeConfigParser
 try:
     from collections import OrderedDict as DictImpl
 except ImportError:
@@ -32,7 +39,7 @@
            "load"]
 
 
-class INIConfigParser(SafeConfigParser):
+class INIConfigParser(_ConfigParserBase):
 
     """A case sensitive config parser that returns all-unicode string
     values.
@@ -40,7 +47,7 @@
     """
 
     def __init__(self, filename, executable=None, encoding=None):
-        SafeConfigParser.__init__(self)
+        _ConfigParserBase.__init__(self)
         if executable is None:
             executable = sys.argv[0]
         if PY2:
@@ -81,10 +88,10 @@
         self.set(None, u("self"), filename)
         self.set(None, u("here"), os.path.dirname(filename))
         self.set(None, u("root"), os.path.dirname(self.executable))
-        if hasattr(SafeConfigParser, "read_file"):
-            SafeConfigParser.read_file(self, fp, source=filename)
+        if hasattr(_ConfigParserBase, "read_file"):
+            _ConfigParserBase.read_file(self, fp, source=filename)
         else:
-            SafeConfigParser.readfp(self, fp, filename=filename)
+            _ConfigParserBase.readfp(self, fp, filename=filename)
         self.filename = filename
         self.root = os.path.dirname(self.executable)