[Python-Dev] PEP 302 support for traceback, inspect, site, warnings, doctest, and linecache

Phillip J. Eby pje at telecommunity.com
Mon Apr 10 19:03:02 CEST 2006

Here's my plan for implementing PEP 302 support (``__loader__`` 
sensitivity) for the above modules:

* Change all of the ``linecache`` API functions (except ``clearcache()``) 
to add an optional ``module_globals`` argument, which they will use to 
obtain the ``__name__`` and ``__loader__`` in the event that the given 
filename doesn't exist, but appears to be the source file for the given 
module.  (That is, if the basename of the filename is consistent with the 
last part of the ``__name__`` in the module_globals, if 
``sys.modules[__name__].__dict__ is module_globals``, and the 
``__loader__`` (if any) in module_globals has a ``get_source()`` method.)

* Change ``inspect.getsourcefile()`` to return the source path as long as 
the object's module has a ``__loader__`` with a ``get_source()`` 
method.  This will ensure that other ``inspect`` functions will still try 
to load the source code for the object in question, even if it's in a 
zipfile or loaded by some other import mechanism.

* Change ``inspect.findsource()`` to pass the target object's module 
globals as an extra argument to ``linecache.getlines()``

* Change the ``traceback`` module to supply frame globals to the 
``linecache`` APIs it uses.

* Change the ``site`` module to not "absolutize" ``__file__`` attributes of 
modules with ``__loader__`` attributes; ``__loader__`` objects are 
responsible for their own ``__file__`` values.  (Actually, if this is 
desirable behavior, the builtin import machinery should be doing it, not 

* Add an optional ``module_globals`` argument to 
``warnings.warn_explicit()``, which will be passed through to 
``linecache.getlines()`` if the warning is not ignored.  (This will prime 
the line cache in case the warning is to be output.)

* Change ``warnings.warn()`` to pass the appropriate globals to 

* Change ``doctest.testfile()`` and ``DocTestSuite`` to use the target 
module's ``__loader__.get_data()`` method (if available) to load the given 
doctest file.

* Change ``__patched_linecache_getlines()`` in ``doctest`` to accept an 
optional ``module_globals`` argument, to be passed through to the original 
``getlines()`` function.

I'm starting work on the above now.  If I finish early enough today, I'll 
follow up with plans for fixing ``pydoc``, which requires slightly more 
extensive surgery, since it doesn't even support packages with multiple 
``__path__`` entries yet, let alone PEP 302.

Please let me know if you have any questions or concerns.

More information about the Python-Dev mailing list