26
26
27
27
py_functools = import_helper .import_fresh_module ('functools' ,
28
28
blocked = ['_functools' ])
29
- c_functools = import_helper .import_fresh_module ('functools' )
29
+ c_functools = import_helper .import_fresh_module ('functools' ,
30
+ fresh = ['_functools' ])
30
31
31
32
decimal = import_helper .import_fresh_module ('decimal' , fresh = ['_decimal' ])
32
33
34
+ _partial_types = [py_functools .partial ]
35
+ if c_functools :
36
+ _partial_types .append (c_functools .partial )
37
+
38
+
33
39
@contextlib .contextmanager
34
40
def replaced_module (name , replacement ):
35
41
original_module = sys .modules [name ]
@@ -201,7 +207,7 @@ def test_repr(self):
201
207
kwargs = {'a' : object (), 'b' : object ()}
202
208
kwargs_reprs = ['a={a!r}, b={b!r}' .format_map (kwargs ),
203
209
'b={b!r}, a={a!r}' .format_map (kwargs )]
204
- if self .partial in ( c_functools . partial , py_functools . partial ) :
210
+ if self .partial in _partial_types :
205
211
name = 'functools.partial'
206
212
else :
207
213
name = self .partial .__name__
@@ -223,7 +229,7 @@ def test_repr(self):
223
229
for kwargs_repr in kwargs_reprs ])
224
230
225
231
def test_recursive_repr (self ):
226
- if self .partial in ( c_functools . partial , py_functools . partial ) :
232
+ if self .partial in _partial_types :
227
233
name = 'functools.partial'
228
234
else :
229
235
name = self .partial .__name__
@@ -250,7 +256,7 @@ def test_recursive_repr(self):
250
256
f .__setstate__ ((capture , (), {}, {}))
251
257
252
258
def test_pickle (self ):
253
- with self .AllowPickle ( ):
259
+ with replaced_module ( 'functools' , self .module ):
254
260
f = self .partial (signature , ['asdf' ], bar = [True ])
255
261
f .attr = []
256
262
for proto in range (pickle .HIGHEST_PROTOCOL + 1 ):
@@ -333,7 +339,7 @@ def test_setstate_subclasses(self):
333
339
self .assertIs (type (r [0 ]), tuple )
334
340
335
341
def test_recursive_pickle (self ):
336
- with self .AllowPickle ( ):
342
+ with replaced_module ( 'functools' , self .module ):
337
343
f = self .partial (capture )
338
344
f .__setstate__ ((f , (), {}, {}))
339
345
try :
@@ -387,14 +393,9 @@ def __getitem__(self, key):
387
393
@unittest .skipUnless (c_functools , 'requires the C _functools module' )
388
394
class TestPartialC (TestPartial , unittest .TestCase ):
389
395
if c_functools :
396
+ module = c_functools
390
397
partial = c_functools .partial
391
398
392
- class AllowPickle :
393
- def __enter__ (self ):
394
- return self
395
- def __exit__ (self , type , value , tb ):
396
- return False
397
-
398
399
def test_attributes_unwritable (self ):
399
400
# attributes should not be writable
400
401
p = self .partial (capture , 1 , 2 , a = 10 , b = 20 )
@@ -437,15 +438,9 @@ def __str__(self):
437
438
438
439
439
440
class TestPartialPy (TestPartial , unittest .TestCase ):
441
+ module = py_functools
440
442
partial = py_functools .partial
441
443
442
- class AllowPickle :
443
- def __init__ (self ):
444
- self ._cm = replaced_module ("functools" , py_functools )
445
- def __enter__ (self ):
446
- return self ._cm .__enter__ ()
447
- def __exit__ (self , type , value , tb ):
448
- return self ._cm .__exit__ (type , value , tb )
449
444
450
445
if c_functools :
451
446
class CPartialSubclass (c_functools .partial ):
@@ -1872,9 +1867,10 @@ def orig(): ...
1872
1867
def py_cached_func (x , y ):
1873
1868
return 3 * x + y
1874
1869
1875
- @c_functools .lru_cache ()
1876
- def c_cached_func (x , y ):
1877
- return 3 * x + y
1870
+ if c_functools :
1871
+ @c_functools .lru_cache ()
1872
+ def c_cached_func (x , y ):
1873
+ return 3 * x + y
1878
1874
1879
1875
1880
1876
class TestLRUPy (TestLRU , unittest .TestCase ):
@@ -1891,18 +1887,20 @@ def cached_staticmeth(x, y):
1891
1887
return 3 * x + y
1892
1888
1893
1889
1890
+ @unittest .skipUnless (c_functools , 'requires the C _functools module' )
1894
1891
class TestLRUC (TestLRU , unittest .TestCase ):
1895
- module = c_functools
1896
- cached_func = c_cached_func ,
1892
+ if c_functools :
1893
+ module = c_functools
1894
+ cached_func = c_cached_func ,
1897
1895
1898
- @module .lru_cache ()
1899
- def cached_meth (self , x , y ):
1900
- return 3 * x + y
1896
+ @module .lru_cache ()
1897
+ def cached_meth (self , x , y ):
1898
+ return 3 * x + y
1901
1899
1902
- @staticmethod
1903
- @module .lru_cache ()
1904
- def cached_staticmeth (x , y ):
1905
- return 3 * x + y
1900
+ @staticmethod
1901
+ @module .lru_cache ()
1902
+ def cached_staticmeth (x , y ):
1903
+ return 3 * x + y
1906
1904
1907
1905
1908
1906
class TestSingleDispatch (unittest .TestCase ):
0 commit comments