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

Barry Warsaw barry at python.org
Fri Apr 9 22:24:10 CEST 2010


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-dev/attachments/20100409/123e4c0d/attachment.pgp>


More information about the Python-Dev mailing list