mirror of
https://github.com/python/cpython.git
synced 2025-08-01 07:33:08 +00:00
Patch #1371075: Make ConfigParser accept optional dict type
for ordering, sorting, etc.
This commit is contained in:
parent
046c4d13be
commit
a00bcac003
4 changed files with 68 additions and 11 deletions
|
@ -1,9 +1,29 @@
|
|||
import ConfigParser
|
||||
import StringIO
|
||||
import unittest
|
||||
import UserDict
|
||||
|
||||
from test import test_support
|
||||
|
||||
class SortedDict(UserDict.UserDict):
|
||||
def items(self):
|
||||
result = self.data.items()
|
||||
result.sort()
|
||||
return result
|
||||
|
||||
def keys(self):
|
||||
result = self.data.keys()
|
||||
result.sort()
|
||||
return result
|
||||
|
||||
def values(self):
|
||||
result = self.items()
|
||||
return [i[1] for i in values]
|
||||
|
||||
def iteritems(self): return iter(self.items())
|
||||
def iterkeys(self): return iter(self.keys())
|
||||
__iter__ = iterkeys
|
||||
def itervalues(self): return iter(self.values())
|
||||
|
||||
class TestCaseBase(unittest.TestCase):
|
||||
def newconfig(self, defaults=None):
|
||||
|
@ -414,12 +434,36 @@ class SafeConfigParserTestCase(ConfigParserTestCase):
|
|||
self.assertRaises(TypeError, cf.set, "sect", "option2", 1.0)
|
||||
self.assertRaises(TypeError, cf.set, "sect", "option2", object())
|
||||
|
||||
class SortedTestCase(RawConfigParserTestCase):
|
||||
def newconfig(self, defaults=None):
|
||||
self.cf = self.config_class(defaults=defaults, dict_type=SortedDict)
|
||||
return self.cf
|
||||
|
||||
def test_sorted(self):
|
||||
self.fromstring("[b]\n"
|
||||
"o4=1\n"
|
||||
"o3=2\n"
|
||||
"o2=3\n"
|
||||
"o1=4\n"
|
||||
"[a]\n"
|
||||
"k=v\n")
|
||||
output = StringIO.StringIO()
|
||||
self.cf.write(output)
|
||||
self.assertEquals(output.getvalue(),
|
||||
"[a]\n"
|
||||
"k = v\n\n"
|
||||
"[b]\n"
|
||||
"o1 = 4\n"
|
||||
"o2 = 3\n"
|
||||
"o3 = 2\n"
|
||||
"o4 = 1\n\n")
|
||||
|
||||
def test_main():
|
||||
test_support.run_unittest(
|
||||
ConfigParserTestCase,
|
||||
RawConfigParserTestCase,
|
||||
SafeConfigParserTestCase
|
||||
SafeConfigParserTestCase,
|
||||
SortedTestCase
|
||||
)
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue