Skip to content

Commit

Permalink
pythongh-113407: Fix import of unittest.mock when CPython is built wi…
Browse files Browse the repository at this point in the history
…thout docstrings (pythonGH-113408)
  • Loading branch information
serhiy-storchaka authored Dec 24, 2023
1 parent 0d74e96 commit 0c57454
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
16 changes: 11 additions & 5 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2229,8 +2229,11 @@ def __get__(self, obj, _type=None):
return self.create_mock()


_CODE_ATTRS = dir(CodeType)
_CODE_SIG = inspect.signature(partial(CodeType.__init__, None))
try:
_CODE_SIG = inspect.signature(partial(CodeType.__init__, None))
_CODE_ATTRS = dir(CodeType)
except ValueError:
_CODE_SIG = None


class AsyncMockMixin(Base):
Expand All @@ -2250,9 +2253,12 @@ def __init__(self, /, *args, **kwargs):
self.__dict__['_mock_await_count'] = 0
self.__dict__['_mock_await_args'] = None
self.__dict__['_mock_await_args_list'] = _CallList()
code_mock = NonCallableMock(spec_set=_CODE_ATTRS)
code_mock.__dict__["_spec_class"] = CodeType
code_mock.__dict__["_spec_signature"] = _CODE_SIG
if _CODE_SIG:
code_mock = NonCallableMock(spec_set=_CODE_ATTRS)
code_mock.__dict__["_spec_class"] = CodeType
code_mock.__dict__["_spec_signature"] = _CODE_SIG
else:
code_mock = NonCallableMock(spec_set=CodeType)
code_mock.co_flags = (
inspect.CO_COROUTINE
+ inspect.CO_VARARGS
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix import of :mod:`unittest.mock` when CPython is built without docstrings.

0 comments on commit 0c57454

Please sign in to comment.