Skip to content

Code objects with non-equal co_linetables compare equal #95150

Closed

Description

@15r10nk

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

Activity

  1. added
    type-bug
    on Jul 22, 2022
  2. alexmojaki commented on Jul 22, 2022

    @alexmojaki

    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'
  3. brandtbucher commented on Jul 22, 2022

    @brandtbucher
    Member

    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.

  4. added
    interpreter-core
    3.11
    3.12
    triaged
    on Jul 22, 2022
  5. changed the title [-]wrong error position in traceback when using generator expressions[/-] [+]Code objects with non-equal `co_linetable`s compare equal[/+] on Jul 23, 2022
  6. brandtbucher commented on Jul 26, 2022

    @brandtbucher
    Member

    @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.

  7. added a commit that references this issue on Aug 1, 2022
  8. added 2 commits that reference this issue on Aug 1, 2022
  9. Repository owner moved this from Todo to Done in Release and Deferred blockers 🚫on Aug 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.113.12interpreter-corerelease-blockertriagedtype-bug

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions