[Python-checkins] r74110 - in python/branches/py3k: Doc/library/importlib.rst Lib/importlib/_bootstrap.py Lib/importlib/abc.py Lib/importlib/test/source/test_abc_loader.py Lib/importlib/test/test_abc.py Misc/NEWS

Nick Coghlan ncoghlan at gmail.com
Mon Jul 20 14:22:41 CEST 2009


brett.cannon wrote:
> Author: brett.cannon
> Date: Mon Jul 20 06:23:48 2009
> New Revision: 74110
> 
> Log:
> Implement the PEP 302 protocol for get_filename() as
> importlib.abc.ExecutionLoader. PyLoader now inherits from this ABC instead of
> InspectLoader directly. Both PyLoader and PyPycLoader provide concrete
> implementations of get_filename in terms of source_path and bytecode_path.
> 
> 
> Modified:
>    python/branches/py3k/Doc/library/importlib.rst
>    python/branches/py3k/Lib/importlib/_bootstrap.py
>    python/branches/py3k/Lib/importlib/abc.py
>    python/branches/py3k/Lib/importlib/test/source/test_abc_loader.py
>    python/branches/py3k/Lib/importlib/test/test_abc.py
>    python/branches/py3k/Misc/NEWS
> 
> Modified: python/branches/py3k/Doc/library/importlib.rst
> ==============================================================================
> --- python/branches/py3k/Doc/library/importlib.rst	(original)
> +++ python/branches/py3k/Doc/library/importlib.rst	Mon Jul 20 06:23:48 2009
> @@ -202,10 +202,24 @@
>          :term:`loader` cannot find the module.
>  
>  
> +.. class:: ExecutionLoader
> +
> +    An abstract base class which inherits from :class:`InspectLoader` that,
> +    when implemented, allows a module to be executed as a script. The ABC
> +    represents an optional :pep:`302` protocol.
> +
> +    .. method:: get_filename(fullname)
> +
> +        An abstract method that is to return the value for :attr:`__file__` for
> +        the specified module. If no path is available, :exc:`ImportError` is
> +        raised.

This isn't quite accurate:

1. Modules that come from a loader without get_filename() can still be
executed via the runpy module. They will just have __file__ set to None
instead of the value that would have been set by load_module() (and if
they were executed with -m then sys.argv[0] will be wrong as well).

2. There's an additional restriction on get_filename(): it isn't allowed
to load the module (otherwise there is no point to the method - if
loading the module was acceptable, then runpy could just do that and
inspect __file__ itself as a fallback when the loader didn't provide
get_filename()).

I have a feeling that I didn't mention point 2 explicitly when I
documented get_filename() in PEP 302 though.

Regards,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------


More information about the Python-checkins mailing list