diff --git a/Lib/test/test_marshal.py b/Lib/test/test_marshal.py index 9c4d91c456dc5d9..cd53a77866d55c1 100644 --- a/Lib/test/test_marshal.py +++ b/Lib/test/test_marshal.py @@ -4,6 +4,7 @@ import array import io import marshal +import struct import sys import unittest import os @@ -140,6 +141,29 @@ def test_different_filenames(self): self.assertEqual(co1.co_filename, "f1") self.assertEqual(co2.co_filename, "f2") + def test_inconsistent_code_object(self): + # localsplusnames and localspluskinds must have equal length; if + # not, marshal must raise ValueError, not SystemError (gh-151830). + co = compile("def f():\n a=1;b=2;c=3", "", "exec").co_consts[0] + n = len(co.co_varnames) + len(co.co_cellvars) + len(co.co_freevars) + blob = marshal.dumps(co) + # Find the localspluskinds record: the TYPE_STRING ('s') whose payload + # is exactly n bytes long (one kind byte per localsplus name). + kinds = None + for off in range(len(blob) - 5): + if blob[off] == ord('s'): + if struct.unpack_from('