[Python-Dev] PEP 3147, __cached__, and PyImport_ExecCodeModuleEx()

Guido van Rossum guido at python.org
Fri Apr 9 23:52:16 CEST 2010


It may be undocumented but it doesn't start with _ and it exists to
preserve backwards compatibility. So I recommend adding
PyImport_ExecCodeModuleExEx().

On Fri, Apr 9, 2010 at 1:24 PM, Barry Warsaw <barry at python.org> wrote:
> I've run into a minor snag implementing the __cached__ attribute on imported
> modules.  From PEP 3147:
>
>    As part of this PEP, we will add an `__cached__` attribute to modules,
>    which will always point to the actual `pyc` file that was read or
>    written.  When the environment variable `$PYTHONDONTWRITEBYTECODE` is
>    set, or the `-B` option is given, or if the source lives on a
>    read-only filesystem, then the `__cached__` attribute will point to
>    the location that the `pyc` file *would* have been written to if it
>    didn't exist.  This location of course includes the `__pycache__`
>    subdirectory in its path.
>
> The right place to add this seems to be PyImport_ExecCodeModuleEx(), which
> passes in a `pathname` argument.  This function is not documented in the C API
> reference manual and about the only place where it's described is
> Misc/HISTORY:
>
>    - New function PyImport_ExecCodeModuleEx(), which extends
>    PyImport_ExecCodeModule() by adding an extra parameter to pass it the
>    true file.
>
> The "true file" can either be the source .py file, the legacy .pyc file, or
> the PEP 3147 .pyc file depending on the circumstances.  The caller knows which
> it is, but that function itself doesn't.  I've tentatively worked out some
> code that lets it guess, but it's a kludge, it's ugly and I don't like it.  I
> think the right fix is to extend PyImport_ExecCodeModuleEx() to also pass in
> the pathname for __cached__ (or NULL if None is appropriate).
>
> It bothers me a little to change this API, but OTOH, it's an *undocumented*
> API, so I don't feel too badly. ;) Since this is one of the last things to
> implement for PEP 3147, I thought I'd ask and see if anybody had any better
> suggestions.
>
> -Barry
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/guido%40python.org
>
>



-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-Dev mailing list