diff --git a/Doc/whatsnew/3.9.rst b/Doc/whatsnew/3.9.rst index 49a52b7504bc95..5ce6bbc64501f4 100644 --- a/Doc/whatsnew/3.9.rst +++ b/Doc/whatsnew/3.9.rst @@ -420,6 +420,12 @@ The :command:`upload` command now creates SHA2-256 and Blake2b-256 hash digests. It skips MD5 on platforms that block MD5 digest. (Contributed by Christian Heimes in :issue:`40698`.) +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 318729091fa098..91e2e8ddb232de 100644 --- a/Lib/dis.py +++ b/Lib/dis.py @@ -1151,6 +1151,16 @@ def dis(self): formatter=formatter) 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 main(args=None): import argparse diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py index 9879a665bc0500..e0e457149cd0a5 100644 --- a/Lib/test/test_dis.py +++ b/Lib/test/test_dis.py @@ -2406,6 +2406,17 @@ 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)"), + ) + @requires_debug_ranges() def test_bytecode_co_positions(self): bytecode = dis.Bytecode("a=1") 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 00000000000000..2333bbfc982e8c --- /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.