Issue #13959: Re-implement imp.load_compiled() in imp.py.
This commit is contained in:
parent
273323cf68
commit
64befe939c
25
Lib/imp.py
25
Lib/imp.py
|
@ -14,7 +14,7 @@ from _imp import (lock_held, acquire_lock, release_lock, reload,
|
||||||
from _imp import (get_magic, get_tag, get_suffixes, cache_from_source,
|
from _imp import (get_magic, get_tag, get_suffixes, cache_from_source,
|
||||||
source_from_cache)
|
source_from_cache)
|
||||||
# Should be re-implemented here (and mostly deprecated)
|
# Should be re-implemented here (and mostly deprecated)
|
||||||
from _imp import (find_module, load_compiled, NullImporter,
|
from _imp import (find_module, NullImporter,
|
||||||
SEARCH_ERROR, PY_SOURCE, PY_COMPILED, C_EXTENSION,
|
SEARCH_ERROR, PY_SOURCE, PY_COMPILED, C_EXTENSION,
|
||||||
PY_RESOURCE, PKG_DIRECTORY, C_BUILTIN, PY_FROZEN,
|
PY_RESOURCE, PKG_DIRECTORY, C_BUILTIN, PY_FROZEN,
|
||||||
PY_CODERESOURCE, IMP_HOOK)
|
PY_CODERESOURCE, IMP_HOOK)
|
||||||
|
@ -25,17 +25,17 @@ from importlib import _bootstrap
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
class _LoadSourceCompatibility(_bootstrap._SourceFileLoader):
|
class _HackedGetData:
|
||||||
|
|
||||||
"""Compatibility support for implementing load_source()."""
|
"""Compatibiilty support for 'file' arguments of various load_*()
|
||||||
|
functions."""
|
||||||
|
|
||||||
def __init__(self, fullname, path, file=None):
|
def __init__(self, fullname, path, file=None):
|
||||||
super().__init__(fullname, path)
|
super().__init__(fullname, path)
|
||||||
self.file = file
|
self.file = file
|
||||||
|
|
||||||
def get_data(self, path):
|
def get_data(self, path):
|
||||||
"""Gross hack to contort SourceFileLoader to deal w/ load_source()'s bad
|
"""Gross hack to contort loader to deal w/ load_*()'s bad API."""
|
||||||
API."""
|
|
||||||
if self.file and path == self._path:
|
if self.file and path == self._path:
|
||||||
with self.file:
|
with self.file:
|
||||||
# Technically should be returning bytes, but
|
# Technically should be returning bytes, but
|
||||||
|
@ -48,10 +48,25 @@ class _LoadSourceCompatibility(_bootstrap._SourceFileLoader):
|
||||||
return super().get_data(path)
|
return super().get_data(path)
|
||||||
|
|
||||||
|
|
||||||
|
class _LoadSourceCompatibility(_HackedGetData, _bootstrap._SourceFileLoader):
|
||||||
|
|
||||||
|
"""Compatibility support for implementing load_source()."""
|
||||||
|
|
||||||
|
|
||||||
def load_source(name, pathname, file=None):
|
def load_source(name, pathname, file=None):
|
||||||
return _LoadSourceCompatibility(name, pathname, file).load_module(name)
|
return _LoadSourceCompatibility(name, pathname, file).load_module(name)
|
||||||
|
|
||||||
|
|
||||||
|
class _LoadCompiledCompatibility(_HackedGetData,
|
||||||
|
_bootstrap._SourcelessFileLoader):
|
||||||
|
|
||||||
|
"""Compatibility support for implementing load_compiled()."""
|
||||||
|
|
||||||
|
|
||||||
|
def load_compiled(name, pathname, file=None):
|
||||||
|
return _LoadCompiledCompatibility(name, pathname, file).load_module(name)
|
||||||
|
|
||||||
|
|
||||||
def load_package(name, path):
|
def load_package(name, path):
|
||||||
if os.path.isdir(path):
|
if os.path.isdir(path):
|
||||||
extensions = _bootstrap._suffix_list(PY_SOURCE)
|
extensions = _bootstrap._suffix_list(PY_SOURCE)
|
||||||
|
|
|
@ -1062,58 +1062,6 @@ make_source_pathname(PyObject *path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Read a code object from a file and check it for validity */
|
|
||||||
|
|
||||||
static PyCodeObject *
|
|
||||||
read_compiled_module(PyObject *cpathname, FILE *fp)
|
|
||||||
{
|
|
||||||
PyObject *co;
|
|
||||||
|
|
||||||
co = PyMarshal_ReadLastObjectFromFile(fp);
|
|
||||||
if (co == NULL)
|
|
||||||
return NULL;
|
|
||||||
if (!PyCode_Check(co)) {
|
|
||||||
PyErr_Format(PyExc_ImportError,
|
|
||||||
"Non-code object in %R", cpathname);
|
|
||||||
Py_DECREF(co);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return (PyCodeObject *)co;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* Load a module from a compiled file, execute it, and return its
|
|
||||||
module object WITH INCREMENTED REFERENCE COUNT */
|
|
||||||
|
|
||||||
static PyObject *
|
|
||||||
load_compiled_module(PyObject *name, PyObject *cpathname, FILE *fp)
|
|
||||||
{
|
|
||||||
long magic;
|
|
||||||
PyCodeObject *co;
|
|
||||||
PyObject *m;
|
|
||||||
|
|
||||||
magic = PyMarshal_ReadLongFromFile(fp);
|
|
||||||
if (magic != pyc_magic) {
|
|
||||||
PyErr_Format(PyExc_ImportError,
|
|
||||||
"Bad magic number in %R", cpathname);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
/* Skip mtime and size */
|
|
||||||
(void) PyMarshal_ReadLongFromFile(fp);
|
|
||||||
(void) PyMarshal_ReadLongFromFile(fp);
|
|
||||||
co = read_compiled_module(cpathname, fp);
|
|
||||||
if (co == NULL)
|
|
||||||
return NULL;
|
|
||||||
if (Py_VerboseFlag)
|
|
||||||
PySys_FormatStderr("import %U # precompiled from %R\n",
|
|
||||||
name, cpathname);
|
|
||||||
m = PyImport_ExecCodeModuleObject(name, (PyObject *)co,
|
|
||||||
cpathname, cpathname);
|
|
||||||
Py_DECREF(co);
|
|
||||||
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
|
update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
|
||||||
{
|
{
|
||||||
|
@ -3010,29 +2958,6 @@ get_file(PyObject *pathname, PyObject *fob, char *mode)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
|
||||||
imp_load_compiled(PyObject *self, PyObject *args)
|
|
||||||
{
|
|
||||||
PyObject *name, *pathname;
|
|
||||||
PyObject *fob = NULL;
|
|
||||||
PyObject *m;
|
|
||||||
FILE *fp;
|
|
||||||
if (!PyArg_ParseTuple(args, "UO&|O:load_compiled",
|
|
||||||
&name,
|
|
||||||
PyUnicode_FSDecoder, &pathname,
|
|
||||||
&fob))
|
|
||||||
return NULL;
|
|
||||||
fp = get_file(pathname, fob, "rb");
|
|
||||||
if (fp == NULL) {
|
|
||||||
Py_DECREF(pathname);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
m = load_compiled_module(name, pathname, fp);
|
|
||||||
fclose(fp);
|
|
||||||
Py_DECREF(pathname);
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef HAVE_DYNAMIC_LOADING
|
#ifdef HAVE_DYNAMIC_LOADING
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
|
@ -3209,7 +3134,6 @@ static PyMethodDef imp_methods[] = {
|
||||||
{"init_frozen", imp_init_frozen, METH_VARARGS},
|
{"init_frozen", imp_init_frozen, METH_VARARGS},
|
||||||
{"is_builtin", imp_is_builtin, METH_VARARGS},
|
{"is_builtin", imp_is_builtin, METH_VARARGS},
|
||||||
{"is_frozen", imp_is_frozen, METH_VARARGS},
|
{"is_frozen", imp_is_frozen, METH_VARARGS},
|
||||||
{"load_compiled", imp_load_compiled, METH_VARARGS},
|
|
||||||
#ifdef HAVE_DYNAMIC_LOADING
|
#ifdef HAVE_DYNAMIC_LOADING
|
||||||
{"load_dynamic", imp_load_dynamic, METH_VARARGS},
|
{"load_dynamic", imp_load_dynamic, METH_VARARGS},
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue