[pypy-commit] pypy py3k: Implement imp.source_from_cache()
amauryfa
noreply at buildbot.pypy.org
Thu Dec 22 00:21:08 CET 2011
Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3k
Changeset: r50815:221a0077614d
Date: 2011-12-21 23:00 +0100
http://bitbucket.org/pypy/pypy/changeset/221a0077614d/
Log: Implement imp.source_from_cache() Kind of... we still use the old
.py/pyc scheme.
diff --git a/pypy/module/imp/__init__.py b/pypy/module/imp/__init__.py
--- a/pypy/module/imp/__init__.py
+++ b/pypy/module/imp/__init__.py
@@ -36,6 +36,7 @@
'release_lock': 'interp_imp.release_lock',
'cache_from_source': 'interp_imp.cache_from_source',
+ 'source_from_cache': 'interp_imp.source_from_cache',
}
appleveldefs = {
diff --git a/pypy/module/imp/importing.py b/pypy/module/imp/importing.py
--- a/pypy/module/imp/importing.py
+++ b/pypy/module/imp/importing.py
@@ -866,6 +866,14 @@
"Given the path to a .py file, return the path to its .pyc file."
return pathname + 'c'
+def make_source_pathname(pathname):
+ pos_extension = len(pathname) - 4 # len('.pyc')
+ if pos_extension < 0:
+ raise ValueError("path is too short")
+ if pathname[pos_extension:] != '.pyc':
+ raise ValueError("not a .pyc path name")
+ return pathname[:pos_extension + 3]
+
@jit.dont_look_inside
def load_source_module(space, w_modulename, w_mod, pathname, source,
write_pyc=True):
diff --git a/pypy/module/imp/interp_imp.py b/pypy/module/imp/interp_imp.py
--- a/pypy/module/imp/interp_imp.py
+++ b/pypy/module/imp/interp_imp.py
@@ -182,5 +182,29 @@
importing.getimportlock(space).reinit_lock()
@unwrap_spec(pathname=str)
-def cache_from_source(space, pathname):
+def cache_from_source(space, pathname, w_debug_override=None):
+ """cache_from_source(path, [debug_override]) -> path
+ Given the path to a .py file, return the path to its .pyc/.pyo file.
+
+ The .py file does not need to exist; this simply returns the path to the
+ .pyc/.pyo file calculated as if the .py file were imported. The extension
+ will be .pyc unless __debug__ is not defined, then it will be .pyo.
+
+ If debug_override is not None, then it must be a boolean and is taken as
+ the value of __debug__ instead."""
return space.wrap(importing.make_compiled_pathname(pathname))
+
+ at unwrap_spec(pathname=str)
+def source_from_cache(space, pathname):
+ """source_from_cache(path) -> 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."""
+ try:
+ sourcename = importing.make_source_pathname(pathname)
+ except ValueError:
+ raise operationerrfmt(space.w_ValueError,
+ "Not a PEP 3147 pyc path: %s", pathname)
+ return space.wrap(sourcename)
diff --git a/pypy/module/imp/test/test_import.py b/pypy/module/imp/test/test_import.py
--- a/pypy/module/imp/test/test_import.py
+++ b/pypy/module/imp/test/test_import.py
@@ -579,6 +579,8 @@
def test_cache_from_source(self):
import imp
assert imp.cache_from_source('a/b/c.py') == 'a/b/c.pyc'
+ assert imp.source_from_cache('a/b/c.pyc') == 'a/b/c.py'
+ raises(ValueError, imp.source_from_cache, 'a/b/c.py')
def test_shadow_builtin(self):
if self.runappdirect: skip("hard to test: module is already imported")
More information about the pypy-commit
mailing list