Skip to content

Commit c105947

Browse files
serhiy-storchakaaisk
authored andcommitted
pythongh-113407: Fix import of unittest.mock when CPython is built without docstrings (pythonGH-113408)
1 parent 4c62e3f commit c105947

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
@@ -2229,8 +2229,11 @@ def __get__(self, obj, _type=None):
22292229
return self.create_mock()
22302230

22312231

2232-
_CODE_ATTRS = dir(CodeType)
2233-
_CODE_SIG = inspect.signature(partial(CodeType.__init__, None))
2232+
try:
2233+
_CODE_SIG = inspect.signature(partial(CodeType.__init__, None))
2234+
_CODE_ATTRS = dir(CodeType)
2235+
except ValueError:
2236+
_CODE_SIG = None
22342237

22352238

22362239
class AsyncMockMixin(Base):
@@ -2250,9 +2253,12 @@ def __init__(self, /, *args, **kwargs):
22502253
self.__dict__['_mock_await_count'] = 0
22512254
self.__dict__['_mock_await_args'] = None
22522255
self.__dict__['_mock_await_args_list'] = _CallList()
2253-
code_mock = NonCallableMock(spec_set=_CODE_ATTRS)
2254-
code_mock.__dict__["_spec_class"] = CodeType
2255-
code_mock.__dict__["_spec_signature"] = _CODE_SIG
2256+
if _CODE_SIG:
2257+
code_mock = NonCallableMock(spec_set=_CODE_ATTRS)
2258+
code_mock.__dict__["_spec_class"] = CodeType
2259+
code_mock.__dict__["_spec_signature"] = _CODE_SIG
2260+
else:
2261+
code_mock = NonCallableMock(spec_set=CodeType)
22562262
code_mock.co_flags = (
22572263
inspect.CO_COROUTINE
22582264
+ 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)