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

Skip to content

Commit 2ee6142

Browse files
committed
Issue #13959: Re-implement imp.load_package() in imp.py.
Thanks to Eric Snow for helping with imp.load_module() (previous commit) which led to the removal of a bunch of C code.
1 parent 01a7617 commit 2ee6142

2 files changed

Lines changed: 17 additions & 173 deletions

File tree

Lib/imp.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,29 @@
1414
from _imp import (get_magic, get_tag, get_suffixes, cache_from_source,
1515
source_from_cache)
1616
# Should be re-implemented here (and mostly deprecated)
17-
from _imp import (find_module, load_compiled,
18-
load_package, load_source, NullImporter,
17+
from _imp import (find_module, load_compiled, load_source, NullImporter,
1918
SEARCH_ERROR, PY_SOURCE, PY_COMPILED, C_EXTENSION,
2019
PY_RESOURCE, PKG_DIRECTORY, C_BUILTIN, PY_FROZEN,
2120
PY_CODERESOURCE, IMP_HOOK)
2221

2322
from importlib._bootstrap import _new_module as new_module
2423

24+
from importlib import _bootstrap
25+
import os
26+
27+
28+
def load_package(name, path):
29+
if os.path.isdir(path):
30+
extensions = _bootstrap._suffix_list(PY_SOURCE)
31+
extensions += _bootstrap._suffix_list(PY_COMPILED)
32+
for extension in extensions:
33+
path = os.path.join(path, '__init__'+extension)
34+
if os.path.exists(path):
35+
break
36+
else:
37+
raise ValueError('{!r} is not a package'.format(path))
38+
return _bootstrap._SourceFileLoader(name, path).load_module(name)
39+
2540

2641
def load_module(name, file, filename, details):
2742
"""Load a module, given information returned by find_module().

Python/import.c

Lines changed: 0 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,65 +1602,10 @@ get_sourcefile(PyObject *filename)
16021602
}
16031603

16041604
/* Forward */
1605-
static PyObject *load_module(PyObject *, FILE *, PyObject *, int, PyObject *);
16061605
static struct filedescr *find_module(PyObject *, PyObject *, PyObject *,
16071606
PyObject **, FILE **, PyObject **);
16081607
static struct _frozen * find_frozen(PyObject *);
16091608

1610-
/* Load a package and return its module object WITH INCREMENTED
1611-
REFERENCE COUNT */
1612-
1613-
static PyObject *
1614-
load_package(PyObject *name, PyObject *pathname)
1615-
{
1616-
PyObject *m, *d, *bufobj;
1617-
PyObject *file = NULL, *path_list = NULL;
1618-
int err;
1619-
FILE *fp = NULL;
1620-
struct filedescr *fdp;
1621-
1622-
m = PyImport_AddModuleObject(name);
1623-
if (m == NULL)
1624-
return NULL;
1625-
if (Py_VerboseFlag)
1626-
PySys_FormatStderr("import %U # directory %R\n",
1627-
name, pathname);
1628-
file = get_sourcefile(pathname);
1629-
if (file == NULL)
1630-
return NULL;
1631-
path_list = Py_BuildValue("[O]", file);
1632-
if (path_list == NULL) {
1633-
Py_DECREF(file);
1634-
return NULL;
1635-
}
1636-
d = PyModule_GetDict(m);
1637-
err = PyDict_SetItemString(d, "__file__", file);
1638-
Py_DECREF(file);
1639-
if (err == 0)
1640-
err = PyDict_SetItemString(d, "__path__", path_list);
1641-
if (err != 0) {
1642-
Py_DECREF(path_list);
1643-
return NULL;
1644-
}
1645-
fdp = find_module(name, initstr, path_list,
1646-
&bufobj, &fp, NULL);
1647-
Py_DECREF(path_list);
1648-
if (fdp == NULL) {
1649-
if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1650-
PyErr_Clear();
1651-
Py_INCREF(m);
1652-
return m;
1653-
}
1654-
else
1655-
return NULL;
1656-
}
1657-
m = load_module(name, fp, bufobj, fdp->type, NULL);
1658-
Py_XDECREF(bufobj);
1659-
if (fp != NULL)
1660-
fclose(fp);
1661-
return m;
1662-
}
1663-
16641609

16651610
/* Helper to test for built-in module */
16661611

@@ -2434,108 +2379,6 @@ find_init_module(PyObject *directory)
24342379

24352380
static int init_builtin(PyObject *); /* Forward */
24362381

2437-
static PyObject*
2438-
load_builtin(PyObject *name, int type)
2439-
{
2440-
PyObject *m, *modules;
2441-
int err;
2442-
2443-
if (type == C_BUILTIN)
2444-
err = init_builtin(name);
2445-
else
2446-
err = PyImport_ImportFrozenModuleObject(name);
2447-
if (err < 0)
2448-
return NULL;
2449-
if (err == 0) {
2450-
PyErr_Format(PyExc_ImportError,
2451-
"Purported %s module %R not found",
2452-
type == C_BUILTIN ? "builtin" : "frozen",
2453-
name);
2454-
return NULL;
2455-
}
2456-
2457-
modules = PyImport_GetModuleDict();
2458-
m = PyDict_GetItem(modules, name);
2459-
if (m == NULL) {
2460-
PyErr_Format(
2461-
PyExc_ImportError,
2462-
"%s module %R not properly initialized",
2463-
type == C_BUILTIN ? "builtin" : "frozen",
2464-
name);
2465-
return NULL;
2466-
}
2467-
Py_INCREF(m);
2468-
return m;
2469-
}
2470-
2471-
/* Load an external module using the default search path and return
2472-
its module object WITH INCREMENTED REFERENCE COUNT */
2473-
2474-
static PyObject *
2475-
load_module(PyObject *name, FILE *fp, PyObject *pathname, int type, PyObject *loader)
2476-
{
2477-
PyObject *m;
2478-
2479-
/* First check that there's an open file (if we need one) */
2480-
switch (type) {
2481-
case PY_SOURCE:
2482-
case PY_COMPILED:
2483-
if (fp == NULL) {
2484-
PyErr_Format(PyExc_ValueError,
2485-
"file object required for import (type code %d)",
2486-
type);
2487-
return NULL;
2488-
}
2489-
}
2490-
2491-
switch (type) {
2492-
2493-
case PY_SOURCE:
2494-
m = load_source_module(name, pathname, fp);
2495-
break;
2496-
2497-
case PY_COMPILED:
2498-
m = load_compiled_module(name, pathname, fp);
2499-
break;
2500-
2501-
#ifdef HAVE_DYNAMIC_LOADING
2502-
case C_EXTENSION:
2503-
m = _PyImport_LoadDynamicModule(name, pathname, fp);
2504-
break;
2505-
#endif
2506-
2507-
case PKG_DIRECTORY:
2508-
m = load_package(name, pathname);
2509-
break;
2510-
2511-
case C_BUILTIN:
2512-
case PY_FROZEN:
2513-
m = load_builtin(name, type);
2514-
break;
2515-
2516-
case IMP_HOOK: {
2517-
_Py_IDENTIFIER(load_module);
2518-
if (loader == NULL) {
2519-
PyErr_SetString(PyExc_ImportError,
2520-
"import hook without loader");
2521-
return NULL;
2522-
}
2523-
m = _PyObject_CallMethodId(loader, &PyId_load_module, "O", name);
2524-
break;
2525-
}
2526-
2527-
default:
2528-
PyErr_Format(PyExc_ImportError,
2529-
"Don't know how to import %R (type code %d)",
2530-
name, type);
2531-
m = NULL;
2532-
2533-
}
2534-
2535-
return m;
2536-
}
2537-
2538-
25392382
/* Initialize a built-in module.
25402383
Return 1 for success, 0 if the module is not found, and -1 with
25412384
an exception set if the initialization failed. */
@@ -3600,19 +3443,6 @@ imp_load_source(PyObject *self, PyObject *args)
36003443
return m;
36013444
}
36023445

3603-
static PyObject *
3604-
imp_load_package(PyObject *self, PyObject *args)
3605-
{
3606-
PyObject *name, *pathname;
3607-
PyObject * ret;
3608-
if (!PyArg_ParseTuple(args, "UO&:load_package",
3609-
&name, PyUnicode_FSDecoder, &pathname))
3610-
return NULL;
3611-
ret = load_package(name, pathname);
3612-
Py_DECREF(pathname);
3613-
return ret;
3614-
}
3615-
36163446
static PyObject *
36173447
imp_reload(PyObject *self, PyObject *v)
36183448
{
@@ -3764,7 +3594,6 @@ static PyMethodDef imp_methods[] = {
37643594
#ifdef HAVE_DYNAMIC_LOADING
37653595
{"load_dynamic", imp_load_dynamic, METH_VARARGS},
37663596
#endif
3767-
{"load_package", imp_load_package, METH_VARARGS},
37683597
{"load_source", imp_load_source, METH_VARARGS},
37693598
{"_fix_co_filename", imp_fix_co_filename, METH_VARARGS},
37703599
{NULL, NULL} /* sentinel */

0 commit comments

Comments
 (0)