Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8f4d331

Browse files
committed
First part of SF patch #416704: More robust freeze, by Toby Dickenson.
This fixes the behavior reported by SF bug #404545, where a file x.y.py could be imported by the statement "import x.y" when there's a frozen package x (I believe even if x.y also exists as a frozen module).
1 parent 4114a4a commit 8f4d331

1 file changed

Lines changed: 23 additions & 5 deletions

File tree

Python/import.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -877,15 +877,15 @@ find_module(char *realname, PyObject *path, char *buf, size_t buflen,
877877
char name[MAXPATHLEN+1];
878878

879879
if (strlen(realname) > MAXPATHLEN) {
880-
PyErr_SetString(PyExc_OverflowError, "module name is too long");
880+
PyErr_SetString(PyExc_OverflowError,
881+
"module name is too long");
881882
return NULL;
882883
}
883884
strcpy(name, realname);
884885

885886
if (path != NULL && PyString_Check(path)) {
886-
/* Submodule of "frozen" package:
887-
Set name to the fullname, path to NULL
888-
and continue as "usual" */
887+
/* The only type of submodule allowed inside a "frozen"
888+
package are other frozen modules or packages. */
889889
if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
890890
PyErr_SetString(PyExc_ImportError,
891891
"full frozen module name too long");
@@ -895,7 +895,13 @@ find_module(char *realname, PyObject *path, char *buf, size_t buflen,
895895
strcat(buf, ".");
896896
strcat(buf, name);
897897
strcpy(name, buf);
898-
path = NULL;
898+
if (find_frozen(name) != NULL) {
899+
strcpy(buf, name);
900+
return &fd_frozen;
901+
}
902+
PyErr_Format(PyExc_ImportError,
903+
"No frozen submodule named %.200s", name);
904+
return NULL;
899905
}
900906
if (path == NULL) {
901907
if (is_builtin(name)) {
@@ -1441,6 +1447,12 @@ get_frozen_object(char *name)
14411447
name);
14421448
return NULL;
14431449
}
1450+
if (p->code == NULL) {
1451+
PyErr_Format(PyExc_ImportError,
1452+
"Excluded frozen object named %.200s",
1453+
name);
1454+
return NULL;
1455+
}
14441456
size = p->size;
14451457
if (size < 0)
14461458
size = -size;
@@ -1463,6 +1475,12 @@ PyImport_ImportFrozenModule(char *name)
14631475

14641476
if (p == NULL)
14651477
return 0;
1478+
if (p->code == NULL) {
1479+
PyErr_Format(PyExc_ImportError,
1480+
"Excluded frozen object named %.200s",
1481+
name);
1482+
return -1;
1483+
}
14661484
size = p->size;
14671485
ispackage = (size < 0);
14681486
if (ispackage)

0 commit comments

Comments
 (0)