Skip to content

Commit f58b763

Browse files
author
armin.rigo
committed
("Forward-port" of r46506)
Remove various dependencies on dictionary order in the standard library tests, and one (clearly an oversight, potentially critical) in the standard library itself - base64.py. Remaining open issues: * test_extcall is an output test, messy to make robust * tarfile.py has a potential bug here, but I'm not familiar enough with this code. Filed in as SF bug #1496501. * urllib2.HTTPPasswordMgr() returns a random result if there is more than one matching root path. I'm asking python-dev for clarification... git-svn-id: http://svn.python.org/projects/python/trunk@46507 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent bc56d0c commit f58b763

8 files changed

Lines changed: 24 additions & 16 deletions

File tree

Lib/base64.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ def urlsafe_b64decode(s):
126126
8: 'I', 17: 'R', 26: '2',
127127
}
128128

129-
_b32tab = [v for v in _b32alphabet.values()]
129+
_b32tab = _b32alphabet.items()
130+
_b32tab.sort()
131+
_b32tab = [v for k, v in _b32tab]
130132
_b32rev = dict([(v, long(k)) for k, v in _b32alphabet.items()])
131133

132134

Lib/doctest.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,12 +1056,13 @@ class DocTestRunner:
10561056
10571057
>>> tests = DocTestFinder().find(_TestClass)
10581058
>>> runner = DocTestRunner(verbose=False)
1059+
>>> tests.sort(key = lambda test: test.name)
10591060
>>> for test in tests:
1060-
... print runner.run(test)
1061-
(0, 2)
1062-
(0, 1)
1063-
(0, 2)
1064-
(0, 2)
1061+
... print test.name, '->', runner.run(test)
1062+
_TestClass -> (0, 2)
1063+
_TestClass.__init__ -> (0, 2)
1064+
_TestClass.get -> (0, 2)
1065+
_TestClass.square -> (0, 1)
10651066
10661067
The `summarize` method prints a summary of all the test cases that
10671068
have been run by the runner, and returns an aggregated `(f, t)`

Lib/optparse.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,8 +611,10 @@ def _set_attrs(self, attrs):
611611
else:
612612
setattr(self, attr, None)
613613
if attrs:
614+
attrs = attrs.keys()
615+
attrs.sort()
614616
raise OptionError(
615-
"invalid keyword arguments: %s" % ", ".join(attrs.keys()),
617+
"invalid keyword arguments: %s" % ", ".join(attrs),
616618
self)
617619

618620

@@ -1661,6 +1663,7 @@ def _match_abbrev(s, wordmap):
16611663
raise BadOptionError(s)
16621664
else:
16631665
# More than one possible completion: ambiguous prefix.
1666+
possibilities.sort()
16641667
raise AmbiguousOptionError(s, possibilities)
16651668

16661669

Lib/test/test_csv.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,10 @@ def test_sniff(self):
875875
def test_delimiters(self):
876876
sniffer = csv.Sniffer()
877877
dialect = sniffer.sniff(self.sample3)
878-
self.assertEqual(dialect.delimiter, "0")
878+
# given that all three lines in sample3 are equal,
879+
# I think that any character could have been 'guessed' as the
880+
# delimiter, depending on dictionary order
881+
self.assert_(dialect.delimiter in self.sample3)
879882
dialect = sniffer.sniff(self.sample3, delimiters="?,")
880883
self.assertEqual(dialect.delimiter, "?")
881884
dialect = sniffer.sniff(self.sample3, delimiters="/,")

Lib/test/test_itertools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ def gen2(x):
766766
767767
>>> from operator import itemgetter
768768
>>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3)
769-
>>> di = sorted(d.iteritems(), key=itemgetter(1))
769+
>>> di = sorted(sorted(d.iteritems()), key=itemgetter(1))
770770
>>> for k, g in groupby(di, itemgetter(1)):
771771
... print k, map(itemgetter(0), g)
772772
...

Lib/test/test_optparse.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def test_opt_string_long_invalid(self):
230230

231231
def test_attr_invalid(self):
232232
self.assertOptionError(
233-
"option -b: invalid keyword arguments: foo, bar",
233+
"option -b: invalid keyword arguments: bar, foo",
234234
["-b"], {'foo': None, 'bar': None})
235235

236236
def test_action_invalid(self):
@@ -718,9 +718,8 @@ def test_defaults(self):
718718
def test_ambiguous_option(self):
719719
self.parser.add_option("--foz", action="store",
720720
type="string", dest="foo")
721-
possibilities = ", ".join({"--foz": None, "--foo": None}.keys())
722721
self.assertParseFail(["--f=bar"],
723-
"ambiguous option: --f (%s?)" % possibilities)
722+
"ambiguous option: --f (--foo, --foz?)")
724723

725724

726725
def test_short_and_long_option_split(self):
@@ -1537,10 +1536,9 @@ def test_match_abbrev(self):
15371536
def test_match_abbrev_error(self):
15381537
s = "--f"
15391538
wordmap = {"--foz": None, "--foo": None, "--fie": None}
1540-
possibilities = ", ".join(wordmap.keys())
15411539
self.assertRaises(
15421540
_match_abbrev, (s, wordmap), None,
1543-
BadOptionError, "ambiguous option: --f (%s?)" % possibilities)
1541+
BadOptionError, "ambiguous option: --f (--fie, --foo, --foz?)")
15441542

15451543

15461544
class TestParseNumber(BaseTest):

Lib/test/test_urllib2.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ def request(self, method, url, body=None, headers={}):
560560
self.method = method
561561
self.selector = url
562562
self.req_headers += headers.items()
563+
self.req_headers.sort()
563564
if body:
564565
self.data = body
565566
if self.raise_on_endheaders:

Lib/test/test_weakref.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,8 +1053,8 @@ def _reference(self):
10531053
...
10541054
>>> obj = Dict(red=1, green=2, blue=3) # this object is weak referencable
10551055
>>> r = weakref.ref(obj)
1056-
>>> print r()
1057-
{'blue': 3, 'green': 2, 'red': 1}
1056+
>>> print r() is obj
1057+
True
10581058
10591059
>>> import weakref
10601060
>>> class Object:

0 commit comments

Comments
 (0)