diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/pickle.py | 18 | ||||
-rw-r--r-- | Lib/test/pickletester.py | 4 |
2 files changed, 18 insertions, 4 deletions
diff --git a/Lib/pickle.py b/Lib/pickle.py index 71aa57d..01d4142 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -1604,17 +1604,29 @@ class _Unpickler: def load_get(self): i = int(self.readline()[:-1]) - self.append(self.memo[i]) + try: + self.append(self.memo[i]) + except KeyError: + msg = f'Memo value not found at index {i}' + raise UnpicklingError(msg) from None dispatch[GET[0]] = load_get def load_binget(self): i = self.read(1)[0] - self.append(self.memo[i]) + try: + self.append(self.memo[i]) + except KeyError as exc: + msg = f'Memo value not found at index {i}' + raise UnpicklingError(msg) from None dispatch[BINGET[0]] = load_binget def load_long_binget(self): i, = unpack('<I', self.read(4)) - self.append(self.memo[i]) + try: + self.append(self.memo[i]) + except KeyError as exc: + msg = f'Memo value not found at index {i}' + raise UnpicklingError(msg) from None dispatch[LONG_BINGET[0]] = load_long_binget def load_put(self): diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py index c9f3746..953fd5c 100644 --- a/Lib/test/pickletester.py +++ b/Lib/test/pickletester.py @@ -1019,7 +1019,9 @@ class AbstractUnpickleTests(unittest.TestCase): self.assertEqual(self.loads(dumped), '\u20ac\x00') def test_misc_get(self): - self.check_unpickling_error(KeyError, b'g0\np0') + self.check_unpickling_error(pickle.UnpicklingError, b'g0\np0') + self.check_unpickling_error(pickle.UnpicklingError, b'jens:') + self.check_unpickling_error(pickle.UnpicklingError, b'hens:') self.assert_is_copy([(100,), (100,)], self.loads(b'((Kdtp0\nh\x00l.))')) |