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 michael.foord
Recipients michael.foord
Date 2014-04-15.15:54:30
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
stopall does not always stop all patches to single target

What steps will reproduce the problem?

python code to reproduce error

import mock

def myfunc():
   return 'hello'

m = mock.patch('__main__.myfunc').start()
m.return_value = 'firstmock'

m2 = mock.patch('__main__.myfunc').start()
m2.return_value = 'secondmock'


What is the expected output? What do you see instead?
I would expect the output from above to be:

Instead, sometimes it comes out as:

This result is non-deterministic though so it may also come out as expected. Re-run several times to get the error.

This is a result of using a set to store the active patches. Conversion from a set to a list is non-deterministic because the set has no notion of order.

Please provide any additional information below.

This use case may seem strange, but it shows up in large unit tests where a base class sets up a default patch to catch external calls or something similar and then an individual unit test requiring specific mock behavior patches it differently.

I have a patch here that fixes the problem using a list to store the patches to maintain order and call them in reverse order to stop them in the correct order.
Date User Action Args
2014-04-15 15:54:31michael.foordsetrecipients: + michael.foord
2014-04-15 15:54:31michael.foordsetmessageid: <>
2014-04-15 15:54:31michael.foordlinkissue21239 messages
2014-04-15 15:54:30michael.foordcreate