[Python-checkins] cpython: Issue #13959: Re-implement imp.source_from_cache() in Lib/imp.py.
brett.cannon
python-checkins at python.org
Sun Apr 22 00:53:24 CEST 2012
http://hg.python.org/cpython/rev/ea46ebba8a0f
changeset: 76451:ea46ebba8a0f
user: Brett Cannon <brett at python.org>
date: Sat Apr 21 18:52:52 2012 -0400
summary:
Issue #13959: Re-implement imp.source_from_cache() in Lib/imp.py.
files:
Lib/imp.py | 26 ++++++++++++++++++-
Lib/importlib/_bootstrap.py | 6 +++-
Python/import.c | 33 ------------------------
Python/importlib.h | Bin
4 files changed, 30 insertions(+), 35 deletions(-)
diff --git a/Lib/imp.py b/Lib/imp.py
--- a/Lib/imp.py
+++ b/Lib/imp.py
@@ -13,7 +13,7 @@
# Could move out of _imp, but not worth the code
from _imp import get_magic
# Can (probably) move to importlib
-from _imp import (get_tag, get_suffixes, source_from_cache)
+from _imp import (get_tag, get_suffixes)
# Should be re-implemented here (and mostly deprecated)
from _imp import (find_module, NullImporter,
SEARCH_ERROR, PY_SOURCE, PY_COMPILED, C_EXTENSION,
@@ -27,6 +27,26 @@
import os
+def source_from_cache(path):
+ """Given the path to a .pyc./.pyo file, return the path to its .py file.
+
+ The .pyc/.pyo file does not need to exist; this simply returns the path to
+ the .py file calculated to correspond to the .pyc/.pyo file. If path does
+ not conform to PEP 3147 format, ValueError will be raised.
+
+ """
+ head, pycache_filename = os.path.split(path)
+ head, pycache = os.path.split(head)
+ if pycache != _bootstrap.PYCACHE:
+ raise ValueError('{} not bottom-level directory in '
+ '{!r}'.format(_bootstrap.PYCACHE, path))
+ if pycache_filename.count('.') != 2:
+ raise ValueError('expected only 2 dots in '
+ '{!r}'.format(pycache_filename))
+ base_filename = pycache_filename.partition('.')[0]
+ return os.path.join(head, base_filename + _bootstrap.SOURCE_SUFFIXES[0])
+
+
class _HackedGetData:
"""Compatibiilty support for 'file' arguments of various load_*()
@@ -55,6 +75,7 @@
"""Compatibility support for implementing load_source()."""
+# XXX deprecate after better API exposed in importlib
def load_source(name, pathname, file=None):
return _LoadSourceCompatibility(name, pathname, file).load_module(name)
@@ -65,10 +86,12 @@
"""Compatibility support for implementing load_compiled()."""
+# XXX deprecate
def load_compiled(name, pathname, file=None):
return _LoadCompiledCompatibility(name, pathname, file).load_module(name)
+# XXX deprecate
def load_package(name, path):
if os.path.isdir(path):
extensions = _bootstrap._suffix_list(PY_SOURCE)
@@ -82,6 +105,7 @@
return _bootstrap._SourceFileLoader(name, path).load_module(name)
+# XXX deprecate
def load_module(name, file, filename, details):
"""Load a module, given information returned by find_module().
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -180,6 +180,8 @@
PYCACHE = '__pycache__'
+SOURCE_SUFFIXES = ['.py'] # _setup() adds .pyw as needed.
+
DEBUG_BYTECODE_SUFFIX = '.pyc'
OPT_BYTECODE_SUFFIX = '.pyo'
BYTECODE_SUFFIX = DEBUG_BYTECODE_SUFFIX if __debug__ else OPT_BYTECODE_SUFFIX
@@ -199,7 +201,7 @@
suffix = DEBUG_BYTECODE_SUFFIX if debug else OPT_BYTECODE_SUFFIX
head, tail = _path_split(path)
base_filename, sep, _ = tail.partition('.')
- filename = '{}{}{}{}'.format(base_filename, sep, _imp.get_tag(), suffix)
+ filename = ''.join([base_filename, sep, _imp.get_tag(), suffix])
return _path_join(head, PYCACHE, filename)
@@ -1195,6 +1197,8 @@
# Constants
setattr(self_module, '_relax_case', _make_relax_case())
setattr(self_module, '_MAGIC_NUMBER', _imp_module.get_magic())
+ if builtin_os == 'nt':
+ SOURCE_SUFFIXES.append('.pyw')
def _install(sys_module, _imp_module):
diff --git a/Python/import.c b/Python/import.c
--- a/Python/import.c
+++ b/Python/import.c
@@ -2926,36 +2926,6 @@
Reload the module. The module must have been successfully imported before.");
-static PyObject *
-imp_source_from_cache(PyObject *self, PyObject *args, PyObject *kws)
-{
- static char *kwlist[] = {"path", NULL};
- PyObject *pathname, *source;
-
- if (!PyArg_ParseTupleAndKeywords(
- args, kws, "O&", kwlist,
- PyUnicode_FSDecoder, &pathname))
- return NULL;
-
- source = make_source_pathname(pathname);
- if (source == NULL) {
- PyErr_Format(PyExc_ValueError, "Not a PEP 3147 pyc path: %R",
- pathname);
- Py_DECREF(pathname);
- return NULL;
- }
- Py_DECREF(pathname);
- return source;
-}
-
-PyDoc_STRVAR(doc_source_from_cache,
-"source_from_cache(path) -> path\n\
-Given the path to a .pyc./.pyo file, return the path to its .py file.\n\
-\n\
-The .pyc/.pyo file does not need to exist; this simply returns the path to\n\
-the .py file calculated to correspond to the .pyc/.pyo file. If path\n\
-does not conform to PEP 3147 format, ValueError will be raised.");
-
/* Doc strings */
PyDoc_STRVAR(doc_imp,
@@ -3007,9 +2977,6 @@
{"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
{"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
{"reload", imp_reload, METH_O, doc_reload},
- {"source_from_cache", (PyCFunction)imp_source_from_cache,
- METH_VARARGS | METH_KEYWORDS, doc_source_from_cache},
- /* The rest are obsolete */
{"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
{"is_frozen_package", imp_is_frozen_package, METH_VARARGS},
{"init_builtin", imp_init_builtin, METH_VARARGS},
diff --git a/Python/importlib.h b/Python/importlib.h
index 754949303d9d603c73365947828a3e2657686063..c369e3c3d370995a3f8e808c1705ffe9037db96c
GIT binary patch
[stripped]
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list