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

Skip to content

Commit 64befe9

Browse files
committed
Issue #13959: Re-implement imp.load_compiled() in imp.py.
1 parent 273323c commit 64befe9

2 files changed

Lines changed: 20 additions & 81 deletions

File tree

Lib/imp.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
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, NullImporter,
17+
from _imp import (find_module, NullImporter,
1818
SEARCH_ERROR, PY_SOURCE, PY_COMPILED, C_EXTENSION,
1919
PY_RESOURCE, PKG_DIRECTORY, C_BUILTIN, PY_FROZEN,
2020
PY_CODERESOURCE, IMP_HOOK)
@@ -25,17 +25,17 @@
2525
import os
2626

2727

28-
class _LoadSourceCompatibility(_bootstrap._SourceFileLoader):
28+
class _HackedGetData:
2929

30-
"""Compatibility support for implementing load_source()."""
30+
"""Compatibiilty support for 'file' arguments of various load_*()
31+
functions."""
3132

3233
def __init__(self, fullname, path, file=None):
3334
super().__init__(fullname, path)
3435
self.file = file
3536

3637
def get_data(self, path):
37-
"""Gross hack to contort SourceFileLoader to deal w/ load_source()'s bad
38-
API."""
38+
"""Gross hack to contort loader to deal w/ load_*()'s bad API."""
3939
if self.file and path == self._path:
4040
with self.file:
4141
# Technically should be returning bytes, but
@@ -48,10 +48,25 @@ def get_data(self, path):
4848
return super().get_data(path)
4949

5050

51+
class _LoadSourceCompatibility(_HackedGetData, _bootstrap._SourceFileLoader):
52+
53+
"""Compatibility support for implementing load_source()."""
54+
55+
5156
def load_source(name, pathname, file=None):
5257
return _LoadSourceCompatibility(name, pathname, file).load_module(name)
5358

5459

60+
class _LoadCompiledCompatibility(_HackedGetData,
61+
_bootstrap._SourcelessFileLoader):
62+
63+
"""Compatibility support for implementing load_compiled()."""
64+
65+
66+
def load_compiled(name, pathname, file=None):
67+
return _LoadCompiledCompatibility(name, pathname, file).load_module(name)
68+
69+
5570
def load_package(name, path):
5671
if os.path.isdir(path):
5772
extensions = _bootstrap._suffix_list(PY_SOURCE)

Python/import.c

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,58 +1062,6 @@ make_source_pathname(PyObject *path)
10621062
}
10631063

10641064

1065-
/* Read a code object from a file and check it for validity */
1066-
1067-
static PyCodeObject *
1068-
read_compiled_module(PyObject *cpathname, FILE *fp)
1069-
{
1070-
PyObject *co;
1071-
1072-
co = PyMarshal_ReadLastObjectFromFile(fp);
1073-
if (co == NULL)
1074-
return NULL;
1075-
if (!PyCode_Check(co)) {
1076-
PyErr_Format(PyExc_ImportError,
1077-
"Non-code object in %R", cpathname);
1078-
Py_DECREF(co);
1079-
return NULL;
1080-
}
1081-
return (PyCodeObject *)co;
1082-
}
1083-
1084-
1085-
/* Load a module from a compiled file, execute it, and return its
1086-
module object WITH INCREMENTED REFERENCE COUNT */
1087-
1088-
static PyObject *
1089-
load_compiled_module(PyObject *name, PyObject *cpathname, FILE *fp)
1090-
{
1091-
long magic;
1092-
PyCodeObject *co;
1093-
PyObject *m;
1094-
1095-
magic = PyMarshal_ReadLongFromFile(fp);
1096-
if (magic != pyc_magic) {
1097-
PyErr_Format(PyExc_ImportError,
1098-
"Bad magic number in %R", cpathname);
1099-
return NULL;
1100-
}
1101-
/* Skip mtime and size */
1102-
(void) PyMarshal_ReadLongFromFile(fp);
1103-
(void) PyMarshal_ReadLongFromFile(fp);
1104-
co = read_compiled_module(cpathname, fp);
1105-
if (co == NULL)
1106-
return NULL;
1107-
if (Py_VerboseFlag)
1108-
PySys_FormatStderr("import %U # precompiled from %R\n",
1109-
name, cpathname);
1110-
m = PyImport_ExecCodeModuleObject(name, (PyObject *)co,
1111-
cpathname, cpathname);
1112-
Py_DECREF(co);
1113-
1114-
return m;
1115-
}
1116-
11171065
static void
11181066
update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
11191067
{
@@ -3010,29 +2958,6 @@ get_file(PyObject *pathname, PyObject *fob, char *mode)
30102958
}
30112959
}
30122960

3013-
static PyObject *
3014-
imp_load_compiled(PyObject *self, PyObject *args)
3015-
{
3016-
PyObject *name, *pathname;
3017-
PyObject *fob = NULL;
3018-
PyObject *m;
3019-
FILE *fp;
3020-
if (!PyArg_ParseTuple(args, "UO&|O:load_compiled",
3021-
&name,
3022-
PyUnicode_FSDecoder, &pathname,
3023-
&fob))
3024-
return NULL;
3025-
fp = get_file(pathname, fob, "rb");
3026-
if (fp == NULL) {
3027-
Py_DECREF(pathname);
3028-
return NULL;
3029-
}
3030-
m = load_compiled_module(name, pathname, fp);
3031-
fclose(fp);
3032-
Py_DECREF(pathname);
3033-
return m;
3034-
}
3035-
30362961
#ifdef HAVE_DYNAMIC_LOADING
30372962

30382963
static PyObject *
@@ -3209,7 +3134,6 @@ static PyMethodDef imp_methods[] = {
32093134
{"init_frozen", imp_init_frozen, METH_VARARGS},
32103135
{"is_builtin", imp_is_builtin, METH_VARARGS},
32113136
{"is_frozen", imp_is_frozen, METH_VARARGS},
3212-
{"load_compiled", imp_load_compiled, METH_VARARGS},
32133137
#ifdef HAVE_DYNAMIC_LOADING
32143138
{"load_dynamic", imp_load_dynamic, METH_VARARGS},
32153139
#endif

0 commit comments

Comments
 (0)