[Python-checkins] r53281 - sandbox/trunk/import_in_py/importer.py
brett.cannon
python-checkins at python.org
Fri Jan 5 23:24:18 CET 2007
Author: brett.cannon
Date: Fri Jan 5 23:24:17 2007
New Revision: 53281
Modified:
sandbox/trunk/import_in_py/importer.py
Log:
Very minor code cleanup.
Modified: sandbox/trunk/import_in_py/importer.py
==============================================================================
--- sandbox/trunk/import_in_py/importer.py (original)
+++ sandbox/trunk/import_in_py/importer.py Fri Jan 5 23:24:17 2007
@@ -213,8 +213,7 @@
if os.path.isdir(absolute_path):
return FileSystemImporter(absolute_path, *self.handlers)
else:
- raise ImportError("can only handle directory entries from "
- "sys.path")
+ raise ImportError("can only handle directories")
class FileSystemImporter(object):
@@ -244,16 +243,19 @@
# files for __init__?
init_filename = '__init__' + file_ext
package_init = os.path.join(package_directory, init_filename)
+ # Check if it is a package with an __init__ file.
if (os.path.isfile(package_init) and
_case_ok(self.path_entry, tail_module) and
_case_ok(package_directory, init_filename)):
return self.loader(package_init, handler, package_directory)
+ # See if it is a module.
file_name = tail_module + file_ext
file_path = os.path.join(self.path_entry, file_name)
if (os.path.isfile(file_path) and
_case_ok(self.path_entry, file_name)):
return self.loader(file_path, handler)
else:
+ # Raise a warning if it matches a directory w/o an __init__ file.
if (os.path.isdir(package_directory) and
_case_ok(self.path_entry, tail_module)):
warnings.warn("Not importing directory %s: missing __init__.py"
@@ -286,11 +288,13 @@
try:
module = self.handler.handle_code(self, fullname,
self.file_path, self.package)
+ # Don't leave any partially initialised modules behind.
except:
if fullname in sys.modules:
del sys.modules[fullname]
raise
- return module
+ else:
+ return module
def mod_time(self, path):
"""Return the modification time for the specified path as an integer."""
@@ -304,7 +308,8 @@
def create_path(self, base_path, type_, must_exist=False):
"""Create a new path based on a base path and requested path type.
- If must_exist is True, the path must already exist.
+ If must_exist is True, the path must already exist in order to return a
+ path instead of None.
"""
path = base_path + type_
@@ -320,7 +325,11 @@
return data
def write_data(self, data, path, binary=False):
- """Write data to a specified path as either binary or textual data."""
+ """Write data to a specified path as either binary or textual data.
+
+ If the path cannot be accessed, then exit silently.
+
+ """
try:
with open(path, 'wb' if binary else 'w') as data_file:
data_file.write(data)
@@ -492,7 +501,7 @@
loader.write_data(pyc, bytecode_path, True)
exec code in module.__dict__
return module
-
+
class ExtensionFileHandler(object):
@@ -744,7 +753,7 @@
# When fromlist has a value and the imported module is a
# package, then if a name in fromlist is not found as an
# attribute on module, try a relative import to find it.
- # Failure is fine and is the exception is suppressed.
+ # Failure is fine and the exception is suppressed.
check_for = list(fromlist)
if '*' in check_for and hasattr(module, '__all__'):
check_for.extend(module.__all__)
@@ -827,6 +836,7 @@
# This call will also handle setting the attribute on the
# package.
self._import_full_module(imported_name)
+ # Absolute module import of a top-level module.
else:
imported_name = name
self._import_full_module(name)
More information about the Python-checkins
mailing list