Skip to content
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

Closed
wilfred mannequin opened this issue Aug 13, 2015 · 11 comments
Closed

mock: Crash on comparing call_args with long strings #69045

wilfred mannequin opened this issue Aug 13, 2015 · 11 comments
Labels
easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@wilfred
Copy link
Mannequin

wilfred mannequin commented Aug 13, 2015

BPO 24857
Nosy @bitdancer, @voidspace, @berkerpeksag, @Wilfred
Files
  • issue24857.patch
  • 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:

    assignee = None
    closed_at = <Date 2015-09-09.20:45:21.975>
    created_at = <Date 2015-08-13.08:34:40.989>
    labels = ['easy', 'type-bug', 'library']
    title = 'mock: Crash on comparing call_args with long strings'
    updated_at = <Date 2015-09-09.20:45:21.973>
    user = 'https://github.com/wilfred'

    bugs.python.org fields:

    activity = <Date 2015-09-09.20:45:21.973>
    actor = 'berker.peksag'
    assignee = 'none'
    closed = True
    closed_date = <Date 2015-09-09.20:45:21.975>
    closer = 'berker.peksag'
    components = ['Library (Lib)']
    creation = <Date 2015-08-13.08:34:40.989>
    creator = 'Wilfred.Hughes'
    dependencies = []
    files = ['40348']
    hgrepos = []
    issue_num = 24857
    keywords = ['patch', 'easy']
    message_count = 11.0
    messages = ['248504', '248508', '248509', '248510', '248511', '248512', '248517', '249709', '249710', '250333', '250334']
    nosy_count = 6.0
    nosy_names = ['r.david.murray', 'michael.foord', 'python-dev', 'berker.peksag', 'akaptur', 'Wilfred.Hughes']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue24857'
    versions = ['Python 3.4', 'Python 3.5', 'Python 3.6']

    @wilfred
    Copy link
    Mannequin Author

    wilfred mannequin commented Aug 13, 2015

    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 )

    @wilfred wilfred mannequin added type-crash A hard crash of the interpreter, possibly with a core dump stdlib Python modules in the Lib dir labels Aug 13, 2015
    @serhiy-storchaka serhiy-storchaka added type-bug An unexpected behavior, bug, or error and removed type-crash A hard crash of the interpreter, possibly with a core dump labels Aug 13, 2015
    @voidspace
    Copy link
    Contributor

    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.

    @voidspace
    Copy link
    Contributor

    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.

    @wilfred wilfred mannequin changed the title Crash on comparing call_args with long strings mock: Crash on comparing call_args with long strings Aug 13, 2015
    @wilfred
    Copy link
    Mannequin Author

    wilfred mannequin commented Aug 13, 2015

    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.

    @wilfred
    Copy link
    Mannequin Author

    wilfred mannequin commented Aug 13, 2015

    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

    @voidspace
    Copy link
    Contributor

    Ok, fair enough.

    @voidspace voidspace reopened this Aug 13, 2015
    @voidspace voidspace removed the invalid label Aug 13, 2015
    @bitdancer
    Copy link
    Member

    Yeah, if it isn't comparable it should return either False or NotImplemented, not raise an exception. False would be better here, I think.

    @bitdancer bitdancer added the easy label Aug 13, 2015
    @akaptur
    Copy link
    Mannequin

    akaptur mannequin commented Sep 4, 2015

    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.

    @akaptur
    Copy link
    Mannequin

    akaptur mannequin commented Sep 4, 2015

    Here's a simple patch + test for the original bug. I'll file the __ne__ question separately.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Sep 9, 2015

    New changeset 83ea55a1204a by Berker Peksag in branch '3.4':
    Issue bpo-24857: Comparing call_args to a long sequence now correctly returns a
    https://hg.python.org/cpython/rev/83ea55a1204a

    New changeset df91c1879e56 by Berker Peksag in branch '3.5':
    Issue bpo-24857: Comparing call_args to a long sequence now correctly returns a
    https://hg.python.org/cpython/rev/df91c1879e56

    New changeset 6853b4bc0b22 by Berker Peksag in branch 'default':
    Issue bpo-24857: Comparing call_args to a long sequence now correctly returns a
    https://hg.python.org/cpython/rev/6853b4bc0b22

    @berkerpeksag
    Copy link
    Member

    Thanks!

    For the record, the __ne__ issue created by A Kaptur is bpo-24997.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants