diff --git a/Lib/dis.py b/Lib/dis.py index 10e5f7fb08ab21c..227e0e42c57a688 100644 --- a/Lib/dis.py +++ b/Lib/dis.py @@ -318,7 +318,7 @@ def _get_instructions_bytes(code, varnames=None, names=None, constants=None, arguments. """ - labels = findlabels(code) + labels = _findlabels_bytes(code) starts_line = None for offset, op, arg in _unpack_opargs(code): if linestarts is not None: @@ -432,6 +432,14 @@ def findlabels(code): Return the list of offsets. + """ + return _findlabels_bytes(code.co_code) + +def _findlabels_bytes(code): + """Detect all offsets in a raw compiled bytecode string which are jump targets. + + Return the list of offsets. + """ labels = [] for offset, op, arg in _unpack_opargs(code): diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py index ac5836d288978ce..7b08c1146b7c733 100644 --- a/Lib/test/test_dis.py +++ b/Lib/test/test_dis.py @@ -1202,5 +1202,12 @@ def test_from_traceback_dis(self): b = dis.Bytecode.from_traceback(tb) self.assertEqual(b.dis(), dis_traceback) + def test_findlabels(self): + self.assertEqual( + dis.findlabels(jumpy.__code__), [ + 44, 30, 8, 52, 94, 82, 102, 200, 118, 144, 142, 188, 172, 178, 210 + ]) + + if __name__ == "__main__": unittest.main()