[New-bugs-announce] [issue2135] Restructure import.c into PEP 302 importer objects

Douglas Greiman report at bugs.python.org
Mon Feb 18 00:11:19 CET 2008

New submission from Douglas Greiman:

This patch reorganizes import.c to move functionality into two new PEP
302-style Importer objects.  I attempted to change as little as
feasible, but the patch is still ~4700 lines long, about 1000 of which
is new tests.

BuiltinImporter: handles C_BUILTIN and PY_FROZEN modules
DirectoryImporter: handles PY_SOURCE, PY_COMPILED, PKG_DIRECTORY,

BuiltinImporter is put on sys.meta_path, DirectoryImporter is put on

To preserve backward compatibility of methods like imp.find_module(),
they use new variables sys.builtin_meta_path and sys.builtin_path_hooks
which are analogous to sys.meta_path and sys.path_hook but contain only
the two importer objects above.

Character encoding issues were substantial.  The existing code was
somewhat inscrutable in this regard.  The tests disabled in issue 1377
were re-added with more safeguards and harder tests.  It is possible to
import modules with non-ascii names from non-ascii paths.  Tested on
Windows XP and Linux.

Areas for discussion:

Naming: Names of the importer classes, names of variables, etc

sys: I added three variables to sys.  Is there a better alternative?

ModuleInfo: The importers use a somewhat tricky way to store an open
file between calls to importer.find_module() and importer.load_module(),
so that the new code doesn't do any more system calls that the old code.

semantics: There are many import corner cases where the semantics are
unknown or unclear.  

Frozen packages: the __path__ of a frozen package is a string rather
than a list, and it is possible to replace that string to change how
imports inside that package are resolved.  I have attempted to keep that
functionality intact but it's not clear that it's a feature rather than
a bug.

components: Interpreter Core
files: builtin_importer-20080217.diff
messages: 62510
nosy: dgreiman
severity: normal
status: open
title: Restructure import.c into PEP 302 importer objects
type: behavior
versions: Python 3.0
Added file: http://bugs.python.org/file9453/builtin_importer-20080217.diff

Tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list