This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author xtreak
Recipients cjw296, mariocj89, matrixise, michael.foord, sfreilich, xtreak
Date 2019-03-19.16:32:39
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
When mock.patch is creates a patch object and patch.start calls __enter__ that sets is_local. On stop __exit__ is called where a check is done is to make sure is_local attribute is present and then cleanup is done along with deleting calling del self.is_local so calling stop second time causes the attribute check to fail. There is no specific reason I could find with git history. 

It seems that calling patch.stop without patch.start makes cleanup to happen on unpatched objects and raises errors which I hope is avoided by always setting is_local on start and removing it on stop like a flag. That being said I am not sure why a early return couldn't be made when is_local is absent instead of proceeding with cleanup logic or raising a runtime error. I see no tests failing on early return except a test where RuntimeError is intentionally tested by calling stop on unstarted patch. I have added mock module devs for some context.

A sample script would be as below : 

from unittest import mock

class Foo:
    bar = None

patch = mock.patch.object(Foo, 'bar', 'x')
Date User Action Args
2019-03-19 16:32:40xtreaksetrecipients: + xtreak, cjw296, michael.foord, matrixise, mariocj89, sfreilich
2019-03-19 16:32:39xtreaksetmessageid: <>
2019-03-19 16:32:39xtreaklinkissue36366 messages
2019-03-19 16:32:39xtreakcreate