[issue21600] mock.patch.stopall doesn't work with patch.dict to sys.modules

2014-05-28 Thread fumihiko kakuma

New submission from fumihiko kakuma:

It seems that stopall doesn't work when do start patch.dict to sys.modules.
I show sample scripts the following.

Using stopall test case seems to always refer the first mock foo object.
But using stop it refers a new mock foo object.

$ cat test_sampmod.py
import foo

def myfunc():
print "myfunc foo=%s" % foo
return foo
$ cat test_samp.py
import mock
import sys
import unittest


class SampTestCase(unittest.TestCase):
def setUp(self):
self.foo_mod = mock.Mock()
self.m = mock.patch.dict('sys.modules', {'foo': self.foo_mod})
self.p = self.m.start()
print "foo_mod=%s" % self.foo_mod
__import__('test_sampmod')
self.test_sampmod = sys.modules['test_sampmod']

def tearDown(self):
if len(sys.argv) > 1:
self.m.stop()
print ">>> stop patch"
else:
mock.patch.stopall()
print ">>> stopall patch"

def test_samp1(self):
self.assertEqual(self.foo_mod, self.test_sampmod.myfunc())

def test_samp2(self):
self.assertEqual(self.foo_mod, self.test_sampmod.myfunc())

def test_samp3(self):
self.assertEqual(self.foo_mod, self.test_sampmod.myfunc())

if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(SampTestCase))
unittest.TextTestRunner(verbosity=2).run(suite)
$ python test_samp.py stop
test_samp1 (__main__.SampTestCase) ... foo_mod=
myfunc foo=
>>> stop patch
ok
test_samp2 (__main__.SampTestCase) ... foo_mod=
myfunc foo=
>>> stop patch
ok
test_samp3 (__main__.SampTestCase) ... foo_mod=
myfunc foo=
>>> stop patch
ok

--
Ran 3 tests in 0.004s

OK
$ python test_samp.py
test_samp1 (__main__.SampTestCase) ... foo_mod=
myfunc foo=
>>> stopall patch
ok
test_samp2 (__main__.SampTestCase) ... foo_mod=
myfunc foo=
FAIL
>>> stopall patch
test_samp3 (__main__.SampTestCase) ... foo_mod=
myfunc foo=
FAIL
>>> stopall patch

==
FAIL: test_samp2 (__main__.SampTestCase)
--
Traceback (most recent call last):
  File "test_samp.py", line 27, in test_samp2
self.assertEqual(self.foo_mod, self.test_sampmod.myfunc())
AssertionError:  != 

==
FAIL: test_samp3 (__main__.SampTestCase)
--
Traceback (most recent call last):
  File "test_samp.py", line 30, in test_samp3
self.assertEqual(self.foo_mod, self.test_sampmod.myfunc())
AssertionError:  != 

--
Ran 3 tests in 0.003s

FAILED (failures=2)
$

--
components: Tests
messages: 219331
nosy: kakuma
priority: normal
severity: normal
status: open
title: mock.patch.stopall doesn't work with patch.dict to sys.modules
type: behavior
versions: Python 2.7

___
Python tracker 
<http://bugs.python.org/issue21600>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21600] mock.patch.stopall doesn't work with patch.dict to sys.modules

2014-06-02 Thread fumihiko kakuma

fumihiko kakuma added the comment:

Checking mock.py(version 1.0.1) it seems that patch.stopall does not support 
patch.dict. Does it have any problem to support ptch.dict by stopall.
I made the attached patch file for this. It seems to work well. How about this?

But I don't know why sys.modules refers the first mock object. 

$ python test_samp.py
test_samp1 (__main__.SampTestCase) ... foo_mod=
myfunc foo=
>>> stopall patch
ok
test_samp2 (__main__.SampTestCase) ... foo_mod=
myfunc foo=
>>> stopall patch
ok
test_samp3 (__main__.SampTestCase) ... foo_mod=
myfunc foo=
>>> stopall patch
ok

--
Ran 3 tests in 0.001s

OK
$

--
keywords: +patch
Added file: http://bugs.python.org/file35445/add_stopall_patch_dict.patch

___
Python tracker 
<http://bugs.python.org/issue21600>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21600] mock.patch.stopall doesn't work with patch.dict to sys.modules

2014-06-06 Thread fumihiko kakuma

fumihiko kakuma added the comment:

Thank you for your reply.
Yes, you are right. The patch was too slapdash. I re-created it and added unit 
tests.

--
Added file: http://bugs.python.org/file35499/support_patch_dict_by_stopall.diff

___
Python tracker 
<http://bugs.python.org/issue21600>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21600] mock.patch.stopall doesn't work with patch.dict to sys.modules

2014-06-07 Thread fumihiko kakuma

fumihiko kakuma added the comment:

Hi michael,
Certainly, thank you for your many advices. I attached the new patch file.

--
Added file: http://bugs.python.org/file35513/support_patch_dict_by_stopall.diff

___
Python tracker 
<http://bugs.python.org/issue21600>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21600] mock.patch.stopall doesn't work with patch.dict to sys.modules

2014-06-08 Thread fumihiko kakuma

fumihiko kakuma added the comment:

Thank you in advance.

--

___
Python tracker 
<http://bugs.python.org/issue21600>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com