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

Doctest module does not work with zipped packages #48447

Closed
abalkin opened this issue Oct 24, 2008 · 14 comments
Closed

Doctest module does not work with zipped packages #48447

abalkin opened this issue Oct 24, 2008 · 14 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@abalkin
Copy link
Member

abalkin commented Oct 24, 2008

BPO 4197
Nosy @loewis, @ncoghlan, @abalkin
Files
  • testmodule.zip: Zip file with a module containing doctest inside
  • doctest.patch
  • doctest-1.patch
  • 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 2008-12-15.12:09:35.693>
    created_at = <Date 2008-10-24.21:59:36.720>
    labels = ['library', 'type-crash']
    title = 'Doctest module does not work with zipped packages'
    updated_at = <Date 2008-12-15.12:09:35.692>
    user = 'https://github.com/abalkin'

    bugs.python.org fields:

    activity = <Date 2008-12-15.12:09:35.692>
    actor = 'ncoghlan'
    assignee = 'ncoghlan'
    closed = True
    closed_date = <Date 2008-12-15.12:09:35.693>
    closer = 'ncoghlan'
    components = ['Library (Lib)']
    creation = <Date 2008-10-24.21:59:36.720>
    creator = 'belopolsky'
    dependencies = []
    files = ['11877', '11878', '11879']
    hgrepos = []
    issue_num = 4197
    keywords = ['patch']
    message_count = 14.0
    messages = ['75186', '75188', '75189', '75191', '76837', '76860', '76872', '77511', '77783', '77791', '77805', '77857', '77858', '77863']
    nosy_count = 3.0
    nosy_names = ['loewis', 'ncoghlan', 'belopolsky']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = 'crash'
    url = 'https://bugs.python.org/issue4197'
    versions = ['Python 2.6', 'Python 2.7']

    @abalkin
    Copy link
    Member Author

    abalkin commented Oct 24, 2008

    With attached teestmodule.zip and svn revision 67006,

    $ ./python.exe testmodule.zip 
    Traceback (most recent call last):
      File "/Users/sasha/Work/python-svn/trunk/Lib/runpy.py", line 121, in 
    _run_module_as_main
        "__main__", fname, loader, pkg_name)
      File "/Users/sasha/Work/python-svn/trunk/Lib/runpy.py", line 34, in 
    _run_code
        exec code in run_globals
      File "testmodule.zip/__main__.py", line 7, in <module>
      File "/Users/sasha/Work/python-svn/trunk/Lib/doctest.py", line 1817, 
    in testmod
        for test in finder.find(m, name, globs=globs, 
    extraglobs=extraglobs):
      File "/Users/sasha/Work/python-svn/trunk/Lib/doctest.py", line 842, in 
    find
        self._find(tests, obj, name, module, source_lines, globs, {})
      File "/Users/sasha/Work/python-svn/trunk/Lib/doctest.py", line 884, in 
    _find
        test = self._get_test(obj, name, module, globs, source_lines)
      File "/Users/sasha/Work/python-svn/trunk/Lib/doctest.py", line 965, in 
    _get_test
        if filename[-4:] in (".pyc", ".pyo"):
    TypeError: 'NoneType' object is unsubscriptable

    @abalkin abalkin added stdlib Python modules in the Lib dir type-crash A hard crash of the interpreter, possibly with a core dump labels Oct 24, 2008
    @abalkin
    Copy link
    Member Author

    abalkin commented Oct 24, 2008

    Attached patch addresses the crash, but two issues remain:

    $ ./python.exe testmodule.zip 
    **********************************************************************
    File "__main__", 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.

    The line number for the failed example cannot be found and file name is
    reported as __main__.c instead of __main__.py

    @abalkin
    Copy link
    Member Author

    abalkin commented Oct 24, 2008

    The next patch, doctest-1.patch addresses the line number issue, but the
    file name is still wrong.

    $ ./python.exe testmodule.zip 
    **********************************************************************
    File "__main__", line 2, 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.

    @abalkin
    Copy link
    Member Author

    abalkin commented Oct 24, 2008

    I don't think there is an easy way to fix source file name reporting
    without injecting __file__ = '<path to archive>/<filename>' into the
    module namespace is zipimporter, but I don't know if any code relies on
    __file__ set to None.

    Setting __file__ to (in the test case) testmodule.zip/main.py will
    eliminate the need for the first patch and the second patch will only need
    to pass globs to linecache.getlines() to fix the line number issue.

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

    ncoghlan commented Dec 3, 2008

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

    @abalkin
    Copy link
    Member Author

    abalkin commented Dec 4, 2008

    I created bpo-4512 to implement get_filename extension in zipimport.
    Will post a patch shortly.

    @abalkin
    Copy link
    Member Author

    abalkin commented Dec 4, 2008

    While the patch at bpo-4512 fixes the crash, it does not fix the line
    number issue. I would prefer to start with doctest-1.patch like solution
    because changing the PEP-302 loader protocol by adding get_filename needs
    additional discussion and certainly is not a candidate for 2.5.x or 2.6.x
    bug fix releases.

    @loewis
    Copy link
    Mannequin

    loewis mannequin commented Dec 10, 2008

    The precise solution still seems to be debated, so this is out of scope
    for 2.5.3.

    @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

    Final revisions for fix:
    2.7 = r67751 (there was a new test file missing from the initial checkin)
    2.6 = r67752
    3.1 = r67753
    3.0 = r67754

    @abalkin
    Copy link
    Member Author

    abalkin commented Dec 14, 2008

    Nick,

    Your commit does not fix the line number reporting from doctest failures:

    $ ./python.exe testmodule.zip
    **********************************************************************
    File "testmodule.zip/__main__.py", line ?, in __main__.c
    ...

    Note the '?' in the output. Did you miss the doctest.py changes in
    doctest-1.patch, or should I open a separate issue for the line number
    bug?

    There is also a problem of __main__.c which my patch does not address,
    but it is less important than the line number.

    On Sun, Dec 14, 2008 at 7:11 AM, Nick Coghlan <report@bugs.python.org> wrote:

    Nick Coghlan <ncoghlan@gmail.com> added the comment:

    Final revisions for fix:
    2.7 = r67751 (there was a new test file missing from the initial checkin)
    2.6 = r67752
    3.1 = r67753
    3.0 = r67754

    ----------
    resolution: -> fixed
    status: open -> closed


    Python tracker <report@bugs.python.org>
    <http://bugs.python.org/issue4197\>


    @ncoghlan
    Copy link
    Contributor

    Re-opening, pending development of a fully passing test specifically for
    the __main__.py in zipfile case.

    @ncoghlan ncoghlan reopened this Dec 15, 2008
    @ncoghlan
    Copy link
    Contributor

    I have a test case locally that demonstrates both the missing line
    number in the __main__.py from a zipfile case, as well as the display of
    main.c as the filename (the latter is not specific to the zipfile case -
    it also happens for a normal main module).

    It still seems to me that there is something going wrong at a deeper
    level for doctest to be getting the "in main.c" part wrong. I want to
    get to the bottom of that before working around the problem at the line
    number lookup stage.

    @ncoghlan
    Copy link
    Contributor

    It turns out you were pretty close to pinpointing the problem in
    doctest, but didn't quite manage to identify which step was going wrong.
    The problem was actually that even after __file__ was being set
    correctly, the call to linecache.getlines wasn't being given the
    __loader__ access it needed in order to retrieve the source code from
    inside the zipfile.

    The __main__.c thing turned out just to be a red herring caused by the
    choice of class name in the test code (I changed it to be __main__.Test
    for the checked in unit test).

    Checkin versions:
    2.7 = r67790
    2.6 = r67791
    3.1 = r67792
    3.0 = r67793

    @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
    stdlib Python modules in the Lib dir type-crash A hard crash of the interpreter, possibly with a core dump
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants