This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Add get_filename method to zipimport
Type: enhancement Stage: resolved
Components: Extension Modules Versions: Python 2.7
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: ncoghlan Nosy List: belopolsky, ncoghlan
Priority: normal Keywords: patch

Created on 2008-12-04 00:43 by belopolsky, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
zipimport.patch belopolsky, 2008-12-04 01:36 Patch against revision 67511
Messages (6)
msg76859 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2008-12-04 00:43
From issue4197:

runpy needs a non-standard PEP 302 extension to set __file__ correctly
in the modules it runs. The pkgutil stuff it uses to find pure Python
modules in the filesystem supports that extension, but zipimport doesn't
"""  -- Nick Coghlan

I am working on a patch.
msg76863 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2008-12-04 02:01
I have attached a rough patch implementing the get_filename.  I 
deliberately copied code from get_source method that finds the filename 
inside the zip archive  and simply prefixed that with the archive path.

I could not find any detailed discussion of what get_filename is 
supposed to do.  Pkgutil's get_filename is not documented and not unit-
tested. Google search revealed an old thread at 
but it only says


>runpy needs a get_filename() method, so it knows what to set __file__ 
too -
>currently its emulation supports that, but it isn't officially part of 
>302 API.

It sounds like maybe a new PEP is needed to document all the extensions 
the importer/loader protocols.  :(

I don't think a brand new PEP is needed, but an amendment to PEP 302 
would be helpful.  Nick, do you have any notes on what get_filename 
should do in various cases?

My implementation fixes one of the problems in issue4197:

$ ./python.exe 
File "", line ?, in __main__.c
Failed example:
    'line 2'
Expected nothing
    'line 2'
1 items had failures:
   1 of   1 in __main__.c
***Test Failed*** 1 failures.

Note that line number is still reported as '?', but there is no crash.
msg77774 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2008-12-14 01:48
The spec get_filename is pretty simple:

Return whatever __file__ would be set to if load_module() was called for
this module.

(runpy's problem is that it never actually loads the module in question,
so it never gets the chance to interrogate __file__)
msg77785 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2008-12-14 10:57
Fixed for 2.7 in r67750.

Will be ported to 2.6, 3.0 and 3.1.
msg77786 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2008-12-14 10:58
Correction - added as _get_filename() in that revision.

Documenting it and making it public (i.e. removing the underscore) is
still to be done, and will only be done for 2.7/3.1
msg81365 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2009-02-08 03:37
ZipImporter.get_filename() made public in:
2.7: r69425
3.1: r69426
Date User Action Args
2022-04-11 14:56:42adminsetgithub: 48762
2009-02-08 03:37:51ncoghlansetstatus: open -> closed
resolution: accepted
messages: + msg81365
stage: resolved
2008-12-14 10:58:24ncoghlansetmessages: + msg77786
2008-12-14 10:57:21ncoghlansetmessages: + msg77785
2008-12-14 01:48:54ncoghlansetmessages: + msg77774
2008-12-09 09:03:11ncoghlansetassignee: ncoghlan
2008-12-04 02:01:29belopolskysetmessages: + msg76863
2008-12-04 01:36:43belopolskysetfiles: + zipimport.patch
keywords: + patch
2008-12-04 00:45:30belopolskysetnosy: + ncoghlan
2008-12-04 00:43:19belopolskycreate