blob: 44090a20d05f2514d657915c0d239d962313452c [file] [log] [blame]
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001From 8a98e4d44a5e59439a4b6bd95368cc362412c995 Mon Sep 17 00:00:00 2001
2From: Alexander Kanavin <alex.kanavin@gmail.com>
3Date: Fri, 24 Mar 2017 18:06:08 +0200
4Subject: [PATCH] Add python 3 compatibility.
5
6Taken from
7http://pkgs.fedoraproject.org/cgit/rpms/python-iniparse.git/tree/python-iniparse-python3-compat.patch
8
9Upstream-Status: Inappropriate [upstream is defunct]
10Signed-off-by: Alexander Kanavin <alex.kanavin@gmail.com>
11---
12 iniparse/__init__.py | 20 ++++++++++----------
13 iniparse/compat.py | 30 ++++++++++++++++--------------
14 iniparse/config.py | 16 ++++++++--------
15 iniparse/configparser.py | 7 +++++++
16 iniparse/ini.py | 20 ++++++++++++--------
17 iniparse/utils.py | 4 ++--
18 tests/__init__.py | 14 +++++++-------
19 tests/test_compat.py | 23 +++++++++++++++--------
20 tests/test_fuzz.py | 18 +++++++++---------
21 tests/test_ini.py | 8 ++++----
22 tests/test_misc.py | 4 ++--
23 tests/test_tidy.py | 2 +-
24 tests/test_unicode.py | 10 +++++-----
25 13 files changed, 98 insertions(+), 78 deletions(-)
26 create mode 100644 iniparse/configparser.py
27
28diff --git a/iniparse/__init__.py b/iniparse/__init__.py
29index 8de756f..7193f92 100644
30--- a/iniparse/__init__.py
31+++ b/iniparse/__init__.py
32@@ -3,17 +3,17 @@
33 # Copyright (c) 2007 Tim Lauridsen <tla@rasmil.dk>
34 # All Rights Reserved. See LICENSE-PSF & LICENSE for details.
35
36-from ini import INIConfig, change_comment_syntax
37-from config import BasicConfig, ConfigNamespace
38-from compat import RawConfigParser, ConfigParser, SafeConfigParser
39-from utils import tidy
40+from .ini import INIConfig, change_comment_syntax
41+from .config import BasicConfig, ConfigNamespace
42+from .compat import RawConfigParser, ConfigParser, SafeConfigParser
43+from .utils import tidy
44
45-from ConfigParser import DuplicateSectionError, \
46- NoSectionError, NoOptionError, \
47- InterpolationMissingOptionError, \
48- InterpolationDepthError, \
49- InterpolationSyntaxError, \
50- DEFAULTSECT, MAX_INTERPOLATION_DEPTH
51+from .configparser import DuplicateSectionError, \
52+ NoSectionError, NoOptionError, \
53+ InterpolationMissingOptionError, \
54+ InterpolationDepthError, \
55+ InterpolationSyntaxError, \
56+ DEFAULTSECT, MAX_INTERPOLATION_DEPTH
57
58 __all__ = [
59 'BasicConfig', 'ConfigNamespace',
60diff --git a/iniparse/compat.py b/iniparse/compat.py
61index db89ed8..f95c25c 100644
62--- a/iniparse/compat.py
63+++ b/iniparse/compat.py
64@@ -12,19 +12,21 @@ The underlying INIConfig object can be accessed as cfg.data
65 """
66
67 import re
68-from ConfigParser import DuplicateSectionError, \
69- NoSectionError, NoOptionError, \
70- InterpolationMissingOptionError, \
71- InterpolationDepthError, \
72- InterpolationSyntaxError, \
73- DEFAULTSECT, MAX_INTERPOLATION_DEPTH
74+from .configparser import DuplicateSectionError, \
75+ NoSectionError, NoOptionError, \
76+ InterpolationMissingOptionError, \
77+ InterpolationDepthError, \
78+ InterpolationSyntaxError, \
79+ DEFAULTSECT, MAX_INTERPOLATION_DEPTH
80
81 # These are imported only for compatiability.
82 # The code below does not reference them directly.
83-from ConfigParser import Error, InterpolationError, \
84- MissingSectionHeaderError, ParsingError
85+from .configparser import Error, InterpolationError, \
86+ MissingSectionHeaderError, ParsingError
87
88-import ini
89+import six
90+
91+from . import ini
92
93 class RawConfigParser(object):
94 def __init__(self, defaults=None, dict_type=dict):
95@@ -56,7 +58,7 @@ class RawConfigParser(object):
96 # The default section is the only one that gets the case-insensitive
97 # treatment - so it is special-cased here.
98 if section.lower() == "default":
99- raise ValueError, 'Invalid section name: %s' % section
100+ raise ValueError('Invalid section name: %s' % section)
101
102 if self.has_section(section):
103 raise DuplicateSectionError(section)
104@@ -88,7 +90,7 @@ class RawConfigParser(object):
105 filename may also be given.
106 """
107 files_read = []
108- if isinstance(filenames, basestring):
109+ if isinstance(filenames, six.string_types):
110 filenames = [filenames]
111 for filename in filenames:
112 try:
113@@ -143,7 +145,7 @@ class RawConfigParser(object):
114 def getboolean(self, section, option):
115 v = self.get(section, option)
116 if v.lower() not in self._boolean_states:
117- raise ValueError, 'Not a boolean: %s' % v
118+ raise ValueError('Not a boolean: %s' % v)
119 return self._boolean_states[v.lower()]
120
121 def has_option(self, section, option):
122@@ -234,7 +236,7 @@ class ConfigParser(RawConfigParser):
123 if "%(" in value:
124 try:
125 value = value % vars
126- except KeyError, e:
127+ except KeyError as e:
128 raise InterpolationMissingOptionError(
129 option, section, rawval, e.args[0])
130 else:
131@@ -283,7 +285,7 @@ class SafeConfigParser(ConfigParser):
132 _badpercent_re = re.compile(r"%[^%]|%$")
133
134 def set(self, section, option, value):
135- if not isinstance(value, basestring):
136+ if not isinstance(value, six.string_types):
137 raise TypeError("option values must be strings")
138 # check for bad percent signs:
139 # first, replace all "good" interpolations
140diff --git a/iniparse/config.py b/iniparse/config.py
141index 5cfa2ea..3b28549 100644
142--- a/iniparse/config.py
143+++ b/iniparse/config.py
144@@ -143,7 +143,7 @@ class BasicConfig(ConfigNamespace):
145
146 >>> n.aaa = 42
147 >>> del n.x
148- >>> print n
149+ >>> print(n)
150 aaa = 42
151 name.first = paramjit
152 name.last = oberoi
153@@ -152,7 +152,7 @@ class BasicConfig(ConfigNamespace):
154
155 >>> isinstance(n.name, ConfigNamespace)
156 True
157- >>> print n.name
158+ >>> print(n.name)
159 first = paramjit
160 last = oberoi
161 >>> sorted(list(n.name))
162@@ -160,7 +160,7 @@ class BasicConfig(ConfigNamespace):
163
164 Finally, values can be read from a file as follows:
165
166- >>> from StringIO import StringIO
167+ >>> from six import StringIO
168 >>> sio = StringIO('''
169 ... # comment
170 ... ui.height = 100
171@@ -171,7 +171,7 @@ class BasicConfig(ConfigNamespace):
172 ... ''')
173 >>> n = BasicConfig()
174 >>> n._readfp(sio)
175- >>> print n
176+ >>> print(n)
177 complexity = medium
178 data.secret.password = goodness=gracious me
179 have_python
180@@ -199,7 +199,7 @@ class BasicConfig(ConfigNamespace):
181
182 def __str__(self, prefix=''):
183 lines = []
184- keys = self._data.keys()
185+ keys = list(self._data.keys())
186 keys.sort()
187 for name in keys:
188 value = self._data[name]
189@@ -258,7 +258,7 @@ def update_config(target, source):
190 >>> n.ui.display_clock = True
191 >>> n.ui.display_qlength = True
192 >>> n.ui.width = 150
193- >>> print n
194+ >>> print(n)
195 playlist.expand_playlist = True
196 ui.display_clock = True
197 ui.display_qlength = True
198@@ -267,7 +267,7 @@ def update_config(target, source):
199 >>> from iniparse import ini
200 >>> i = ini.INIConfig()
201 >>> update_config(i, n)
202- >>> print i
203+ >>> print(i)
204 [playlist]
205 expand_playlist = True
206 <BLANKLINE>
207@@ -277,7 +277,7 @@ def update_config(target, source):
208 width = 150
209
210 """
211- for name in source:
212+ for name in sorted(source):
213 value = source[name]
214 if isinstance(value, ConfigNamespace):
215 if name in target:
216diff --git a/iniparse/configparser.py b/iniparse/configparser.py
217new file mode 100644
218index 0000000..c543d50
219--- /dev/null
220+++ b/iniparse/configparser.py
221@@ -0,0 +1,7 @@
222+try:
223+ from ConfigParser import *
224+ # not all objects get imported with __all__
225+ from ConfigParser import Error, InterpolationMissingOptionError
226+except ImportError:
227+ from configparser import *
228+ from configparser import Error, InterpolationMissingOptionError
229diff --git a/iniparse/ini.py b/iniparse/ini.py
230index 408354d..052d9e9 100644
231--- a/iniparse/ini.py
232+++ b/iniparse/ini.py
233@@ -7,7 +7,7 @@
234
235 Example:
236
237- >>> from StringIO import StringIO
238+ >>> from six import StringIO
239 >>> sio = StringIO('''# configure foo-application
240 ... [foo]
241 ... bar1 = qualia
242@@ -16,14 +16,14 @@ Example:
243 ... special = 1''')
244
245 >>> cfg = INIConfig(sio)
246- >>> print cfg.foo.bar1
247+ >>> print(cfg.foo.bar1)
248 qualia
249- >>> print cfg['foo-ext'].special
250+ >>> print(cfg['foo-ext'].special)
251 1
252 >>> cfg.foo.newopt = 'hi!'
253 >>> cfg.baz.enabled = 0
254
255- >>> print cfg
256+ >>> print(cfg)
257 # configure foo-application
258 [foo]
259 bar1 = qualia
260@@ -42,9 +42,11 @@ Example:
261 # Backward-compatiable with ConfigParser
262
263 import re
264-from ConfigParser import DEFAULTSECT, ParsingError, MissingSectionHeaderError
265+from .configparser import DEFAULTSECT, ParsingError, MissingSectionHeaderError
266
267-import config
268+import six
269+
270+from . import config
271
272 class LineType(object):
273 line = None
274@@ -278,6 +280,8 @@ class LineContainer(object):
275 value = property(get_value, set_value)
276
277 def __str__(self):
278+ for c in self.contents:
279+ pass#print(c.__str__())
280 s = [x.__str__() for x in self.contents]
281 return '\n'.join(s)
282
283@@ -465,7 +469,7 @@ class INIConfig(config.ConfigNamespace):
284 self._sections = {}
285 if defaults is None: defaults = {}
286 self._defaults = INISection(LineContainer(), optionxformsource=self)
287- for name, value in defaults.iteritems():
288+ for name, value in defaults.items():
289 self._defaults[name] = value
290 if fp is not None:
291 self._readfp(fp)
292@@ -551,7 +555,7 @@ class INIConfig(config.ConfigNamespace):
293
294 for line in readline_iterator(fp):
295 # Check for BOM on first line
296- if linecount == 0 and isinstance(line, unicode):
297+ if linecount == 0 and isinstance(line, six.text_type):
298 if line[0] == u'\ufeff':
299 line = line[1:]
300 self._bom = True
301diff --git a/iniparse/utils.py b/iniparse/utils.py
302index 829fc28..f8b773a 100644
303--- a/iniparse/utils.py
304+++ b/iniparse/utils.py
305@@ -1,5 +1,5 @@
306-import compat
307-from ini import LineContainer, EmptyLine
308+from . import compat
309+from .ini import LineContainer, EmptyLine
310
311 def tidy(cfg):
312 """Clean up blank lines.
313diff --git a/tests/__init__.py b/tests/__init__.py
314index f1fa321..88689fb 100644
315--- a/tests/__init__.py
316+++ b/tests/__init__.py
317@@ -1,12 +1,12 @@
318 import unittest, doctest
319
320-import test_ini
321-import test_misc
322-import test_fuzz
323-import test_compat
324-import test_unicode
325-import test_tidy
326-import test_multiprocessing
327+from . import test_ini
328+from . import test_misc
329+from . import test_fuzz
330+from . import test_compat
331+from . import test_unicode
332+from . import test_tidy
333+from . import test_multiprocessing
334 from iniparse import config
335 from iniparse import ini
336
337diff --git a/tests/test_compat.py b/tests/test_compat.py
338index b8da3d5..b6dfb5c 100644
339--- a/tests/test_compat.py
340+++ b/tests/test_compat.py
341@@ -1,9 +1,16 @@
342 from iniparse import compat as ConfigParser
343-import StringIO
344+from six import StringIO
345+try:
346+ import UserDict
347+except ImportError:
348+ import collections as UserDict
349 import unittest
350-import UserDict
351
352-from test import test_support
353+import sys
354+if sys.version_info[0] < 3:
355+ from test import test_support
356+else:
357+ from test import support as test_support
358
359 class SortedDict(UserDict.UserDict):
360 def items(self):
361@@ -35,7 +42,7 @@ class TestCaseBase(unittest.TestCase):
362
363 def fromstring(self, string, defaults=None):
364 cf = self.newconfig(defaults)
365- sio = StringIO.StringIO(string)
366+ sio = StringIO(string)
367 cf.readfp(sio)
368 return cf
369
370@@ -161,7 +168,7 @@ class TestCaseBase(unittest.TestCase):
371 "No Section!\n")
372
373 def parse_error(self, exc, src):
374- sio = StringIO.StringIO(src)
375+ sio = StringIO(src)
376 self.assertRaises(exc, self.cf.readfp, sio)
377
378 def test_query_errors(self):
379@@ -181,7 +188,7 @@ class TestCaseBase(unittest.TestCase):
380 def get_error(self, exc, section, option):
381 try:
382 self.cf.get(section, option)
383- except exc, e:
384+ except exc as e:
385 return e
386 else:
387 self.fail("expected exception type %s.%s"
388@@ -227,7 +234,7 @@ class TestCaseBase(unittest.TestCase):
389 "foo: another very\n"
390 " long line"
391 )
392- output = StringIO.StringIO()
393+ output = StringIO()
394 cf.write(output)
395 self.assertEqual(
396 output.getvalue(),
397@@ -465,7 +472,7 @@ class SortedTestCase(RawConfigParserTestCase):
398 "o1=4\n"
399 "[a]\n"
400 "k=v\n")
401- output = StringIO.StringIO()
402+ output = StringIO()
403 self.cf.write(output)
404 self.assertEquals(output.getvalue(),
405 "[a]\n"
406diff --git a/tests/test_fuzz.py b/tests/test_fuzz.py
407index 5420dcc..b219500 100644
408--- a/tests/test_fuzz.py
409+++ b/tests/test_fuzz.py
410@@ -1,9 +1,10 @@
411 import re
412 import os
413 import random
414+import sys
415 import unittest
416-import ConfigParser
417-from StringIO import StringIO
418+from six import StringIO
419+from six.moves import configparser
420 from iniparse import compat, ini, tidy
421
422 # TODO:
423@@ -96,24 +97,25 @@ class test_fuzz(unittest.TestCase):
424 s = '\n'.join(good_lines)
425 cc = compat.RawConfigParser()
426 cc.readfp(StringIO(s))
427- cc_py = ConfigParser.RawConfigParser()
428+ cc_py = configparser.RawConfigParser()
429 cc_py.readfp(StringIO(s))
430 # compare the two configparsers
431 self.assertEqualConfig(cc_py, cc)
432 # check that tidy does not change semantics
433 tidy(cc)
434- cc_tidy = ConfigParser.RawConfigParser()
435+ cc_tidy = configparser.RawConfigParser()
436 cc_tidy.readfp(StringIO(str(cc.data)))
437 self.assertEqualConfig(cc_py, cc_tidy)
438 except AssertionError:
439 fname = 'fuzz-test-iter-%d.ini' % fuzz_iter
440- print 'Fuzz test failed at iteration', fuzz_iter
441- print 'Writing out failing INI file as', fname
442+ print('Fuzz test failed at iteration', fuzz_iter)
443+ print('Writing out failing INI file as', fname)
444 f = open(fname, 'w')
445 f.write(s)
446 f.close()
447 raise
448
449+ @unittest.skipIf(sys.version_info[0] > 2, 'http://code.google.com/p/iniparse/issues/detail?id=22#c9')
450 def assertEqualConfig(self, c1, c2):
451 self.assertEqualSorted(c1.sections(), c2.sections())
452 self.assertEqualSorted(c1.defaults().items(), c2.defaults().items())
453@@ -123,9 +125,7 @@ class test_fuzz(unittest.TestCase):
454 self.assertEqual(c1.get(sec, opt), c2.get(sec, opt))
455
456 def assertEqualSorted(self, l1, l2):
457- l1.sort()
458- l2.sort()
459- self.assertEqual(l1, l2)
460+ self.assertEqual(sorted(l1), sorted(l2))
461
462 class suite(unittest.TestSuite):
463 def __init__(self):
464diff --git a/tests/test_ini.py b/tests/test_ini.py
465index 6a76edb..07d4f4e 100644
466--- a/tests/test_ini.py
467+++ b/tests/test_ini.py
468@@ -1,5 +1,5 @@
469 import unittest
470-from StringIO import StringIO
471+from six import StringIO
472
473 from iniparse import ini
474 from iniparse import compat
475@@ -196,13 +196,13 @@ but = also me
476 self.assertEqual(p._data.find('section2').find('just').value, 'kidding')
477
478 itr = p._data.finditer('section1')
479- v = itr.next()
480+ v = next(itr)
481 self.assertEqual(v.find('help').value, 'yourself')
482 self.assertEqual(v.find('but').value, 'also me')
483- v = itr.next()
484+ v = next(itr)
485 self.assertEqual(v.find('help').value, 'me')
486 self.assertEqual(v.find('I\'m').value, 'desperate')
487- self.assertRaises(StopIteration, itr.next)
488+ self.assertRaises(StopIteration, next, itr)
489
490 self.assertRaises(KeyError, p._data.find, 'section')
491 self.assertRaises(KeyError, p._data.find('section2').find, 'ahem')
492diff --git a/tests/test_misc.py b/tests/test_misc.py
493index 31cf4da..96ef035 100644
494--- a/tests/test_misc.py
495+++ b/tests/test_misc.py
496@@ -1,9 +1,9 @@
497 import re
498 import unittest
499 import pickle
500-import ConfigParser
501+from six.moves import configparser
502+from six import StringIO
503 from textwrap import dedent
504-from StringIO import StringIO
505 from iniparse import compat, ini
506
507 class CaseSensitiveConfigParser(compat.ConfigParser):
508diff --git a/tests/test_tidy.py b/tests/test_tidy.py
509index 7304747..26b6cde 100644
510--- a/tests/test_tidy.py
511+++ b/tests/test_tidy.py
512@@ -1,6 +1,6 @@
513 import unittest
514 from textwrap import dedent
515-from StringIO import StringIO
516+from six import StringIO
517
518 from iniparse import tidy,INIConfig
519 from iniparse.ini import EmptyLine
520diff --git a/tests/test_unicode.py b/tests/test_unicode.py
521index a56fcab..14d4fbd 100644
522--- a/tests/test_unicode.py
523+++ b/tests/test_unicode.py
524@@ -1,5 +1,5 @@
525 import unittest
526-from StringIO import StringIO
527+import six
528 from iniparse import compat, ini
529
530 class test_unicode(unittest.TestCase):
531@@ -17,14 +17,14 @@ baz = Marc-Andr\202
532 """
533
534 def basic_tests(self, s, strable):
535- f = StringIO(s)
536+ f = six.StringIO(s)
537 i = ini.INIConfig(f)
538- self.assertEqual(unicode(i), s)
539- self.assertEqual(type(i.foo.bar), unicode)
540+ self.assertEqual(six.text_type(i), s)
541+ self.assertEqual(type(i.foo.bar), six.text_type)
542 if strable:
543 self.assertEqual(str(i), str(s))
544 else:
545- self.assertRaises(UnicodeEncodeError, lambda: str(i))
546+ self.assertRaises(UnicodeEncodeError, lambda: six.text_type(i).encode('ascii'))
547 return i
548
549 def test_ascii(self):
550--
5512.11.0
552