Skip to content

3.13: not a == b has incorrect bytecode positions #114671

Closed as not planned
Closed as not planned

Description

@15r10nk

Bug report

Bug description:

The following code reproduces the bug:

import dis

code = """
not a == b 
"""

code = compile(code, "<code>", "exec")

for bc in dis.get_instructions(code):
    p = bc.positions
    print(
        f"{p.lineno}:{p.col_offset} {p.end_lineno}:{p.end_col_offset}",
        bc.opname,
        bc.argval,
    )


for bc in dis.get_instructions(code):
    if bc.opname == "COMPARE_OP":
        assert bc.positions.col_offset == 4

output (Python 3.13.0a3+):

0:0 1:0 RESUME 0
2:4 2:5 LOAD_NAME a
2:9 2:10 LOAD_NAME b
2:0 2:10 COMPARE_OP ==  # <-- incorrect col_offset
2:0 2:10 UNARY_NOT None
2:0 2:10 POP_TOP None
2:0 2:10 RETURN_CONST None
Traceback (most recent call last):
  File "/home/frank/projects/executing/codi.py", line 20, in <module>
    assert bc.positions.col_offset == 4
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError

The source range for the compare operation should not include the not.
There was no problem in 3.12, where the col_offset started at 4

output (Python 3.12.1):

0:0 1:0 RESUME 0
2:4 2:5 LOAD_NAME a
2:9 2:10 LOAD_NAME b
2:4 2:10 COMPARE_OP ==  # <-- correct col_offset
2:0 2:10 UNARY_NOT None
2:0 2:10 POP_TOP None
2:0 2:10 RETURN_CONST None

I bisected the problem down to 7b2d94d (@brandtbucher).
The problem can also be reproduced on the current main (7a47054).

CPython versions tested on:

3.13, CPython main branch

Operating systems tested on:

Linux

Activity

  1. added
    type-bug
    on Jan 28, 2024
  2. added
    3.13
    on Jan 28, 2024
  3. alexmojaki commented on Jan 28, 2024

    @alexmojaki

    Here's how this affects a traceback:

    class A:
        def __eq__(self, other):
            raise NotImplementedError
    
    
    print(not A() == A())
    ➜  ~ python3.12 test.py
    Traceback (most recent call last):
      File "test.py", line 6, in <module>
        print(not A() == A())
                  ^^^^^^^^^^
      File "test.py", line 3, in __eq__
        raise NotImplementedError
    NotImplementedError
    
    ➜  ~ python3.13 test.py
    Traceback (most recent call last):
      File "test.py", line 6, in <module>
        print(not A() == A())
              ^^^^^^^^^^^^^^
      File "test.py", line 3, in __eq__
        raise NotImplementedError
    NotImplementedError
    
  4. gaogaotiantian commented on Jan 28, 2024

    @gaogaotiantian
    Member

    Not sure if this is a bug - yes it has a different behavior. The position of an instruction is basically the source code that generates this instruction, including the arg. In 3.13, for specializing, the COMPARE_OP instruction has a new bit in the arg that indicates the result should be converted to bool, which is part of the instruction itself. In your case, that bit is set by the not before the actual comparison.

    From that perspective, not actually contributes to the bytecode and should probably be included in the bytecode source (position). Of course, I'm not the expert on this topic, so @brandtbucher would have a more official explanation.

  5. brandtbucher commented on Jan 29, 2024

    @brandtbucher
    Member

    Yep. I admit it's a bit weird, but COMPARE_OP now "cares" whether or not it occurs in a boolean context, so it's natural that it would include that boolean context in its source location.

    If the boolean conversion fails, the traceback should probably include the not. Since we only have one position available for both the equality and boolean conversion, I think it makes sense to make it wide enough to include both.

  6. brandtbucher commented on Jan 29, 2024

    @brandtbucher
    Member

    For reference:

    class NastyBool:
        def __bool__(self):
            raise RuntimeError()
    
    class BoolRaises:
        def __eq__(self, _):
            return NastyBool()
    
    class EqRaises:
        def __eq__(self, _):
            raise RuntimeError()

    Tracebacks look like:

    not EqRaises() == ...
        ^^^^^^^^^^^^^^^^^  # 3.12
    ^^^^^^^^^^^^^^^^^^^^^  # 3.13
    not BoolRaises() == ...
    ^^^^^^^^^^^^^^^^^^^^^^^  # 3.12
    ^^^^^^^^^^^^^^^^^^^^^^^  # 3.13
    if not EqRaises() == ...:
           ^^^^^^^^^^^^^^^^^  # 3.12
           ^^^^^^^^^^^^^^^^^  # 3.13
    if not BoolRaises() == ...:
           ^^^^^^^^^^^^^^^^^^^  # 3.12
           ^^^^^^^^^^^^^^^^^^^  # 3.13
  7. 15r10nk commented on Jan 30, 2024

    @15r10nk
    ContributorAuthor

    Thank you for your explanation.

    There is no way to make it behave like in 3.12 if I understand this correct. I think I will find a way to work around this in my code.

    Good to know that it is not a bug in cpython.

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.13type-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions