Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

unittest.mock.create_autospec() can crash when accessing properties from objects #98301

Copy link
Copy link
@joaoe

Description

@joaoe
Issue body actions

Bug report

This crashes. It should not.

from unittest.mock import create_autospec

class X:
    @property
    def myprop(self):
        raise Exception("some bug")

create_autospec(X())

Now you could tell for me to fix X. The problem though is that in my project the problem is much more complex. X is some other class that is being autospeced and the getter touches some mock objects it does not like.

Your environment

  • CPython versions tested on: 3.10
  • Operating system and architecture: Windows

Fix

--- a\unittest\mock.py
+++ b\unittest\mock.py
         for attr in dir(spec):
-            if iscoroutinefunction(getattr(spec, attr, None)):
-                _spec_asyncs.append(attr)
+            try::
+                value = getattr(spec, attr, None)
+            except:
+                pass
+            else:
+                if iscoroutinefunction(value):
+                    _spec_asyncs.append(attr)
Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.