[Python-checkins] r52768 - sandbox/trunk/import_in_py/importer.py sandbox/trunk/import_in_py/test_importer.py
brett.cannon
python-checkins at python.org
Thu Nov 16 23:16:02 CET 2006
Author: brett.cannon
Date: Thu Nov 16 23:16:01 2006
New Revision: 52768
Modified:
sandbox/trunk/import_in_py/importer.py
sandbox/trunk/import_in_py/test_importer.py
Log:
Clear out notes on semantics for things that have been implemented and
implement any easy tests to take something.
Mostly leaves work involving the fromlist.
Modified: sandbox/trunk/import_in_py/importer.py
==============================================================================
--- sandbox/trunk/import_in_py/importer.py (original)
+++ sandbox/trunk/import_in_py/importer.py Thu Nov 16 23:16:01 2006
@@ -26,55 +26,26 @@
or not.
-Package notes
+XXX Semantics
=============
-Semantics
----------
-* __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].
-* When importing pre-requisite modules/packages for a dotted module name,
- calls are handled internally [introspection].
- + sys.modules is checked, then find/load is done.
-
-* 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].
+Packages
+--------
* ``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].
+ + If already an attribute, then everything is fine.
+ + If not an attribute, treat as a relative import and do import.
+ - Newly imported module should end up as an attribute as is needed.
+ + Same treatment for any existence of '*' in fromlist if __all__ defined
+ for the module.
* ``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., importing 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].
-* Only meta_path entries have the 'path' argument for find_module and
- load_module set. path_importer_cache entries (which covers sys.path and
- entries for __path__ for package) do not have 'path' set to anything (but
- still have the full name of the module passed in)
- [pep 302 and introspection(Python/import.c:1278)].
-
-Semantic notes
-==============
-XXX
+Bytecode
+--------
* What to do when magic number and timestamp works, but bytecode is bad?
Modified: sandbox/trunk/import_in_py/test_importer.py
==============================================================================
--- sandbox/trunk/import_in_py/test_importer.py (original)
+++ sandbox/trunk/import_in_py/test_importer.py Thu Nov 16 23:16:01 2006
@@ -274,6 +274,7 @@
sub_pkg = getattr(module, self.sub_pkg_tail_name)
self.failUnlessEqual(sub_pkg.__name__, self.sub_pkg_name)
self.failUnlessEqual(sub_pkg.__file__, self.sub_pkg_init_path)
+ self.failUnlessEqual(sub_pkg.__path__, [self.sub_pkg_path])
self.verify_module(sub_pkg)
if actual_name == self.sub_pkg_module_name:
self.failUnless(hasattr(sub_pkg, self.module_name))
@@ -368,6 +369,16 @@
self.failUnlessEqual(loader.handler, self.handler)
self.failUnlessEqual(loader.package, self.pkg_path)
+ def test_pkg_before_module(self):
+ # Importing a name that is represented as both a package and a module
+ # should give precedence to the package.
+ module_path = os.path.join(self.directory, self.pkg_name + '.' +
+ self.py_ext)
+ with open(module_path, 'w') as test_file:
+ test_file.write("blah")
+ loader = self.importer.find_module(self.pkg_name)
+ self.failUnlessEqual(loader.file_path, self.pkg_init_path)
+
class FileSystemLoaderMockEnv(unittest.TestCase):
@@ -991,6 +1002,7 @@
self.failUnless(search_paths[0] in succeed_importer.path_entries)
self.failUnlessEqual(succeed_importer.find_request,
(module_name, None))
+ self.failUnless(search_paths[0] in sys.path_importer_cache)
self.clear_sys_modules(module_name)
del sys.path_importer_cache[search_paths[0]]
succeed_importer.path_entries = []
More information about the Python-checkins
mailing list