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

Skip to content

Commit 9905ef9

Browse files
committed
Added support for __all__, which should be a list of modules to be
imported when the user says "from package import *".
1 parent c8bf884 commit 9905ef9

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

Python/import.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ static PyObject *load_next Py_PROTO((PyObject *mod, PyObject *altmod,
10001000
char **p_name, char *buf, int *p_buflen));
10011001
static int mark_miss Py_PROTO((char *name));
10021002
static int ensure_fromlist Py_PROTO((PyObject *mod, PyObject *fromlist,
1003-
char *buf, int buflen));
1003+
char *buf, int buflen, int recursive));
10041004
static PyObject * import_submodule Py_PROTO((PyObject *mod,
10051005
char *name, char *fullname));
10061006

@@ -1054,7 +1054,7 @@ PyImport_ImportModuleEx(name, globals, locals, fromlist)
10541054
}
10551055

10561056
Py_DECREF(head);
1057-
if (!ensure_fromlist(tail, fromlist, buf, buflen)) {
1057+
if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
10581058
Py_DECREF(tail);
10591059
return NULL;
10601060
}
@@ -1203,11 +1203,12 @@ mark_miss(name)
12031203
}
12041204

12051205
static int
1206-
ensure_fromlist(mod, fromlist, buf, buflen)
1206+
ensure_fromlist(mod, fromlist, buf, buflen, recursive)
12071207
PyObject *mod;
12081208
PyObject *fromlist;
12091209
char *buf;
12101210
int buflen;
1211+
int recursive;
12111212
{
12121213
int i;
12131214

@@ -1231,7 +1232,19 @@ ensure_fromlist(mod, fromlist, buf, buflen)
12311232
return 0;
12321233
}
12331234
if (PyString_AS_STRING(item)[0] == '*') {
1235+
PyObject *all;
12341236
Py_DECREF(item);
1237+
/* See if the package defines __all__ */
1238+
if (recursive)
1239+
continue; /* Avoid endless recursion */
1240+
all = PyObject_GetAttrString(mod, "__all__");
1241+
if (all == NULL)
1242+
PyErr_Clear();
1243+
else {
1244+
if (!ensure_fromlist(mod, all, buf, buflen, 1))
1245+
return 0;
1246+
Py_DECREF(all);
1247+
}
12351248
continue;
12361249
}
12371250
hasit = PyObject_HasAttr(mod, item);

0 commit comments

Comments
 (0)