Skip to content

gh-90805: make sure test_functools works with and without _functoolsmodule #108644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 28 additions & 30 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@

py_functools = import_helper.import_fresh_module('functools',
blocked=['_functools'])
c_functools = import_helper.import_fresh_module('functools')
c_functools = import_helper.import_fresh_module('functools',
fresh=['_functools'])

decimal = import_helper.import_fresh_module('decimal', fresh=['_decimal'])

_partial_types = [py_functools.partial]
if c_functools:
_partial_types.append(c_functools.partial)


@contextlib.contextmanager
def replaced_module(name, replacement):
original_module = sys.modules[name]
Expand Down Expand Up @@ -201,7 +207,7 @@ def test_repr(self):
kwargs = {'a': object(), 'b': object()}
kwargs_reprs = ['a={a!r}, b={b!r}'.format_map(kwargs),
'b={b!r}, a={a!r}'.format_map(kwargs)]
if self.partial in (c_functools.partial, py_functools.partial):
if self.partial in _partial_types:
name = 'functools.partial'
else:
name = self.partial.__name__
Expand All @@ -223,7 +229,7 @@ def test_repr(self):
for kwargs_repr in kwargs_reprs])

def test_recursive_repr(self):
if self.partial in (c_functools.partial, py_functools.partial):
if self.partial in _partial_types:
name = 'functools.partial'
else:
name = self.partial.__name__
Expand All @@ -250,7 +256,7 @@ def test_recursive_repr(self):
f.__setstate__((capture, (), {}, {}))

def test_pickle(self):
with self.AllowPickle():
with replaced_module('functools', self.module):
f = self.partial(signature, ['asdf'], bar=[True])
f.attr = []
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
Expand Down Expand Up @@ -333,7 +339,7 @@ def test_setstate_subclasses(self):
self.assertIs(type(r[0]), tuple)

def test_recursive_pickle(self):
with self.AllowPickle():
with replaced_module('functools', self.module):
f = self.partial(capture)
f.__setstate__((f, (), {}, {}))
try:
Expand Down Expand Up @@ -387,14 +393,9 @@ def __getitem__(self, key):
@unittest.skipUnless(c_functools, 'requires the C _functools module')
class TestPartialC(TestPartial, unittest.TestCase):
if c_functools:
module = c_functools
partial = c_functools.partial

class AllowPickle:
def __enter__(self):
return self
def __exit__(self, type, value, tb):
return False

def test_attributes_unwritable(self):
# attributes should not be writable
p = self.partial(capture, 1, 2, a=10, b=20)
Expand Down Expand Up @@ -437,15 +438,9 @@ def __str__(self):


class TestPartialPy(TestPartial, unittest.TestCase):
module = py_functools
partial = py_functools.partial

class AllowPickle:
def __init__(self):
self._cm = replaced_module("functools", py_functools)
def __enter__(self):
return self._cm.__enter__()
def __exit__(self, type, value, tb):
return self._cm.__exit__(type, value, tb)

if c_functools:
class CPartialSubclass(c_functools.partial):
Expand Down Expand Up @@ -1872,9 +1867,10 @@ def orig(): ...
def py_cached_func(x, y):
return 3 * x + y

@c_functools.lru_cache()
def c_cached_func(x, y):
return 3 * x + y
if c_functools:
@c_functools.lru_cache()
def c_cached_func(x, y):
return 3 * x + y


class TestLRUPy(TestLRU, unittest.TestCase):
Expand All @@ -1891,18 +1887,20 @@ def cached_staticmeth(x, y):
return 3 * x + y


@unittest.skipUnless(c_functools, 'requires the C _functools module')
class TestLRUC(TestLRU, unittest.TestCase):
module = c_functools
cached_func = c_cached_func,
if c_functools:
module = c_functools
cached_func = c_cached_func,

@module.lru_cache()
def cached_meth(self, x, y):
return 3 * x + y
@module.lru_cache()
def cached_meth(self, x, y):
return 3 * x + y

@staticmethod
@module.lru_cache()
def cached_staticmeth(x, y):
return 3 * x + y
@staticmethod
@module.lru_cache()
def cached_staticmeth(x, y):
return 3 * x + y


class TestSingleDispatch(unittest.TestCase):
Expand Down