Skip to content

Commit bd306a9

Browse files
[3.12] gh-113407: Fix import of unittest.mock when CPython is built without docstrings (GH-113408) (GH-113454)
(cherry picked from commit 0c57454) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent fbfb90c commit bd306a9

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

Lib/unittest/mock.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -2199,8 +2199,11 @@ def __get__(self, obj, _type=None):
21992199
return self.create_mock()
22002200

22012201

2202-
_CODE_ATTRS = dir(CodeType)
2203-
_CODE_SIG = inspect.signature(partial(CodeType.__init__, None))
2202+
try:
2203+
_CODE_SIG = inspect.signature(partial(CodeType.__init__, None))
2204+
_CODE_ATTRS = dir(CodeType)
2205+
except ValueError:
2206+
_CODE_SIG = None
22042207

22052208

22062209
class AsyncMockMixin(Base):
@@ -2220,9 +2223,12 @@ def __init__(self, /, *args, **kwargs):
22202223
self.__dict__['_mock_await_count'] = 0
22212224
self.__dict__['_mock_await_args'] = None
22222225
self.__dict__['_mock_await_args_list'] = _CallList()
2223-
code_mock = NonCallableMock(spec_set=_CODE_ATTRS)
2224-
code_mock.__dict__["_spec_class"] = CodeType
2225-
code_mock.__dict__["_spec_signature"] = _CODE_SIG
2226+
if _CODE_SIG:
2227+
code_mock = NonCallableMock(spec_set=_CODE_ATTRS)
2228+
code_mock.__dict__["_spec_class"] = CodeType
2229+
code_mock.__dict__["_spec_signature"] = _CODE_SIG
2230+
else:
2231+
code_mock = NonCallableMock(spec_set=CodeType)
22262232
code_mock.co_flags = (
22272233
inspect.CO_COROUTINE
22282234
+ inspect.CO_VARARGS
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix import of :mod:`unittest.mock` when CPython is built without docstrings.

0 commit comments

Comments
 (0)