1313import typing
1414import unittest
1515import unittest .mock
16- import os
1716import weakref
1817import gc
1918from weakref import proxy
2019import contextlib
2120
2221from test .support import import_helper
2322from test .support import threading_helper
24- from test .support .script_helper import assert_python_ok
2523
2624import functools
2725
2826py_functools = import_helper .import_fresh_module ('functools' ,
2927 blocked = ['_functools' ])
30- c_functools = import_helper .import_fresh_module ('functools' )
28+ c_functools = import_helper .import_fresh_module ('functools' ,
29+ fresh = ['_functools' ])
3130
3231decimal = import_helper .import_fresh_module ('decimal' , fresh = ['_decimal' ])
3332
@@ -202,7 +201,10 @@ def test_repr(self):
202201 kwargs = {'a' : object (), 'b' : object ()}
203202 kwargs_reprs = ['a={a!r}, b={b!r}' .format_map (kwargs ),
204203 '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+ ):
206208 name = 'functools.partial'
207209 else :
208210 name = self .partial .__name__
@@ -224,7 +226,10 @@ def test_repr(self):
224226 for kwargs_repr in kwargs_reprs ])
225227
226228 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+ ):
228233 name = 'functools.partial'
229234 else :
230235 name = self .partial .__name__
@@ -390,11 +395,13 @@ class TestPartialC(TestPartial, unittest.TestCase):
390395 if c_functools :
391396 partial = c_functools .partial
392397
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 )
398405
399406 def test_attributes_unwritable (self ):
400407 # attributes should not be writable
@@ -1857,9 +1864,10 @@ def test_staticmethod(x):
18571864def py_cached_func (x , y ):
18581865 return 3 * x + y
18591866
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
18631871
18641872
18651873class TestLRUPy (TestLRU , unittest .TestCase ):
@@ -1876,18 +1884,20 @@ def cached_staticmeth(x, y):
18761884 return 3 * x + y
18771885
18781886
1887+ @unittest .skipUnless (c_functools , 'requires the C _functools module' )
18791888class 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 ,
18821892
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
18861896
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
18911901
18921902
18931903class TestSingleDispatch (unittest .TestCase ):
0 commit comments