Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get_filename method to zipimport #48762

Closed
abalkin opened this issue Dec 4, 2008 · 6 comments
Closed

Add get_filename method to zipimport #48762

abalkin opened this issue Dec 4, 2008 · 6 comments
Assignees
Labels
extension-modules C modules in the Modules dir type-feature A feature request or enhancement

Comments

@abalkin
Copy link
Member

abalkin commented Dec 4, 2008

BPO 4512
Nosy @ncoghlan, @abalkin
Files
  • zipimport.patch: Patch against revision 67511
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/ncoghlan'
    closed_at = <Date 2009-02-08.03:37:51.395>
    created_at = <Date 2008-12-04.00:43:19.246>
    labels = ['extension-modules', 'type-feature']
    title = 'Add get_filename method to zipimport'
    updated_at = <Date 2009-02-08.03:37:51.394>
    user = 'https://github.com/abalkin'

    bugs.python.org fields:

    activity = <Date 2009-02-08.03:37:51.394>
    actor = 'ncoghlan'
    assignee = 'ncoghlan'
    closed = True
    closed_date = <Date 2009-02-08.03:37:51.395>
    closer = 'ncoghlan'
    components = ['Extension Modules']
    creation = <Date 2008-12-04.00:43:19.246>
    creator = 'belopolsky'
    dependencies = []
    files = ['12214']
    hgrepos = []
    issue_num = 4512
    keywords = ['patch']
    message_count = 6.0
    messages = ['76859', '76863', '77774', '77785', '77786', '81365']
    nosy_count = 2.0
    nosy_names = ['ncoghlan', 'belopolsky']
    pr_nums = []
    priority = 'normal'
    resolution = 'accepted'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue4512'
    versions = ['Python 2.7']

    @abalkin
    Copy link
    Member Author

    abalkin commented Dec 4, 2008

    From bpo-4197:

    """
    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
    (yet).
    """ -- Nick Coghlan

    I am working on a patch.

    @abalkin abalkin added extension-modules C modules in the Modules dir type-feature A feature request or enhancement labels Dec 4, 2008
    @abalkin
    Copy link
    Member Author

    abalkin commented Dec 4, 2008

    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
    <http://mail.python.org/pipermail/python-dev/2006-April/063626.html\>,
    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
    the
    PEP
    302 API.

    It sounds like maybe a new PEP is needed to document all the extensions
    to
    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 bpo-4197:

    $ ./python.exe testmodule.zip 
    **********************************************************************
    File "testmodule.zip/__main__.py", line ?, in __main__.c
    Failed example:
        'line 2'
    Expected nothing
    Got:
        '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.

    @ncoghlan ncoghlan self-assigned this Dec 9, 2008
    @ncoghlan
    Copy link
    Contributor

    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__)

    @ncoghlan
    Copy link
    Contributor

    Fixed for 2.7 in r67750.

    Will be ported to 2.6, 3.0 and 3.1.

    @ncoghlan
    Copy link
    Contributor

    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

    @ncoghlan
    Copy link
    Contributor

    ncoghlan commented Feb 8, 2009

    ZipImporter.get_filename() made public in:
    2.7: r69425
    3.1: r69426

    @ncoghlan ncoghlan closed this as completed Feb 8, 2009
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    extension-modules C modules in the Modules dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants