Skip to content

bpo-46647: make sure test_functools works with and without _functoolsmodule #31141

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

Closed
wants to merge 1 commit into from
Closed
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
54 changes: 32 additions & 22 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@
import typing
import unittest
import unittest.mock
import os
import weakref
import gc
from weakref import proxy
import contextlib

from test.support import import_helper
from test.support import threading_helper
from test.support.script_helper import assert_python_ok

import functools

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'])

Expand Down Expand Up @@ -202,7 +201,10 @@ 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 (
getattr(c_functools, 'partial', object()),
py_functools.partial,
):
name = 'functools.partial'
else:
name = self.partial.__name__
Expand All @@ -224,7 +226,10 @@ 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 (
getattr(c_functools, 'partial', object()),
py_functools.partial,
):
name = 'functools.partial'
else:
name = self.partial.__name__
Expand Down Expand Up @@ -390,11 +395,13 @@ class TestPartialC(TestPartial, unittest.TestCase):
if c_functools:
partial = c_functools.partial

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

def test_attributes_unwritable(self):
# attributes should not be writable
Expand Down Expand Up @@ -1857,9 +1864,10 @@ def test_staticmethod(x):
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 @@ -1876,18 +1884,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