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
mock: Crash on comparing call_args with long strings #69045
Comments
What steps will reproduce the problem? >>> from mock import Mock
>>> m = Mock()
>>> m(1, 2)
<Mock name='mock()' id='139781492681104'>
>>> m.call_args == "foob"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/wilfred/.py_envs/trifle/lib/python2.7/site-packages/mock.py", line 2061, in __eq__
first, second = other
ValueError: too many values to unpack What is the expected output? What do you see instead? Expected False, got an error instead. (Migrated from testing-cabal/mock#232 ) |
call_args is not user settable! It is set for you by the mock when it is called. Arguably it could be a property instead. |
Oops, I misunderstood the bug report - however, call_args is a tuple, so you can't compare it directly to a string like that. Please refer to the docs on using call_args. |
This caught me by surprise and I spent a while debugging due to this issue. Isn't it reasonable that I can compare two values in Python without exceptions being raised? >>> (1, 2) == "foob"
False I'm happy to write a patch. |
This bug is particularly subtle because it only applies to *long* strings. >>> m.call_args == "f"
False
>>> m.call_args == "fo"
False
>>> m.call_args == "foo"
False
>>> m.call_args == "foob"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.linux-x86_64/egg/mock.py", line 2061, in __eq__
ValueError: too many values to unpack |
Ok, fair enough. |
Yeah, if it isn't comparable it should return either False or NotImplemented, not raise an exception. False would be better here, I think. |
It looks like there's a related bug in call_args around __ne__: >>> m = Mock()
>>> m(1,2)
<Mock name='mock()' id='4483976016'>
>>> m.call_args
call(1, 2)
>>> m.call_args == call(1,2)
True
>>> m.call_args != call(1,2)
True Any reason not to define __ne__ as not __eq__? Otherwise it looks like you fall back to tuple.__ne__, which does the wrong thing here. |
Here's a simple patch + test for the original bug. I'll file the __ne__ question separately. |
New changeset 83ea55a1204a by Berker Peksag in branch '3.4': New changeset df91c1879e56 by Berker Peksag in branch '3.5': New changeset 6853b4bc0b22 by Berker Peksag in branch 'default': |
Thanks! For the record, the __ne__ issue created by A Kaptur is bpo-24997. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: