13
13
import typing
14
14
import unittest
15
15
import unittest .mock
16
- import os
17
16
import weakref
18
17
import gc
19
18
from weakref import proxy
20
19
import contextlib
21
20
22
21
from test .support import import_helper
23
22
from test .support import threading_helper
24
- from test .support .script_helper import assert_python_ok
25
23
26
24
import functools
27
25
28
26
py_functools = import_helper .import_fresh_module ('functools' ,
29
27
blocked = ['_functools' ])
30
- c_functools = import_helper .import_fresh_module ('functools' )
28
+ c_functools = import_helper .import_fresh_module ('functools' ,
29
+ fresh = ['_functools' ])
31
30
32
31
decimal = import_helper .import_fresh_module ('decimal' , fresh = ['_decimal' ])
33
32
@@ -202,7 +201,10 @@ def test_repr(self):
202
201
kwargs = {'a' : object (), 'b' : object ()}
203
202
kwargs_reprs = ['a={a!r}, b={b!r}' .format_map (kwargs ),
204
203
'b={b!r}, a={a!r}' .format_map (kwargs )]
205
- if self .partial in (c_functools .partial , py_functools .partial ):
204
+ if self .partial in (
205
+ getattr (c_functools , 'partial' , object ()),
206
+ py_functools .partial ,
207
+ ):
206
208
name = 'functools.partial'
207
209
else :
208
210
name = self .partial .__name__
@@ -224,7 +226,10 @@ def test_repr(self):
224
226
for kwargs_repr in kwargs_reprs ])
225
227
226
228
def test_recursive_repr (self ):
227
- if self .partial in (c_functools .partial , py_functools .partial ):
229
+ if self .partial in (
230
+ getattr (c_functools , 'partial' , object ()),
231
+ py_functools .partial ,
232
+ ):
228
233
name = 'functools.partial'
229
234
else :
230
235
name = self .partial .__name__
@@ -390,11 +395,13 @@ class TestPartialC(TestPartial, unittest.TestCase):
390
395
if c_functools :
391
396
partial = c_functools .partial
392
397
393
- class AllowPickle :
394
- def __enter__ (self ):
395
- return self
396
- def __exit__ (self , type , value , tb ):
397
- return False
398
+ class AllowPickle :
399
+ def __init__ (self ):
400
+ self ._cm = replaced_module ("functools" , c_functools )
401
+ def __enter__ (self ):
402
+ return self ._cm .__enter__ ()
403
+ def __exit__ (self , type , value , tb ):
404
+ return self ._cm .__exit__ (type , value , tb )
398
405
399
406
def test_attributes_unwritable (self ):
400
407
# attributes should not be writable
@@ -1857,9 +1864,10 @@ def test_staticmethod(x):
1857
1864
def py_cached_func (x , y ):
1858
1865
return 3 * x + y
1859
1866
1860
- @c_functools .lru_cache ()
1861
- def c_cached_func (x , y ):
1862
- return 3 * x + y
1867
+ if c_functools :
1868
+ @c_functools .lru_cache ()
1869
+ def c_cached_func (x , y ):
1870
+ return 3 * x + y
1863
1871
1864
1872
1865
1873
class TestLRUPy (TestLRU , unittest .TestCase ):
@@ -1876,18 +1884,20 @@ def cached_staticmeth(x, y):
1876
1884
return 3 * x + y
1877
1885
1878
1886
1887
+ @unittest .skipUnless (c_functools , 'requires the C _functools module' )
1879
1888
class TestLRUC (TestLRU , unittest .TestCase ):
1880
- module = c_functools
1881
- cached_func = c_cached_func ,
1889
+ if c_functools :
1890
+ module = c_functools
1891
+ cached_func = c_cached_func ,
1882
1892
1883
- @module .lru_cache ()
1884
- def cached_meth (self , x , y ):
1885
- return 3 * x + y
1893
+ @module .lru_cache ()
1894
+ def cached_meth (self , x , y ):
1895
+ return 3 * x + y
1886
1896
1887
- @staticmethod
1888
- @module .lru_cache ()
1889
- def cached_staticmeth (x , y ):
1890
- return 3 * x + y
1897
+ @staticmethod
1898
+ @module .lru_cache ()
1899
+ def cached_staticmeth (x , y ):
1900
+ return 3 * x + y
1891
1901
1892
1902
1893
1903
class TestSingleDispatch (unittest .TestCase ):
0 commit comments