Skip to content

Commit b786fe8

Browse files
[3.12] gh-105979: Fix exception handling in unmarshal_frozen_code (Python/import.c) (GH-105980) (#106055)
gh-105979: Fix exception handling in `unmarshal_frozen_code` (`Python/import.c`) (GH-105980) (cherry picked from commit cd52803) Co-authored-by: chgnrdv <[email protected]>
1 parent e9366df commit b786fe8

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

Lib/test/test_import/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import unittest
2424
from unittest import mock
2525
import _testinternalcapi
26+
import _imp
2627

2728
from test.support import os_helper
2829
from test.support import (
@@ -763,6 +764,13 @@ def test_dll_dependency_import(self):
763764
env=env,
764765
cwd=os.path.dirname(pyexe))
765766

767+
def test_issue105979(self):
768+
# this used to crash
769+
with self.assertRaises(ImportError) as cm:
770+
_imp.get_frozen_object("x", b"6\'\xd5Cu\x12")
771+
self.assertIn("Frozen object named 'x' is invalid",
772+
str(cm.exception))
773+
766774

767775
@skip_if_dont_write_bytecode
768776
class FilePermissionTests(unittest.TestCase):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix crash in :func:`!_imp.get_frozen_object` due to improper exception handling.

Python/import.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,6 +2053,7 @@ unmarshal_frozen_code(PyInterpreterState *interp, struct frozen_info *info)
20532053
PyObject *co = PyMarshal_ReadObjectFromString(info->data, info->size);
20542054
if (co == NULL) {
20552055
/* Does not contain executable code. */
2056+
PyErr_Clear();
20562057
set_frozen_error(FROZEN_INVALID, info->nameobj);
20572058
return NULL;
20582059
}

0 commit comments

Comments
 (0)