Skip to content
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

Fix RuntimeError if user code calls patcher.stopall #138

Merged
merged 1 commit into from
Mar 25, 2019

Conversation

nicoddemus
Copy link
Member

Fix #137

@blueyed
Copy link
Contributor

blueyed commented Mar 25, 2019

Hmm, from #137 (comment) it sounds like it would be better if pytest-mock's mocks could be excluded? But that's maybe a) not trivial, and b) not good in general - likely you should not call stopall if you do not mean it..

@nicoddemus
Copy link
Member Author

likely you should not call stopall if you do not mean it..

Agree, unfortunately there's no public API to check for that; __exit__ uses _is_started(self) to raise that exception, which I would use if it was public. I catching the exception and checking the actual message is safe, but brittle, as the message might change in future versions, so I think this is OK for now.

if text_type(e) == "stop called on unstarted patcher":
pass
else:
raise
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks heaps for this, but I'm concerned that this might obfuscate other errors.

What would happen if some test called .stopall(), then some other test which relied on pytest-mock's internal mocks was called. Would it be clear what was going on?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be a separate issue. It's possible to get into this situation as-is.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review!

I don't think so, as we are not catching any RuntimeError, we only ignore the very specific message... as I commented above, I think this is safe, but might break in future mock releases if they ever change the message. We should also switch to a public API that allows us to check if a patcher has already been stopped, if one is ever introduced.

@coveralls
Copy link

Coverage Status

Coverage remained the same at ?% when pulling 24bce30 on nicoddemus:release-1.10.2 into 35ade1c on pytest-dev:master.

@nicoddemus nicoddemus merged commit 540c17b into pytest-dev:master Mar 25, 2019
@nicoddemus nicoddemus deleted the release-1.10.2 branch March 25, 2019 11:56
@tirkarthi
Copy link
Contributor

Just to add to this the RuntimeError was removed in CPython and mock 3.0.0 due to similar concerns raised in #137 . Upstream issue : https://bugs.python.org/issue36366 .

@nicoddemus
Copy link
Member Author

Thanks @tirkarthi for the pointer, created #147 to track it. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

patch.stopall() breaks pytest-mock
5 participants