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