Batuhan Taskaya <batuhanosmantask...@gmail.com> added the comment:

Code objects themselves supports equality comparisons, 

>>> compile("print(1)", "<stdin>", "eval") == compile("print(1)", "<stdin>", 
>>> "eval")
True

So this patch basically compares the underlying code objects with 2 Bytecode 
specific attribute, first_line and current_offset. So these objects will be 
equal

>>> import dis
>>> dis.Bytecode("print(1)") == dis.Bytecode("print(1)")
True

but these won't
>>> dis.Bytecode("print(1)") == dis.Bytecode("print(1)", first_line=2)
False
>>> dis.Bytecode("print(1)") == dis.Bytecode("print(1)", current_offset=12)
False

A simple example that would be problamatic in .dis() method is code objects 
that contains other code objects

import dis

source = "def x(a, b): print(1)"
print(dis.Bytecode(source).dis())
print(dis.Bytecode(source).dis())
print(dis.Bytecode(source).dis() == dis.Bytecode(source).dis())

  1           0 LOAD_CONST               0 (<code object x at 
!!0x7fd76239aee0!!, file "<disassembly>", line 1>)
              2 LOAD_CONST               1 ('x')
              4 MAKE_FUNCTION            0
              6 STORE_NAME               0 (x)
              8 LOAD_CONST               2 (None)
             10 RETURN_VALUE

  1           0 LOAD_CONST               0 (<code object x at 
!!0x7fd7623b76c0!!, file "<disassembly>", line 1>)
              2 LOAD_CONST               1 ('x')
              4 MAKE_FUNCTION            0
              6 STORE_NAME               0 (x)
              8 LOAD_CONST               2 (None)
             10 RETURN_VALUE

False

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue39902>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to