[Python-checkins] r70377 - in python/branches/py3k: Doc/library/importlib.rst Lib/importlib/NOTES Lib/importlib/_bootstrap.py Lib/importlib/abc.py Lib/importlib/test/frozen/test_loader.py Python/import.c
brett.cannon
python-checkins at python.org
Sun Mar 15 03:20:16 CET 2009
Author: brett.cannon
Date: Sun Mar 15 03:20:16 2009
New Revision: 70377
Log:
Implement InspectLoader for FrozenImporter.
Modified:
python/branches/py3k/Doc/library/importlib.rst
python/branches/py3k/Lib/importlib/NOTES
python/branches/py3k/Lib/importlib/_bootstrap.py
python/branches/py3k/Lib/importlib/abc.py
python/branches/py3k/Lib/importlib/test/frozen/test_loader.py
python/branches/py3k/Python/import.c
Modified: python/branches/py3k/Doc/library/importlib.rst
==============================================================================
--- python/branches/py3k/Doc/library/importlib.rst (original)
+++ python/branches/py3k/Doc/library/importlib.rst Sun Mar 15 03:20:16 2009
@@ -284,7 +284,8 @@
.. class:: FrozenImporter
An :term:`importer` for frozen modules. This class implements the
- :class:`importlib.abc.Finder` and :class:`importlib.abc.Loader` ABCs.
+ :class:`importlib.abc.Finder` and :class:`importlib.abc.InspectLoader`
+ ABCs.
Only class methods are defined by this class to alleviate the need for
instantiation.
Modified: python/branches/py3k/Lib/importlib/NOTES
==============================================================================
--- python/branches/py3k/Lib/importlib/NOTES (original)
+++ python/branches/py3k/Lib/importlib/NOTES Sun Mar 15 03:20:16 2009
@@ -1,10 +1,6 @@
to do
/////
-* Implement InspectLoader for FrozenImporter.
-
- + Expose function to see if a frozen module is a package.
-
* Make sure that there is documentation *somewhere* fully explaining the
semantics of import that can be referenced from the package's documentation
(even if it is in the package documentation itself, although it might be best
Modified: python/branches/py3k/Lib/importlib/_bootstrap.py
==============================================================================
--- python/branches/py3k/Lib/importlib/_bootstrap.py (original)
+++ python/branches/py3k/Lib/importlib/_bootstrap.py Sun Mar 15 03:20:16 2009
@@ -183,6 +183,16 @@
return wrapper
+def _requires_frozen(fxn):
+ """Decorator to verify the named module is frozen."""
+ def wrapper(self, fullname):
+ if not imp.is_frozen(fullname):
+ raise ImportError("{0} is not a frozen module".format(fullname))
+ return fxn(self, fullname)
+ _wrap(wrapper, fxn)
+ return wrapper
+
+
def _suffix_list(suffix_type):
"""Return a list of file suffixes based on the imp file type."""
return [suffix[0] for suffix in imp.get_suffixes()
@@ -261,10 +271,9 @@
@classmethod
@set_package
@set_loader
+ @_requires_frozen
def load_module(cls, fullname):
"""Load a frozen module."""
- if cls.find_module(fullname) is None:
- raise ImportError("{0} is not a frozen module".format(fullname))
is_reload = fullname in sys.modules
try:
return imp.init_frozen(fullname)
@@ -273,6 +282,24 @@
del sys.modules[fullname]
raise
+ @classmethod
+ @_requires_frozen
+ def get_code(cls, fullname):
+ """Return the code object for the frozen module."""
+ return imp.get_frozen_object(fullname)
+
+ @classmethod
+ @_requires_frozen
+ def get_source(cls, fullname):
+ """Return None as frozen modules do not have source code."""
+ return None
+
+ @classmethod
+ @_requires_frozen
+ def is_package(cls, fullname):
+ """Return if the frozen module is a package."""
+ return imp.is_frozen_package(fullname)
+
class PyLoader:
Modified: python/branches/py3k/Lib/importlib/abc.py
==============================================================================
--- python/branches/py3k/Lib/importlib/abc.py (original)
+++ python/branches/py3k/Lib/importlib/abc.py Sun Mar 15 03:20:16 2009
@@ -14,8 +14,6 @@
"""Abstract method which when implemented should load a module."""
raise NotImplementedError
-Loader.register(machinery.FrozenImporter)
-
class Finder(metaclass=abc.ABCMeta):
@@ -75,6 +73,7 @@
return NotImplementedError
InspectLoader.register(machinery.BuiltinImporter)
+InspectLoader.register(machinery.FrozenImporter)
class PyLoader(_bootstrap.PyLoader, InspectLoader):
Modified: python/branches/py3k/Lib/importlib/test/frozen/test_loader.py
==============================================================================
--- python/branches/py3k/Lib/importlib/test/frozen/test_loader.py (original)
+++ python/branches/py3k/Lib/importlib/test/frozen/test_loader.py Sun Mar 15 03:20:16 2009
@@ -1,4 +1,6 @@
from importlib import machinery
+import imp
+import unittest
from .. import abc
from .. import util
@@ -53,9 +55,41 @@
'_not_real')
+class InspectLoaderTests(unittest.TestCase):
+
+ """Tests for the InspectLoader methods for FrozenImporter."""
+
+ def test_get_code(self):
+ # Make sure that the code object is good.
+ name = '__hello__'
+ code = machinery.FrozenImporter.get_code(name)
+ mod = imp.new_module(name)
+ exec(code, mod.__dict__)
+ self.assert_(hasattr(mod, 'initialized'))
+
+ def test_get_source(self):
+ # Should always return None.
+ result = machinery.FrozenImporter.get_source('__hello__')
+ self.assert_(result is None)
+
+ def test_is_package(self):
+ # Should be able to tell what is a package.
+ test_for = (('__hello__', False), ('__phello__', True),
+ ('__phello__.spam', False))
+ for name, is_package in test_for:
+ result = machinery.FrozenImporter.is_package(name)
+ self.assert_(bool(result) == is_package)
+
+ def test_failure(self):
+ # Raise ImportError for modules that are not frozen.
+ for meth_name in ('get_code', 'get_source', 'is_package'):
+ method = getattr(machinery.FrozenImporter, meth_name)
+ self.assertRaises(ImportError, method, 'importlib')
+
+
def test_main():
from test.support import run_unittest
- run_unittest(LoaderTests)
+ run_unittest(LoaderTests, InspectLoaderTests)
if __name__ == '__main__':
Modified: python/branches/py3k/Python/import.c
==============================================================================
--- python/branches/py3k/Python/import.c (original)
+++ python/branches/py3k/Python/import.c Sun Mar 15 03:20:16 2009
@@ -1951,6 +1951,28 @@
return PyMarshal_ReadObjectFromString((char *)p->code, size);
}
+static PyObject *
+is_frozen_package(char *name)
+{
+ struct _frozen *p = find_frozen(name);
+ int size;
+
+ if (p == NULL) {
+ PyErr_Format(PyExc_ImportError,
+ "No such frozen object named %.200s",
+ name);
+ return NULL;
+ }
+
+ size = p->size;
+
+ if (size < 0)
+ Py_RETURN_TRUE;
+ else
+ Py_RETURN_FALSE;
+}
+
+
/* Initialize a frozen module.
Return 1 for success, 0 if the module is not found, and -1 with
an exception set if the initialization failed.
@@ -2959,6 +2981,16 @@
}
static PyObject *
+imp_is_frozen_package(PyObject *self, PyObject *args)
+{
+ char *name;
+
+ if (!PyArg_ParseTuple(args, "s:is_frozen_package", &name))
+ return NULL;
+ return is_frozen_package(name);
+}
+
+static PyObject *
imp_is_builtin(PyObject *self, PyObject *args)
{
char *name;
@@ -3191,6 +3223,7 @@
{"reload", imp_reload, METH_O, doc_reload},
/* 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},
{"init_frozen", imp_init_frozen, METH_VARARGS},
{"is_builtin", imp_is_builtin, METH_VARARGS},
More information about the Python-checkins
mailing list