diff options
author | Éric Araujo <merwok@netwok.org> | 2011-07-29 01:11:09 (GMT) |
---|---|---|
committer | Éric Araujo <merwok@netwok.org> | 2011-07-29 01:11:09 (GMT) |
commit | 2527796a22404d5b8cb0e498a965c6b4a743caac (patch) | |
tree | 5bc07b91dde0085cc09c3336ed740f6817f11fc3 /Lib | |
parent | 1e3a68d36b08cd9d59084a37c8cb6c2d911868ce (diff) | |
parent | cf534817adc49b2562d175fabd3e3992d25063fe (diff) | |
download | cpython-2527796a22404d5b8cb0e498a965c6b4a743caac.zip cpython-2527796a22404d5b8cb0e498a965c6b4a743caac.tar.gz cpython-2527796a22404d5b8cb0e498a965c6b4a743caac.tar.bz2 |
Merge from 3.2 (#10318, #12255, #12043, #12417 and other fixes)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/pstats.py | 4 | ||||
-rw-r--r-- | Lib/test/list_tests.py | 16 | ||||
-rw-r--r-- | Lib/test/test_sort.py | 23 |
3 files changed, 13 insertions, 30 deletions
diff --git a/Lib/pstats.py b/Lib/pstats.py index 8a025cd..3f0add2 100644 --- a/Lib/pstats.py +++ b/Lib/pstats.py @@ -1,13 +1,9 @@ """Class for printing reports on profiled python code.""" -# Class for printing reports on profiled python code. rev 1.0 4/1/94 -# # Written by James Roskind # Based on prior profile module by Sjoerd Mullender... # which was hacked somewhat by: Guido van Rossum -"""Class for profiling Python code.""" - # Copyright Disney Enterprises, Inc. All Rights Reserved. # Licensed to PSF under a Contributor Agreement # diff --git a/Lib/test/list_tests.py b/Lib/test/list_tests.py index 0c656fd..42e118b 100644 --- a/Lib/test/list_tests.py +++ b/Lib/test/list_tests.py @@ -4,17 +4,10 @@ Tests common to list and UserList.UserList import sys import os +from functools import cmp_to_key from test import support, seq_tests -def CmpToKey(mycmp): - 'Convert a cmp= function into a key= function' - class K(object): - def __init__(self, obj): - self.obj = obj - def __lt__(self, other): - return mycmp(self.obj, other.obj) == -1 - return K class CommonTest(seq_tests.CommonTest): @@ -484,7 +477,7 @@ class CommonTest(seq_tests.CommonTest): return 1 else: # a > b return -1 - u.sort(key=CmpToKey(revcmp)) + u.sort(key=cmp_to_key(revcmp)) self.assertEqual(u, self.type2test([2,1,0,-1,-2])) # The following dumps core in unpatched Python 1.5: @@ -497,7 +490,7 @@ class CommonTest(seq_tests.CommonTest): else: # xmod > ymod return 1 z = self.type2test(range(12)) - z.sort(key=CmpToKey(myComparison)) + z.sort(key=cmp_to_key(myComparison)) self.assertRaises(TypeError, z.sort, 2) @@ -509,7 +502,8 @@ class CommonTest(seq_tests.CommonTest): return -1 else: # x > y return 1 - self.assertRaises(ValueError, z.sort, key=CmpToKey(selfmodifyingComparison)) + self.assertRaises(ValueError, z.sort, + key=cmp_to_key(selfmodifyingComparison)) self.assertRaises(TypeError, z.sort, 42, 42, 42, 42) diff --git a/Lib/test/test_sort.py b/Lib/test/test_sort.py index 55503b5..8f6af64 100644 --- a/Lib/test/test_sort.py +++ b/Lib/test/test_sort.py @@ -2,18 +2,11 @@ from test import support import random import sys import unittest +from functools import cmp_to_key verbose = support.verbose nerrors = 0 -def CmpToKey(mycmp): - 'Convert a cmp= function into a key= function' - class K(object): - def __init__(self, obj): - self.obj = obj - def __lt__(self, other): - return mycmp(self.obj, other.obj) == -1 - return K def check(tag, expected, raw, compare=None): global nerrors @@ -23,7 +16,7 @@ def check(tag, expected, raw, compare=None): orig = raw[:] # save input in case of error if compare: - raw.sort(key=CmpToKey(compare)) + raw.sort(key=cmp_to_key(compare)) else: raw.sort() @@ -108,7 +101,7 @@ class TestBase(unittest.TestCase): print(" Checking against an insane comparison function.") print(" If the implementation isn't careful, this may segfault.") s = x[:] - s.sort(key=CmpToKey(lambda a, b: int(random.random() * 3) - 1)) + s.sort(key=cmp_to_key(lambda a, b: int(random.random() * 3) - 1)) check("an insane function left some permutation", x, s) if len(x) >= 2: @@ -165,12 +158,12 @@ class TestBugs(unittest.TestCase): L.pop() return (x > y) - (x < y) L = [1,2] - self.assertRaises(ValueError, L.sort, key=CmpToKey(mutating_cmp)) + self.assertRaises(ValueError, L.sort, key=cmp_to_key(mutating_cmp)) def mutating_cmp(x, y): L.append(3) del L[:] return (x > y) - (x < y) - self.assertRaises(ValueError, L.sort, key=CmpToKey(mutating_cmp)) + self.assertRaises(ValueError, L.sort, key=cmp_to_key(mutating_cmp)) memorywaster = [memorywaster] #============================================================================== @@ -185,7 +178,7 @@ class TestDecorateSortUndecorate(unittest.TestCase): def my_cmp(x, y): xlower, ylower = x.lower(), y.lower() return (xlower > ylower) - (xlower < ylower) - copy.sort(key=CmpToKey(my_cmp)) + copy.sort(key=cmp_to_key(my_cmp)) def test_baddecorator(self): data = 'The quick Brown fox Jumped over The lazy Dog'.split() @@ -261,8 +254,8 @@ class TestDecorateSortUndecorate(unittest.TestCase): def my_cmp_reversed(x, y): x0, y0 = x[0], y[0] return (y0 > x0) - (y0 < x0) - data.sort(key=CmpToKey(my_cmp), reverse=True) - copy1.sort(key=CmpToKey(my_cmp_reversed)) + data.sort(key=cmp_to_key(my_cmp), reverse=True) + copy1.sort(key=cmp_to_key(my_cmp_reversed)) self.assertEqual(data, copy1) copy2.sort(key=lambda x: x[0], reverse=True) self.assertEqual(data, copy2) |