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

dict views don't implement subtraction correctly #70665

Closed
MojoVampire mannequin opened this issue Mar 3, 2016 · 4 comments
Closed

dict views don't implement subtraction correctly #70665

MojoVampire mannequin opened this issue Mar 3, 2016 · 4 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@MojoVampire
Copy link
Mannequin

MojoVampire mannequin commented Mar 3, 2016

BPO 26478
Nosy @wimglenn, @MojoVampire, @zhangyangyu

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 2016-03-04.06:11:05.806>
created_at = <Date 2016-03-03.23:20:42.873>
labels = ['interpreter-core']
title = "dict views don't implement subtraction correctly"
updated_at = <Date 2016-03-08.04:55:26.092>
user = 'https://github.com/MojoVampire'

bugs.python.org fields:

activity = <Date 2016-03-08.04:55:26.092>
actor = 'python-dev'
assignee = 'none'
closed = True
closed_date = <Date 2016-03-04.06:11:05.806>
closer = 'python-dev'
components = ['Interpreter Core']
creation = <Date 2016-03-03.23:20:42.873>
creator = 'josh.r'
dependencies = []
files = []
hgrepos = []
issue_num = 26478
keywords = []
message_count = 4.0
messages = ['261180', '261182', '261193', '261322']
nosy_count = 4.0
nosy_names = ['python-dev', 'wim.glenn', 'josh.r', 'xiang.zhang']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue26478'
versions = ['Python 3.5', 'Python 3.6']

@MojoVampire
Copy link
Mannequin Author

MojoVampire mannequin commented Mar 3, 2016

Don't know when the problem was introduced, but dictviews_sub is doing:

    tmp = _PyObject_CallMethodId(result, &PyId_difference_update, "O", other);

to implement subtraction (after creating result as a set of the keys in question). That's violating the CallMethod contract though (which states the format string should produce a tuple), and while it looks like CallMethod does fixups when the contract is violated, this creates some very odd behaviors.

With a list, everything is fine:

    >>> d = {0: 'zero', 1: 'one', 2: 'two', 3: 'three'}
    >>> d.keys() - [0, 2]
    {1, 3}
    >>> d.keys() - (0, 2)
    TypeError: 'int' object is not iterable

Basically, the fix up doesn't get applied when you subtract a tuple, so it's as if it's trying to call:

    result.difference_update(*(0, 2))  # Unpacking used to illustrate, effect is  result.difference_update(0, 2)

With the list, it's wrapping to make a one element tuple, so it behaves like:

    result.difference_update(*([0, 2],))  # Unpacking used to illustrate, effect is  result.difference_update([0, 2])

For more details, see http://stackoverflow.com/q/35784258/364696

Fix should be to change call line to:

    tmp = _PyObject_CallMethodObjArgsId(result, &PyId_difference_update, other, NULL);

(assuming _PyObject_CallMethodObjArgsId is a thing), or if it's not a thing, to fix the format string to force tuple wrapping:

    tmp = _PyObject_CallMethodId(result, &PyId_difference_update, "(O)", other);

@MojoVampire MojoVampire mannequin added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Mar 3, 2016
@python-dev
Copy link
Mannequin

python-dev mannequin commented Mar 4, 2016

New changeset 0cae6b6e3d7d by Benjamin Peterson in branch '3.4':
properly use the ObjArgs variant of CallMethod in dictview binary operations (closes bpo-26478)
https://hg.python.org/cpython/rev/0cae6b6e3d7d

New changeset a5bc5c9490f5 by Benjamin Peterson in branch '3.5':
merge 3.4 (closes bpo-26478)
https://hg.python.org/cpython/rev/a5bc5c9490f5

New changeset 9532a8815a9c by Benjamin Peterson in branch 'default':
merge 3.5 (closes bpo-26478)
https://hg.python.org/cpython/rev/9532a8815a9c

@python-dev python-dev mannequin closed this as completed Mar 4, 2016
@wimglenn
Copy link
Mannequin

wimglenn mannequin commented Mar 4, 2016

Well that was patched quickly, impressive turnaround on this

@python-dev
Copy link
Mannequin

python-dev mannequin commented Mar 8, 2016

New changeset 309f3559a729 by Benjamin Peterson in branch '2.7':
properly use PyObject_CallMethod in dictview binary operations (closes bpo-26478)
https://hg.python.org/cpython/rev/309f3559a729

@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
interpreter-core (Objects, Python, Grammar, and Parser dirs)
Projects
None yet
Development

No branches or pull requests

0 participants