cpython: fcfaca024160 (original) (raw)

--- a/Lib/functools.py +++ b/Lib/functools.py @@ -11,7 +11,10 @@ all = ['update_wrapper', 'wraps', 'WRAPPER_ASSIGNMENTS', 'WRAPPER_UPDATES', 'total_ordering', 'cmp_to_key', 'lru_cache', 'reduce', 'partial'] -from _functools import partial, reduce +try:

+except ImportError:

from collections import namedtuple try: from _thread import allocate_lock as Lock @@ -137,6 +140,29 @@ except ImportError: ################################################################################ +### partial() argument application +################################################################################ + +def partial(func, *args, **keywords):

+ +try:

+except ImportError:

+ + +################################################################################

LRU Cache function decorator

################################################################################

--- a/Lib/test/test_functools.py +++ b/Lib/test/test_functools.py @@ -1,4 +1,3 @@ -import functools import collections import sys import unittest @@ -7,17 +6,31 @@ from weakref import proxy import pickle from random import choice -@staticmethod -def PythonPartial(func, *args, **keywords):

+import functools + +original_functools = functools +py_functools = support.import_fresh_module('functools', blocked=['_functools']) +c_functools = support.import_fresh_module('functools', fresh=['_functools']) + +class BaseTest(unittest.TestCase): +

+

+

+ +class BaseTestC(BaseTest):

+ +class BaseTestPy(BaseTest):

+ +PythonPartial = py_functools.partial def capture(*args, **kw): """capture all positional and keyword arguments""" @@ -27,31 +40,32 @@ def signature(part): """ return the signature of a partial object """ return (part.func, part.args, part.keywords, part.dict) -class TestPartial(unittest.TestCase): +class TestPartial(object):

def test_basic_examples(self):

def test_attributes(self):

@@ -60,9 +74,9 @@ class TestPartial(unittest.TestCase): self.fail('partial object allowed dict to be deleted') def test_argument_checking(self):

@@ -73,7 +87,7 @@ class TestPartial(unittest.TestCase): def func(a=10, b=20): return a d = {'a':3}

@@ -82,20 +96,20 @@ class TestPartial(unittest.TestCase): def test_arg_combinations(self): # exercise special code paths for zero args in either partial # object or the caller

def test_kw_combinations(self): # exercise special code paths for no keyword args in # either the partial object or the caller

@@ -104,7 +118,7 @@ class TestPartial(unittest.TestCase): def test_positional(self): # make sure positional arguments are captured correctly for args in [(), (0,), (0,1), (0,1,2), (0,1,2,3)]:

@@ -112,14 +126,14 @@ class TestPartial(unittest.TestCase): def test_keyword(self): # make sure keyword arguments are captured correctly for a in ['a', 0, None, 3.5]:

def test_no_side_effects(self): # make sure there are no side effects that affect subsequent calls

@@ -128,13 +142,13 @@ class TestPartial(unittest.TestCase): def test_error_propagation(self): def f(x, y): x / y

def test_weakref(self):

@@ -142,9 +156,9 @@ class TestPartial(unittest.TestCase): def test_with_bound_and_unbound_methods(self): data = list(map(str, range(10)))

def test_repr(self): @@ -152,49 +166,57 @@ class TestPartial(unittest.TestCase): args_repr = ', '.join(repr(a) for a in args) kwargs = {'a': object(), 'b': object()} kwargs_repr = ', '.join("%s=%r" % (k, v) for k, v in kwargs.items())

def test_pickle(self):

-class PartialSubclass(functools.partial): +class TestPartialC(BaseTestC, TestPartial): pass -class TestPartialSubclass(TestPartial): +class TestPartialPy(BaseTestPy, TestPartial):

-class TestPythonPartial(TestPartial): +class TestPartialCSubclass(BaseTestC, TestPartial):

+

+class TestPartialPySubclass(TestPartialPy):

+

class TestUpdateWrapper(unittest.TestCase): @@ -320,7 +342,7 @@ class TestWraps(TestUpdateWrapper): self.assertEqual(wrapper.qualname, f.qualname) self.assertEqual(wrapper.attr, 'This is also a test')

@@ -441,24 +463,28 @@ class TestReduce(unittest.TestCase): d = {"one": 1, "two": 2, "three": 3} self.assertEqual(self.func(add, d), "".join(d.keys())) -class TestCmpToKey(unittest.TestCase): +class TestCmpToKey(object): def test_cmp_to_key(self): def cmp1(x, y): return (x > y) - (x < y)

+ def cmp2(x, y): return int(x) - int(y)

def test_cmp_to_key_arguments(self): def cmp1(x, y): return (x > y) - (x < y)

@@ -466,10 +492,10 @@ class TestCmpToKey(unittest.TestCase): with self.assertRaises((TypeError, AttributeError)): 1 < key(3) # lhs is not a K object with self.assertRaises(TypeError):

@@ -478,7 +504,7 @@ class TestCmpToKey(unittest.TestCase): def test_bad_cmp(self): def cmp1(x, y): raise ZeroDivisionError

@@ -493,13 +519,13 @@ class TestCmpToKey(unittest.TestCase): def test_obj_field(self): def cmp1(x, y): return (x > y) - (x < y)

def test_sort_int(self): def mycmp(x, y): return y - x

def test_sort_int_str(self): @@ -507,18 +533,24 @@ class TestCmpToKey(unittest.TestCase): x, y = int(x), int(y) return (x > y) - (x < y) values = [5, '3', 7, 2, '0', '1', 4, '10', 1]

def test_hash(self): def mycmp(x, y): return y - x

+class TestCmpToKeyC(BaseTestC, TestCmpToKey):

+ +class TestCmpToKeyPy(BaseTestPy, TestCmpToKey):

+ class TestTotalOrdering(unittest.TestCase): def test_total_ordering_lt(self): @@ -623,7 +655,7 @@ class TestLRU(unittest.TestCase): def test_lru(self): def orig(x, y):

@@ -728,7 +760,7 @@ class TestLRU(unittest.TestCase): # Verify that user_function exceptions get passed through without # creating a hard-to-read chained exception. # http://bugs.python.org/issue13177[](#l2.388)

@@ -741,7 +773,7 @@ class TestLRU(unittest.TestCase): func(15) def test_lru_with_types(self):

@@ -756,14 +788,46 @@ class TestLRU(unittest.TestCase): self.assertEqual(square.cache_info().hits, 4) self.assertEqual(square.cache_info().misses, 4)

+

+ def test_main(verbose=None): test_classes = (

--- a/Misc/ACKS +++ b/Misc/ACKS @@ -1166,6 +1166,7 @@ Tobias Thelen Nicolas M. ThiƩry James Thomas Robin Thomas +Brian Thorne Stephen Thorne Jeremy Thurgood Eric Tiedemann

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -124,6 +124,9 @@ Core and Builtins Library ------- +- Issue #12428: Add a pure Python implementation of functools.partial().