Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
64befe93
Commit
64befe93
authored
Apr 17, 2012
by
Brett Cannon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #13959: Re-implement imp.load_compiled() in imp.py.
parent
273323cf
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
81 deletions
+20
-81
Lib/imp.py
Lib/imp.py
+20
-5
Python/import.c
Python/import.c
+0
-76
No files found.
Lib/imp.py
View file @
64befe93
...
@@ -14,7 +14,7 @@ from _imp import (lock_held, acquire_lock, release_lock, reload,
...
@@ -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
...
@@ -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):
...
@@ -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
)
...
...
Python/import.c
View file @
64befe93
...
@@ -1062,58 +1062,6 @@ make_source_pathname(PyObject *path)
...
@@ -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)
...
@@ -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[] = {
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment