[Python-checkins] r52179 - sandbox/trunk/import_in_py/importer.py

brett.cannon python-checkins at python.org
Thu Oct 5 21:16:37 CEST 2006


Author: brett.cannon
Date: Thu Oct 5 21:16:36 2006
New Revision: 52179
Modified:
 sandbox/trunk/import_in_py/importer.py
Log:
Refactor built-in and frozen module importers so as to work off of a common
superclass.
Modified: sandbox/trunk/import_in_py/importer.py
==============================================================================
--- sandbox/trunk/import_in_py/importer.py	(original)
+++ sandbox/trunk/import_in_py/importer.py	Thu Oct 5 21:16:36 2006
@@ -22,38 +22,37 @@
 import sys
 
 
-class BuiltinImporter(object):
+class BuiltinFrozenImporter(object):
+
+ """Base class for meta_path importers for built-in and frozen modules.
+
+ Subclasses must provide the _find and _load methods. Both expect only the
+ name of the modules to import. The methods are expected to be defined on
+ the subclass itself and not on an instance.
 
- """sys.meta_path class for importing built-in modules.
- 
- Use static and classmethods as no purpose is served by having instances of
- this class.
- 
 """
 
 @classmethod
 def find_module(cls, fullname, path=None):
- """See if import is for a built-in module.
+ """See if import is for a built-in or frozen module.
 
- Since built-in modules should never have a path, short-circuit if one
- is specified.
+ Since built-in and frozen modules should never have a path,
+ short-circuit if one is specified.
 
 """
 if path is not None:
 return None
- # Could also check sys.builtin_module_names to see if the import is for
- # a built-in module.
- elif imp.is_builtin(fullname):
+ elif cls._find(fullname):
 return cls
 else:
 return None
 
- @staticmethod
- def load_module(fullname, path=None):
- """Load a built-in module.
+ @classmethod
+ def load_module(cls, fullname, path=None):
+ """Load a built-in or frozen module.
 
- 'imp' code for loading a built-in modules handles the setting of a
- module in sys.modules before initializing the module.
+ 'imp' code for loading a built-in or frozen module handles the setting
+ of a module in sys.modules before initializing the module.
 
 """
 if path is not None:
@@ -61,39 +60,23 @@
 try:
 return sys.modules[fullname]
 except KeyError:
- mod = imp.init_builtin(fullname)
+ mod = cls._load(fullname)
 if not mod:
 raise ImportError("expected built-in module not loaded")
 return mod
 
 
-class FrozenImporter(object):
+class BuiltinImporter(BuiltinFrozenImporter):
 
- """sys.meta_path class for importing frozen modules.
+ """sys.meta_path class for importing built-in modules."""
 
- Based off of BuiltinImporter.
+ _find = imp.is_builtin
+ _load = imp.init_builtin
 
- """
 
- @classmethod
- def find_module(cls, fullname, path=None):
- """Return a loader for frozen modules if the module is a frozen
- module."""
- if path is not None:
- return None
- elif imp.is_frozen(fullname):
- return cls
- else:
- return None
+class FrozenImporter(object):
 
- @staticmethod
- def load_module(fullname, path=None):
- if path is not None:
- raise TypeError("loader does not expect an argument for 'path'")
- try:
- return sys.modules[fullname]
- except KeyError:
- mod = imp.init_frozen(fullname)
- if not mod:
- raise ImportError("expected frozen module not loaded")
- return mod
+ """sys.meta_path class for importing frozen modules."""
+
+ _find = imp.is_frozen
+ _load = imp.init_frozen


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /