From c1bf01bb63ccdeaa6ea1cbecbc4a5d348f44b8bc Mon Sep 17 00:00:00 2001 From: Batuhan Taskaya Date: Sun, 8 Mar 2020 15:19:08 +0300 Subject: [PATCH] bpo-39902: support equality comparisons in dis.Bytecode --- Doc/whatsnew/3.9.rst | 6 ++++++ Lib/dis.py | 10 ++++++++++ Lib/test/test_dis.py | 11 +++++++++++ .../Library/2020-03-08-15-16-45.bpo-39902.HoE484.rst | 2 ++ 4 files changed, 29 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2020-03-08-15-16-45.bpo-39902.HoE484.rst diff --git a/Doc/whatsnew/3.9.rst b/Doc/whatsnew/3.9.rst index 479c33b4a7fa1c3..2cf59bfcef53580 100644 --- a/Doc/whatsnew/3.9.rst +++ b/Doc/whatsnew/3.9.rst @@ -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 ----- diff --git a/Lib/dis.py b/Lib/dis.py index 10e5f7fb08ab21c..a71ba5f08382f88 100644 --- a/Lib/dis.py +++ b/Lib/dis.py @@ -536,6 +536,16 @@ def dis(self): lasti=offset) return output.getvalue() + def __eq__(self, other): + 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.""" diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py index ac5836d288978ce..2608cbfcd9624a2 100644 --- a/Lib/test/test_dis.py +++ b/Lib/test/test_dis.py @@ -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() diff --git a/Misc/NEWS.d/next/Library/2020-03-08-15-16-45.bpo-39902.HoE484.rst b/Misc/NEWS.d/next/Library/2020-03-08-15-16-45.bpo-39902.HoE484.rst new file mode 100644 index 000000000000000..2333bbfc982e8ce --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-03-08-15-16-45.bpo-39902.HoE484.rst @@ -0,0 +1,2 @@ +Added support for equality comparisons in ``dis.Bytecode`` objects. Patch by +Batuhan Taskaya.