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

Code objects with non-equal co_linetables compare equal #95150

Closed
15r10nk opened this issue Jul 22, 2022 · 3 comments
Closed

Code objects with non-equal co_linetables compare equal #95150

15r10nk opened this issue Jul 22, 2022 · 3 comments
Labels
3.11 3.12 interpreter-core Interpreter core (Objects, Python, Grammar, and Parser dirs) release-blocker triaged The issue has been accepted as valid by a triager. type-bug An unexpected behavior, bug, or error

Comments

@15r10nk
Copy link

@15r10nk 15r10nk commented Jul 22, 2022

Bug report

def f(a, b):
    return dict((k.lower(), v) for k, v in a.items()) == dict(
        (k.lower(), v) for k, v in b.items()
    )

f({"a": 5}, {1: 1})

output:

Traceback (most recent call last):
  File "/home/frank/projects/executing/test8.py", line 8, in <module>
    f({"a": 5}, {1: 1})
  File "/home/frank/projects/executing/test8.py", line 2, in f
    return dict((k.lower(), v) for k, v in a.items()) == dict(
                                                         ^^^^^
  File "/home/frank/projects/executing/test8.py", line 2, in <genexpr>
    return dict((k.lower(), v) for k, v in a.items()) == dict(
                 ^^^^^^^
AttributeError: 'int' object has no attribute 'lower'

The error should be reported at line 3 and not at line 2, because the integer is passed to b

Your environment

  • current cpython 3.11 branch (3a33e9b)
  • and the Python 3.11.0b4
@15r10nk 15r10nk added the type-bug An unexpected behavior, bug, or error label Jul 22, 2022
15r10nk added a commit to 15r10nk/executing that referenced this issue Jul 22, 2022
@alexmojaki
Copy link

@alexmojaki alexmojaki commented Jul 22, 2022

Simpler reproduction:

[k.lower() for k in "a"], [k.lower() for k in [1]]
Traceback (most recent call last):
  File "...", line 1, in <module>
    [k.lower() for k in "a"], [k.lower() for k in [1]]
                              ^^^^^^^^^^^^^^^^^^^^^^^^
  File "...", line 1, in <listcomp>
    [k.lower() for k in "a"], [k.lower() for k in [1]]
     ^^^^^^^^^
AttributeError: 'int' object has no attribute 'lower'

@brandtbucher
Copy link
Member

@brandtbucher brandtbucher commented Jul 22, 2022

Ooh, nice find! The code objects are being "deduplicated" in the compiler, even though they have different column positions.

>>> def f():
...     (a for b in c), (a for b in d)
... 
>>> f.__code__.co_consts
(None, <code object <genexpr> at 0x7fa4fbf92640, file "<stdin>", line 2>)

We need to consider the entire positions table when comparing code objects for equality.

@brandtbucher brandtbucher added interpreter-core Interpreter core (Objects, Python, Grammar, and Parser dirs) 3.11 3.12 triaged The issue has been accepted as valid by a triager. labels Jul 22, 2022
@brandtbucher brandtbucher changed the title wrong error position in traceback when using generator expressions Code objects with non-equal co_linetables compare equal Jul 23, 2022
@brandtbucher
Copy link
Member

@brandtbucher brandtbucher commented Jul 26, 2022

@markshannon: we should add co_linetable to the code equality (and maybe hash?) functions.

Probably co_exceptiontable too, since I’m pretty sure it’s possible to have two code objects that differ only in exception table when compiling custom ASTs. We definitely don't want those to dedup.

miss-islington pushed a commit to miss-islington/cpython that referenced this issue Aug 1, 2022
…d equality (pythonGH-95509)

(cherry picked from commit c7e5bba)

Co-authored-by: Brandt Bucher <brandtbucher@gmail.com>
miss-islington added a commit that referenced this issue Aug 1, 2022
…lity (GH-95509)

(cherry picked from commit c7e5bba)

Co-authored-by: Brandt Bucher <brandtbucher@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 3.12 interpreter-core Interpreter core (Objects, Python, Grammar, and Parser dirs) release-blocker triaged The issue has been accepted as valid by a triager. type-bug An unexpected behavior, bug, or error
Projects
Status: Done
Development

No branches or pull requests

3 participants