derwiki-adroll / mock

Automatically exported from code.google.com/p/mock
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

patch with autospec=True fails for methods #100

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I am testing the latest alpha release 0.8.0.alpha1.

I have the following code:

with patch('module.Class', autospec=True):
    do_something()

and I get the following error:

  File "/home/adi/chevah/server/devel/build-ubuntu-x86/lib/python2.5/mock.py", line 592, in __getattr__
    result.parent, result.name, result.instance
  File "/home/adi/chevah/server/devel/build-ubuntu-x86/lib/python2.5/mock.py", line 1366, in create_autospec
    _instance=True)
  File "/home/adi/chevah/server/devel/build-ubuntu-x86/lib/python2.5/mock.py", line 1402, in create_autospec
    _check_signature(original, new, skipfirst=skipfirst)
  File "/home/adi/chevah/server/devel/build-ubuntu-x86/lib/python2.5/mock.py", line 181, in _check_signature
    checksig = eval(src, {})
SyntaxError: duplicate argument 'self' in function definition (<string>, line 0)

----

To fix this error I have chenged the mock.py so that _check_signature will not 
use self if signature starts with self.

Not sure if this is the right way to do it.

Here is the code.
def _check_signature(func, mock, skipfirst):
    if not _callable(func):
        return

    result = _getsignature2(func, skipfirst)
    if result is None:
        return
    signature, func = result

    if signature.startswith('self'):
        src = "lambda %s: None" % signature
    else:
        src = "lambda self, %s: None" % signature

    checksig = eval(src, {})
    _copy_func_details(func, checksig)
    type(mock)._mock_check_sig = checksig

Original issue reported on code.google.com by adiroi...@gmail.com on 28 Jun 2011 at 2:22

GoogleCodeExporter commented 9 years ago
Hello. I've seen that error as well and have fixed it (on trunk) for *some* 
cases (specifically when self is used as an argument anywhere *other* than the 
first argument to a method or function).

Could you show the definition of module.Class that is problematic so I can 
reproduce the issue and see whether or not I have already fixed it please.

Original comment by fuzzyman on 28 Jun 2011 at 2:36

GoogleCodeExporter commented 9 years ago
Thanks for your quick reply.

I can send you the module but unfortunately it is not loosely coupled and is 
using other external modules from Twisted.

In my case it fails when accessing the home_segments property.

Please let me know if you need the whole code.

Original comment by adiroi...@gmail.com on 28 Jun 2011 at 2:58

Attachments:

GoogleCodeExporter commented 9 years ago
I can't reproduce the bug from that I'm afraid. Is there a minimal repro you 
can find?

Certainly passing a class with a property, defined in the same way as 
'home_segments', to autospec works.

Original comment by fuzzyman on 28 Jun 2011 at 3:11

GoogleCodeExporter commented 9 years ago
Alternatively you could try your tests with mock.py from the mercurial repo and 
see if it is fixed. 

I like to know *why* problems happen though - and what you're describing 
doesn't sound identical to the one I fixed. Maybe though.

Original comment by fuzzyman on 28 Jun 2011 at 3:15

GoogleCodeExporter commented 9 years ago
I have tried my tests with mock.py and the problem is fixed.

I will try to create a test that fails in my case using the alpha1 code.

I have attached a more complete class.

It fails when I try to make a call similar to this one:
normal_filesystem = LocalUnixFilesystem()
normal_filesystem.createFolder(
                segments=normal_filesystem.home_segments,
                recursive=False)

Original comment by adiroi...@gmail.com on 28 Jun 2011 at 3:29

Attachments:

GoogleCodeExporter commented 9 years ago
As far as I can tell this works now, so closing the issue. Please create a new 
issue if the problem recurs.

Original comment by fuzzyman on 1 Jul 2011 at 11:40