[Python-checkins] r68791 - in python/branches/py3k: Doc/library/importlib.rst Doc/library/modules.rst Lib/importlib/NOTES Lib/importlib/__init__.py Misc/NEWS

brett.cannon python-checkins at python.org
Tue Jan 20 03:21:28 CET 2009


Author: brett.cannon
Date: Tue Jan 20 03:21:27 2009
New Revision: 68791

Log:
Document the (very small) public API for importlib. As time goes on and some
key refactorings occur more of the API will be exposed and documented.


Added:
   python/branches/py3k/Doc/library/importlib.rst
Modified:
   python/branches/py3k/Doc/library/modules.rst
   python/branches/py3k/Lib/importlib/NOTES
   python/branches/py3k/Lib/importlib/__init__.py
   python/branches/py3k/Misc/NEWS

Added: python/branches/py3k/Doc/library/importlib.rst
==============================================================================
--- (empty file)
+++ python/branches/py3k/Doc/library/importlib.rst	Tue Jan 20 03:21:27 2009
@@ -0,0 +1,78 @@
+:mod:`importlib` -- An implementation of :keyword:`import`
+==========================================================
+
+.. module:: importlib
+   :synopsis: An implementation of the import machinery.
+
+.. moduleauthor:: Brett Cannon <brett at python.org>
+.. sectionauthor:: Brett Cannon <brett at python.org>
+
+.. versionadded:: 3.1
+
+
+Introduction
+------------
+
+The purpose of the :mod:`importlib` package is two-fold. One is to provide an
+implementation of the :keyword:`import` statement (and thus, by extension, the
+:func:`__import__` function) in Python source code. This provides an
+implementaiton of :keyword:`import` which is portable to any Python
+interpreter. This also provides a reference implementation which is easier to
+read than one in a programming language other than Python.
+
+Two, the components to implement :keyword:`import` can be exposed in this
+package, making it easier for users to create their own custom objects (known
+generically as importers) to participate in the import process. Details on
+providing custom importers can be found in :pep:`302`.
+
+.. seealso::
+
+    :ref:`import`
+        The language reference for the :keyword:`import` statement.
+
+    `Packages specification <http://www.python.org/doc/essays/packages.html>`__
+        Original specification of packages. Some semantics have changed since
+        the writing of this document (e.g. redirecting based on :keyword:`None`
+        in :data:`sys.modules`).
+
+    The :func:`.__import__` function
+        The built-in function for which the :keyword:`import` statement is
+        syntactic sugar for.
+
+    :pep:`235`
+        Import on Case-Insensitive Platforms
+
+    :pep:`263`
+        Defining Python Source Code Encodings
+
+    :pep:`302`
+        New Import Hooks.
+
+    :pep:`328`
+        Imports: Multi-Line and Absolute/Relative
+
+    :pep:`366`
+        Main module explicit relative imports
+
+    :pep:`3128`
+        Using UTF-8 as the Default Source Encoding
+
+
+Functions
+---------
+
+.. function:: __import__(name, globals={}, locals={}, fromlist=\[\], level=0)
+
+    An implementation of the built-in :func:`__import__` function. See the
+    built-in function's documentation for usage instructions.
+
+.. function:: import_module(name, package=None)
+
+    Import a module. The ``name`` argument specifies what module to
+    import in absolute or relative terms
+    (e.g. either ``pkg.mod`` or ``..mod``). If the name is
+    specified in relative terms, then the ``package`` argument must be
+    specified to the package which is to act as the anchor for resolving the
+    package name (e.g. ``import_module('..mod', 'pkg.subpkg')`` will import
+    ``pkg.mod``). The specified module will be inserted into
+    :data:`sys.modules` and returned.

Modified: python/branches/py3k/Doc/library/modules.rst
==============================================================================
--- python/branches/py3k/Doc/library/modules.rst	(original)
+++ python/branches/py3k/Doc/library/modules.rst	Tue Jan 20 03:21:27 2009
@@ -18,3 +18,4 @@
    pkgutil.rst
    modulefinder.rst
    runpy.rst
+   importlib.rst

Modified: python/branches/py3k/Lib/importlib/NOTES
==============================================================================
--- python/branches/py3k/Lib/importlib/NOTES	(original)
+++ python/branches/py3k/Lib/importlib/NOTES	Tue Jan 20 03:21:27 2009
@@ -1,12 +1,11 @@
 to do
 /////
 
-* Write importlib.__import__
+* Expose resolve_name().
 
-* Document
-    + Package.
+* Backport to Python 2.7.
     + import_module
-    + __import__
+    + resolve_name
 
 * Create reasonable base tests that all finders and loaders must pass so
   that various implementations can just subclass as needed.
@@ -42,7 +41,7 @@
     - Absolute name from sys.path.
     - Relative name from sys.path.
 
-* Public API (w/ docs!)
+* Public API to expose (w/ docs!)
   + abc
       - Finder
         * find_module
@@ -72,9 +71,5 @@
       - Source/bytecode importers
           * SourceFinder
           * (?) Loader
-  + __init__
-      - __import__
-      - import_module (backport to 2.7)
-      - resolve_name (backport to 2.7)
 
 * Bootstrap importlib as implementation of builtins.__import__

Modified: python/branches/py3k/Lib/importlib/__init__.py
==============================================================================
--- python/branches/py3k/Lib/importlib/__init__.py	(original)
+++ python/branches/py3k/Lib/importlib/__init__.py	Tue Jan 20 03:21:27 2009
@@ -79,27 +79,6 @@
     return x
 
 
-def import_module(name, package=None):
-    """Import a module.
-
-    The 'package' argument is used to resolve relative import names. Typically
-    this is the __package__ attribute of the module making the function call.
-
-    """
-    if name.startswith('.'):
-        if not package:
-            raise TypeError("relative imports require the 'package' argument")
-        level = 0
-        for character in name:
-            if character != '.':
-                break
-            level += 1
-        name = Import._resolve_name(name[level:], package, level)
-    __import__(name)
-    return sys.modules[name]
-
-
-
 # Required built-in modules.
 try:
     import posix as _os
@@ -130,4 +109,30 @@
 marshal._w_long = _w_long
 marshal._r_long = _r_long
 
+
+__import__ = _bootstrap.Import().__call__
+
+
+def import_module(name, package=None):
+    """Import a module.
+
+    The 'package' argument is required when performing a relative import. It
+    specifies the package to use as the anchor point from which to resolve the
+    relative import to an absolute import.
+
+    """
+    if name.startswith('.'):
+        if not package:
+            raise TypeError("relative imports require the 'package' argument")
+        level = 0
+        for character in name:
+            if character != '.':
+                break
+            level += 1
+        name = Import._resolve_name(name[level:], package, level)
+    __import__(name)
+    return sys.modules[name]
+
+
+
 from ._bootstrap import *

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Tue Jan 20 03:21:27 2009
@@ -134,6 +134,8 @@
 Library
 -------
 
+- Add the importlib package.
+
 - Issue #4301: Patch the logging module to add processName support, remove
   _check_logger_class from multiprocessing.
 


More information about the Python-checkins mailing list