summaryrefslogtreecommitdiff
path: root/debian/patches/python-iniparse-python3-compat.patch
blob: 843a0273ac6041640456f1de9cc13b9c824cc758 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
From: Slavek Kabrda <slavek.kabrda@gmail.com>
Subject: Python 3 support using python-six
Origin: https://code.google.com/p/iniparse/issues/detail?id=22
Bug-RedHat: https://bugzilla.redhat.com/show_bug.cgi?id=1010302

--- a/iniparse/compat.py
+++ b/iniparse/compat.py
@@ -12,19 +12,21 @@
 """
 
 import re
-from ConfigParser import DuplicateSectionError,    \
-                  NoSectionError, NoOptionError,   \
-                  InterpolationMissingOptionError, \
-                  InterpolationDepthError,         \
-                  InterpolationSyntaxError,        \
-                  DEFAULTSECT, MAX_INTERPOLATION_DEPTH
+from .configparser import DuplicateSectionError,    \
+                   NoSectionError, NoOptionError,   \
+                   InterpolationMissingOptionError, \
+                   InterpolationDepthError,         \
+                   InterpolationSyntaxError,        \
+                   DEFAULTSECT, MAX_INTERPOLATION_DEPTH
 
 # These are imported only for compatiability.
 # The code below does not reference them directly.
-from ConfigParser import Error, InterpolationError, \
-                  MissingSectionHeaderError, ParsingError
+from .configparser import Error, InterpolationError, \
+                   MissingSectionHeaderError, ParsingError
 
-import ini
+import six
+
+from . import ini
 
 class RawConfigParser(object):
     def __init__(self, defaults=None, dict_type=dict):
@@ -56,7 +58,7 @@
         # The default section is the only one that gets the case-insensitive
         # treatment - so it is special-cased here.
         if section.lower() == "default":
-            raise ValueError, 'Invalid section name: %s' % section
+            raise ValueError('Invalid section name: %s' % section)
 
         if self.has_section(section):
             raise DuplicateSectionError(section)
@@ -88,7 +90,7 @@
         filename may also be given.
         """
         files_read = []
-        if isinstance(filenames, basestring):
+        if isinstance(filenames, six.string_types):
             filenames = [filenames]
         for filename in filenames:
             try:
@@ -143,7 +145,7 @@
     def getboolean(self, section, option):
         v = self.get(section, option)
         if v.lower() not in self._boolean_states:
-            raise ValueError, 'Not a boolean: %s' % v
+            raise ValueError('Not a boolean: %s' % v)
         return self._boolean_states[v.lower()]
 
     def has_option(self, section, option):
@@ -234,7 +236,7 @@
             if "%(" in value:
                 try:
                     value = value % vars
-                except KeyError, e:
+                except KeyError as e:
                     raise InterpolationMissingOptionError(
                         option, section, rawval, e.args[0])
             else:
@@ -283,7 +285,7 @@
     _badpercent_re = re.compile(r"%[^%]|%$")
 
     def set(self, section, option, value):
-        if not isinstance(value, basestring):
+        if not isinstance(value, six.string_types):
             raise TypeError("option values must be strings")
         # check for bad percent signs:
         # first, replace all "good" interpolations
--- /dev/null
+++ b/iniparse/configparser.py
@@ -0,0 +1,7 @@
+try:
+    from ConfigParser import *
+    # not all objects get imported with __all__
+    from ConfigParser import Error, InterpolationMissingOptionError
+except ImportError:
+    from configparser import *
+    from configparser import Error, InterpolationMissingOptionError
--- a/iniparse/config.py
+++ b/iniparse/config.py
@@ -143,7 +143,7 @@
 
     >>> n.aaa = 42
     >>> del n.x
-    >>> print n
+    >>> print(n)
     aaa = 42
     name.first = paramjit
     name.last = oberoi
@@ -152,7 +152,7 @@
 
     >>> isinstance(n.name, ConfigNamespace)
     True
-    >>> print n.name
+    >>> print(n.name)
     first = paramjit
     last = oberoi
     >>> sorted(list(n.name))
@@ -160,7 +160,7 @@
 
     Finally, values can be read from a file as follows:
 
-    >>> from StringIO import StringIO
+    >>> from six import StringIO
     >>> sio = StringIO('''
     ... # comment
     ... ui.height = 100
@@ -171,7 +171,7 @@
     ... ''')
     >>> n = BasicConfig()
     >>> n._readfp(sio)
-    >>> print n
+    >>> print(n)
     complexity = medium
     data.secret.password = goodness=gracious me
     have_python
@@ -199,7 +199,7 @@
 
     def __str__(self, prefix=''):
         lines = []
-        keys = self._data.keys()
+        keys = list(self._data.keys())
         keys.sort()
         for name in keys:
             value = self._data[name]
@@ -258,7 +258,7 @@
     >>> n.ui.display_clock = True
     >>> n.ui.display_qlength = True
     >>> n.ui.width = 150
-    >>> print n
+    >>> print(n)
     playlist.expand_playlist = True
     ui.display_clock = True
     ui.display_qlength = True
@@ -267,7 +267,7 @@
     >>> from iniparse import ini
     >>> i = ini.INIConfig()
     >>> update_config(i, n)
-    >>> print i
+    >>> print(i)
     [playlist]
     expand_playlist = True
     <BLANKLINE>
@@ -277,7 +277,7 @@
     width = 150
 
     """
-    for name in source:
+    for name in sorted(source):
         value = source[name]
         if isinstance(value, ConfigNamespace):
             if name in target:
--- a/iniparse/ini.py
+++ b/iniparse/ini.py
@@ -7,7 +7,7 @@
 
 Example:
 
-    >>> from StringIO import StringIO
+    >>> from six import StringIO
     >>> sio = StringIO('''# configure foo-application
     ... [foo]
     ... bar1 = qualia
@@ -16,14 +16,14 @@
     ... special = 1''')
 
     >>> cfg = INIConfig(sio)
-    >>> print cfg.foo.bar1
+    >>> print(cfg.foo.bar1)
     qualia
-    >>> print cfg['foo-ext'].special
+    >>> print(cfg['foo-ext'].special)
     1
     >>> cfg.foo.newopt = 'hi!'
     >>> cfg.baz.enabled = 0
 
-    >>> print cfg
+    >>> print(cfg)
     # configure foo-application
     [foo]
     bar1 = qualia
@@ -42,9 +42,11 @@
 # Backward-compatiable with ConfigParser
 
 import re
-from ConfigParser import DEFAULTSECT, ParsingError, MissingSectionHeaderError
+from .configparser import DEFAULTSECT, ParsingError, MissingSectionHeaderError
 
-import config
+import six
+
+from . import config
 
 class LineType(object):
     line = None
@@ -278,6 +280,8 @@
     value = property(get_value, set_value)
 
     def __str__(self):
+        for c in self.contents:
+            pass#print(c.__str__())
         s = [x.__str__() for x in self.contents]
         return '\n'.join(s)
 
@@ -465,7 +469,7 @@
         self._sections = {}
         if defaults is None: defaults = {}
         self._defaults = INISection(LineContainer(), optionxformsource=self)
-        for name, value in defaults.iteritems():
+        for name, value in defaults.items():
             self._defaults[name] = value
         if fp is not None:
             self._readfp(fp)
@@ -551,7 +555,7 @@
 
         for line in readline_iterator(fp):
             # Check for BOM on first line
-            if linecount == 0 and isinstance(line, unicode):
+            if linecount == 0 and isinstance(line, six.text_type):
                 if line[0] == u'\ufeff':
                     line = line[1:]
                     self._bom = True
--- a/iniparse/__init__.py
+++ b/iniparse/__init__.py
@@ -3,17 +3,17 @@
 # Copyright (c) 2007 Tim Lauridsen <tla@rasmil.dk>
 # All Rights Reserved.  See LICENSE-PSF & LICENSE for details.
 
-from ini import INIConfig, change_comment_syntax
-from config import BasicConfig, ConfigNamespace
-from compat import RawConfigParser, ConfigParser, SafeConfigParser
-from utils import tidy
+from .ini import INIConfig, change_comment_syntax
+from .config import BasicConfig, ConfigNamespace
+from .compat import RawConfigParser, ConfigParser, SafeConfigParser
+from .utils import tidy
 
-from ConfigParser import DuplicateSectionError,    \
-                  NoSectionError, NoOptionError,   \
-                  InterpolationMissingOptionError, \
-                  InterpolationDepthError,         \
-                  InterpolationSyntaxError,        \
-                  DEFAULTSECT, MAX_INTERPOLATION_DEPTH
+from .configparser import DuplicateSectionError,    \
+                   NoSectionError, NoOptionError,   \
+                   InterpolationMissingOptionError, \
+                   InterpolationDepthError,         \
+                   InterpolationSyntaxError,        \
+                   DEFAULTSECT, MAX_INTERPOLATION_DEPTH
 
 __all__ = [
     'BasicConfig', 'ConfigNamespace',
--- a/iniparse/utils.py
+++ b/iniparse/utils.py
@@ -1,5 +1,5 @@
-import compat
-from ini import LineContainer, EmptyLine
+from . import compat
+from .ini import LineContainer, EmptyLine
 
 def tidy(cfg):
     """Clean up blank lines.
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -1,12 +1,12 @@
 import unittest, doctest
 
-import test_ini
-import test_misc
-import test_fuzz
-import test_compat
-import test_unicode
-import test_tidy
-import test_multiprocessing
+from . import test_ini
+from . import test_misc
+from . import test_fuzz
+from . import test_compat
+from . import test_unicode
+from . import test_tidy
+from . import test_multiprocessing
 from iniparse import config
 from iniparse import ini
 
--- a/tests/test_compat.py
+++ b/tests/test_compat.py
@@ -1,9 +1,16 @@
 from iniparse import compat as ConfigParser
-import StringIO
+from six import StringIO
+try:
+    import UserDict
+except ImportError:
+    import collections as UserDict
 import unittest
-import UserDict
 
-from test import test_support
+import sys
+if sys.version_info[0] < 3:
+    from test import test_support
+else:
+    from test import support as test_support
 
 class SortedDict(UserDict.UserDict):
     def items(self):
@@ -35,7 +42,7 @@
 
     def fromstring(self, string, defaults=None):
         cf = self.newconfig(defaults)
-        sio = StringIO.StringIO(string)
+        sio = StringIO(string)
         cf.readfp(sio)
         return cf
 
@@ -161,7 +168,7 @@
                          "No Section!\n")
 
     def parse_error(self, exc, src):
-        sio = StringIO.StringIO(src)
+        sio = StringIO(src)
         self.assertRaises(exc, self.cf.readfp, sio)
 
     def test_query_errors(self):
@@ -181,7 +188,7 @@
     def get_error(self, exc, section, option):
         try:
             self.cf.get(section, option)
-        except exc, e:
+        except exc as e:
             return e
         else:
             self.fail("expected exception type %s.%s"
@@ -227,7 +234,7 @@
             "foo: another very\n"
             " long line"
             )
-        output = StringIO.StringIO()
+        output = StringIO()
         cf.write(output)
         self.assertEqual(
             output.getvalue(),
@@ -465,7 +472,7 @@
                         "o1=4\n"
                         "[a]\n"
                         "k=v\n")
-        output = StringIO.StringIO()
+        output = StringIO()
         self.cf.write(output)
         self.assertEquals(output.getvalue(),
                           "[a]\n"
--- a/tests/test_fuzz.py
+++ b/tests/test_fuzz.py
@@ -1,9 +1,10 @@
 import re
 import os
 import random
+import sys
 import unittest
-import ConfigParser
-from StringIO import StringIO
+from six import StringIO
+from six.moves import configparser
 from iniparse import compat, ini, tidy
 
 # TODO:
@@ -96,24 +97,25 @@
                 s = '\n'.join(good_lines)
                 cc = compat.RawConfigParser()
                 cc.readfp(StringIO(s))
-                cc_py = ConfigParser.RawConfigParser()
+                cc_py = configparser.RawConfigParser()
                 cc_py.readfp(StringIO(s))
                 # compare the two configparsers
                 self.assertEqualConfig(cc_py, cc)
                 # check that tidy does not change semantics
                 tidy(cc)
-                cc_tidy = ConfigParser.RawConfigParser()
+                cc_tidy = configparser.RawConfigParser()
                 cc_tidy.readfp(StringIO(str(cc.data)))
                 self.assertEqualConfig(cc_py, cc_tidy)
             except AssertionError:
                 fname = 'fuzz-test-iter-%d.ini' % fuzz_iter
-                print 'Fuzz test failed at iteration', fuzz_iter
-                print 'Writing out failing INI file as', fname
+                print('Fuzz test failed at iteration', fuzz_iter)
+                print('Writing out failing INI file as', fname)
                 f = open(fname, 'w')
                 f.write(s)
                 f.close()
                 raise
 
+    @unittest.skipIf(sys.version_info[0] > 2, 'http://code.google.com/p/iniparse/issues/detail?id=22#c9')
     def assertEqualConfig(self, c1, c2):
         self.assertEqualSorted(c1.sections(), c2.sections())
         self.assertEqualSorted(c1.defaults().items(), c2.defaults().items())
@@ -123,9 +125,7 @@
                 self.assertEqual(c1.get(sec, opt), c2.get(sec, opt))
 
     def assertEqualSorted(self, l1, l2):
-        l1.sort()
-        l2.sort()
-        self.assertEqual(l1, l2)
+        self.assertEqual(sorted(l1), sorted(l2))
 
 class suite(unittest.TestSuite):
     def __init__(self):
--- a/tests/test_ini.py
+++ b/tests/test_ini.py
@@ -1,5 +1,5 @@
 import unittest
-from StringIO import StringIO
+from six import StringIO
 
 from iniparse import ini
 from iniparse import compat
@@ -196,13 +196,13 @@
         self.assertEqual(p._data.find('section2').find('just').value, 'kidding')
 
         itr = p._data.finditer('section1')
-        v = itr.next()
+        v = next(itr)
         self.assertEqual(v.find('help').value, 'yourself')
         self.assertEqual(v.find('but').value, 'also me')
-        v = itr.next()
+        v = next(itr)
         self.assertEqual(v.find('help').value, 'me')
         self.assertEqual(v.find('I\'m').value, 'desperate')
-        self.assertRaises(StopIteration, itr.next)
+        self.assertRaises(StopIteration, next, itr)
 
         self.assertRaises(KeyError, p._data.find, 'section')
         self.assertRaises(KeyError, p._data.find('section2').find, 'ahem')
--- a/tests/test_misc.py
+++ b/tests/test_misc.py
@@ -1,9 +1,9 @@
 import re
 import unittest
 import pickle
-import ConfigParser
+from six.moves import configparser
+from six import StringIO
 from textwrap import dedent
-from StringIO import StringIO
 from iniparse import compat, ini
 
 class CaseSensitiveConfigParser(compat.ConfigParser):
--- a/tests/test_tidy.py
+++ b/tests/test_tidy.py
@@ -1,6 +1,6 @@
 import unittest
 from textwrap import dedent
-from StringIO import StringIO
+from six import StringIO
 
 from iniparse import tidy,INIConfig
 from iniparse.ini import  EmptyLine
--- a/tests/test_unicode.py
+++ b/tests/test_unicode.py
@@ -1,5 +1,5 @@
 import unittest
-from StringIO import StringIO
+import six
 from iniparse import compat, ini
 
 class test_unicode(unittest.TestCase):
@@ -17,14 +17,14 @@
     """
 
     def basic_tests(self, s, strable):
-        f = StringIO(s)
+        f = six.StringIO(s)
         i = ini.INIConfig(f)
-        self.assertEqual(unicode(i), s)
-        self.assertEqual(type(i.foo.bar), unicode)
+        self.assertEqual(six.text_type(i), s)
+        self.assertEqual(type(i.foo.bar), six.text_type)
         if strable:
             self.assertEqual(str(i), str(s))
         else:
-            self.assertRaises(UnicodeEncodeError, lambda: str(i))
+            self.assertRaises(UnicodeEncodeError, lambda: six.text_type(i).encode('ascii'))
         return i
 
     def test_ascii(self):
--- /dev/null
+++ b/cfgparser.1
@@ -0,0 +1,2 @@
+[Foo Bar]
+foo = newbar