[Python-checkins] r52393 - sandbox/trunk/import_in_py/importer.py
brett.cannon
python-checkins at python.org
Fri Oct 20 04:08:38 CEST 2006
Author: brett.cannon
Date: Fri Oct 20 04:08:37 2006
New Revision: 52393
Modified:
sandbox/trunk/import_in_py/importer.py
Log:
Add notes about what packages entail.
Also add notes about possible API changes for this code. Starting with
tweaking PEP 302 optional APIs so that most of what PyPycFileHandler can be
gotten from that and some additions and thus be just a function that takes a
loader and uses that to perform its queries.
Modified: sandbox/trunk/import_in_py/importer.py
==============================================================================
--- sandbox/trunk/import_in_py/importer.py (original)
+++ sandbox/trunk/import_in_py/importer.py Fri Oct 20 04:08:37 2006
@@ -16,17 +16,55 @@
* PEP 328: Imports: Multi-line and Absolute/Relative
http://www.python.org/dev/peps/pep-0328
-Clarifications for PEP 302
-==========================
+Clarifications
+==============
* Raise ImportError when load_module() fails to load a module without
raising an exception.
* Is module returned by load_module() actually used for anything?
+
+Package notes
+=============
+* __path__ is set to the deepest package directory, not the top package
+ directory [introspection].
+* __path__ is not inherited in any way [introspection].
+* sys.path_importer_cache has an entry for each package directory that was
+ successfully imported [introspection].
+
+* Packages take precedence over modules with the same name in the same sys.path
+ entry [package essay].
+* __path__ can be a list that must be used for searching other locations
+ [package essay].
+* ``from ... import ...`` first checks that fromlist is from module specified
+ or is a list of modules to import [package essay].
+* ``import ...`` can only be a package or module [package essay].
+* ``from ... import *`` for packages uses __all__ as a list of modules to be
+ imported; if no __all__ is defined then the attributes on the packages are
+ bound in the namespace (including previously imported modules)
+ [package essay].
+* Must make sure that importing submodules later on have the proper attributes
+ for modules with the submodule in its path (e.g., inheriting A.B.C and A.B.D
+ need to have A.B having C and D as attributes) [package essay].
+* Submodules must be imported first (e.g., importing A.B requires A to be
+ imported first) [package essay].
+* __path__ is always initialized to a list with a single item [package essay].
+* __path__ is the directory of the package (a subdirectory of an entry on
+ sys.path) [package essay].
+* Modules in sys.modules with a value of None are for redirection to a
+ top-level module that was imported in a submodule [pacakge essay].
+
+* for modules in packages, __path__ is used instead of sys.path for searching;
+ this means that the usual path_hooks/path_importer_cache dance is done for
+ entries in __path__ instead of sys.path (meta_path is still searched
+ regardless of the value of __path__) [PEP 302].
+
+
Things to be exposed at the Python level
========================================
* Python/marshal.c:r_long()/w_long()
-Py3K
+
+Py3Kk
====
Improvements
------------
@@ -51,7 +89,7 @@
point it is added to initialization.
* Remove any idea of default importer.
+ Removes None entries from sys.path_importer_cache.
- + Rely on default importers being in sys.path_hooks or sys.meta_path.
+ + Rely on default importers being in sys.path_hooks or sys.meta_path.
Rejected Ideas
--------------
@@ -59,6 +97,16 @@
Creating a new module is minimal and loader might want to use a different
type of object.
+
+API Ideas
+=========
+* Move most of API out of handlers and into loaders.
+ + Most of the methods in PyPycFileHandler are to deal with files which is a
+ loader thing.
+ + Basic ideas already there in optional API for loaders from PEP 302, just
+ not enough for .pyc regeneration and verification.
+
+
Use Cases
=========
PTL
@@ -176,6 +224,8 @@
class FrozenImporter(BuiltinFrozenBaseImporter):
"""sys.meta_path class for importing frozen modules."""
+
+ # XXX Frozen packages seem to exist.
_find = imp.is_frozen
_load = imp.init_frozen
@@ -553,6 +603,7 @@
"""
# XXX Packages not supported.
+ # XXX Import lock not used.
def __init__(self, default_path_hook=None,
extended_meta_path=(BuiltinImporter, FrozenImporter)):
@@ -640,7 +691,7 @@
def __call__(self, name, globals={}, locals={}, fromlist=[], level=-1):
"""Import a module.
- 'name' is the dotted name of the module/package to import. 'globals' and
+ 'name' is the dotted name of the module/package to import. 'globals'and
'locals' are the global and local namespace dictionaries of the caller
(only 'globals' is used to introspect the __path__ attribute of the calling
module). fromlist is any specific objects that are to eventually be put
@@ -658,6 +709,15 @@
"""
# XXX Does not handle packages yet, which means no absolute/relative imports
# or fromlist worries.
+
+ # Check for a relative import; if it is one make it absolute to try to import that,
+ # otherwise import as a top-level module.
+ # XXX
+
+ # Import submodules.
+ # XXX
+
+
# Try meta_path entries.
try:
# Attempt to find a loader on sys.meta_path.
@@ -667,4 +727,4 @@
# sys.path. If this fails then module cannot be found.
loader = self.search_sys_path(name)
# A loader was found.
- return loader.load_module(name)
\ No newline at end of file
+ return loader.load_module(name)
More information about the Python-checkins
mailing list