Skip to content

Commit c1bf01b

Browse files
committed
bpo-39902: support equality comparisons in dis.Bytecode
1 parent dff92bb commit c1bf01b

4 files changed

Lines changed: 29 additions & 0 deletions

File tree

Doc/whatsnew/3.9.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,12 @@ and :meth:`~datetime.datetime.isocalendar()` of :class:`datetime.datetime`
325325
methods now returns a :func:`~collections.namedtuple` instead of a :class:`tuple`.
326326
(Contributed by Dong-hee Na in :issue:`24416`.)
327327

328+
dis
329+
---
330+
331+
:class:`dis.Bytecode` objects now support equality comparisons. (Contributed
332+
by Batuhan Taskaya in :issue:`39902`.)
333+
328334
fcntl
329335
-----
330336

Lib/dis.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,16 @@ def dis(self):
536536
lasti=offset)
537537
return output.getvalue()
538538

539+
def __eq__(self, other):
540+
if isinstance(other, Bytecode):
541+
return (self.codeobj, self.first_line, self.current_offset) == (
542+
other.codeobj,
543+
other.first_line,
544+
other.current_offset,
545+
)
546+
else:
547+
return NotImplemented
548+
539549

540550
def _test():
541551
"""Simple test program to disassemble a file."""

Lib/test/test_dis.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,5 +1202,16 @@ def test_from_traceback_dis(self):
12021202
b = dis.Bytecode.from_traceback(tb)
12031203
self.assertEqual(b.dis(), dis_traceback)
12041204

1205+
def test_equality(self):
1206+
self.assertEqual(dis.Bytecode("print(1)"), dis.Bytecode("print(1)"))
1207+
self.assertNotEqual(dis.Bytecode("print(1)"), dis.Bytecode("print(2)"))
1208+
self.assertNotEqual(
1209+
dis.Bytecode("print(1)", first_line=3), dis.Bytecode("print(1)")
1210+
)
1211+
self.assertNotEqual(
1212+
dis.Bytecode("print(1)", current_offset=5),
1213+
dis.Bytecode("print(1)"),
1214+
)
1215+
12051216
if __name__ == "__main__":
12061217
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Added support for equality comparisons in ``dis.Bytecode`` objects. Patch by
2+
Batuhan Taskaya.

0 commit comments

Comments
 (0)