Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Doc/whatsnew/3.9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,12 @@ and :meth:`~datetime.datetime.isocalendar()` of :class:`datetime.datetime`
methods now returns a :func:`~collections.namedtuple` instead of a :class:`tuple`.
(Contributed by Dong-hee Na in :issue:`24416`.)

dis
---

:class:`dis.Bytecode` objects now support equality comparisons. (Contributed
by Batuhan Taskaya in :issue:`39902`.)

fcntl
-----

Expand Down
10 changes: 10 additions & 0 deletions Lib/dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,16 @@ def dis(self):
lasti=offset)
return output.getvalue()

def __eq__(self, other):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't add a check about underlying code object because I think we should compare the bytecode not the source but I'm OK to change it if needed?

>>> dis.Bytecode("print(1)") == dis.Bytecode(compile("print(1)", "<stdin>", "eval"))
True
>>> dis.Bytecode("print(1)")._original_object == dis.Bytecode(compile("print(1)", "<stdin>", "eval"))._original_object
False

if isinstance(other, Bytecode):
return (self.codeobj, self.first_line, self.current_offset) == (
other.codeobj,
other.first_line,
other.current_offset,
)
else:
return NotImplemented


def _test():
"""Simple test program to disassemble a file."""
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1202,5 +1202,16 @@ def test_from_traceback_dis(self):
b = dis.Bytecode.from_traceback(tb)
self.assertEqual(b.dis(), dis_traceback)

def test_equality(self):
self.assertEqual(dis.Bytecode("print(1)"), dis.Bytecode("print(1)"))
self.assertNotEqual(dis.Bytecode("print(1)"), dis.Bytecode("print(2)"))
self.assertNotEqual(
dis.Bytecode("print(1)", first_line=3), dis.Bytecode("print(1)")
)
self.assertNotEqual(
dis.Bytecode("print(1)", current_offset=5),
dis.Bytecode("print(1)"),
)

if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added support for equality comparisons in ``dis.Bytecode`` objects. Patch by
Batuhan Taskaya.