[Python-checkins] r53027 - sandbox/trunk/import_in_py/importer.py sandbox/trunk/import_in_py/test_importer.py
brett.cannon
python-checkins at python.org
Thu Dec 14 00:28:56 CET 2006
Author: brett.cannon
Date: Thu Dec 14 00:28:55 2006
New Revision: 53027
Modified:
sandbox/trunk/import_in_py/importer.py
sandbox/trunk/import_in_py/test_importer.py
Log:
Raise a warning when a directory is found that matches a package name
but does not have an __init__.py file.
Modified: sandbox/trunk/import_in_py/importer.py
==============================================================================
--- sandbox/trunk/import_in_py/importer.py (original)
+++ sandbox/trunk/import_in_py/importer.py Thu Dec 14 00:28:55 2006
@@ -123,6 +123,7 @@
import os
import contextlib
import py_compile
+import warnings
def _set__import__():
@@ -274,26 +275,30 @@
registered.
If the module's name is dotted then only search for the trailing
- module's name on the path entry.
+ module's name on the path entry. An importer is already created
+ for each directory in __path__ for a package.
"""
# XXX Does not worry about case-insensitive filesystems.
+ tail_module = fullname.rsplit('.', 1)[-1]
+ package_directory = os.path.join(self.path_entry, tail_module)
for handler in self.handlers:
for file_ext in handler.handles:
# XXX Backwards-incompatible to use anything but .py/.pyc
# files for __init__?
- tail_module = fullname.rsplit('.', 1)[-1]
- package_entry = os.path.join(self.path_entry, tail_module)
- package_name = os.path.join(package_entry,
+ package_init = os.path.join(package_directory,
'__init__'+file_ext)
- file_path = os.path.join(self.path_entry, package_name)
- if os.path.isfile(file_path):
- return self.loader(file_path, handler, package_entry)
+ if os.path.isfile(package_init):
+ return self.loader(package_init, handler, package_directory)
file_name = tail_module + file_ext
file_path = os.path.join(self.path_entry, file_name)
if os.path.isfile(file_path):
return self.loader(file_path, handler)
else:
+ if os.path.isdir(package_directory):
+ warnings.warn("Not importing directory %s: missing __init__.py"
+ % package_directory,
+ ImportWarning)
return None
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 Dec 14 00:28:55 2006
@@ -15,6 +15,7 @@
import StringIO
import sys
import tempfile
+import warnings
class BuiltinFrozen_Tester(unittest.TestCase):
@@ -385,6 +386,15 @@
self.failUnlessEqual(loader.file_path, self.pkg_init_path)
finally:
os.remove(module_path)
+
+ def test_directory_no_init_warning(self):
+ # If a directory matches for a package name but lacks an __init__.py
+ # file then raise ImportWarning.
+ os.remove(self.pkg_init_path)
+ with test_support.guard_warnings_filter():
+ warnings.simplefilter('error', ImportWarning)
+ self.failUnlessRaises(ImportWarning, self.importer.find_module,
+ self.pkg_name)
class FileSystemLoaderMockEnv(unittest.TestCase):
More information about the Python-checkins
mailing list