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

pkgutil.get_loader and find_loader fail when module is missing #58915

Closed
PavelAslanov mannequin opened this issue May 3, 2012 · 9 comments
Closed

pkgutil.get_loader and find_loader fail when module is missing #58915

PavelAslanov mannequin opened this issue May 3, 2012 · 9 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@PavelAslanov
Copy link
Mannequin

PavelAslanov mannequin commented May 3, 2012

BPO 14710
Nosy @brettcannon, @ncoghlan, @merwok, @ericsnowcurrently
Files
  • python_pkgutil_bug.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/brettcannon'
    closed_at = <Date 2014-05-23.16:33:33.263>
    created_at = <Date 2012-05-03.10:45:40.755>
    labels = ['type-bug', 'library']
    title = 'pkgutil.get_loader and find_loader fail when module is missing'
    updated_at = <Date 2014-05-23.16:33:33.262>
    user = 'https://bugs.python.org/PavelAslanov'

    bugs.python.org fields:

    activity = <Date 2014-05-23.16:33:33.262>
    actor = 'brett.cannon'
    assignee = 'brett.cannon'
    closed = True
    closed_date = <Date 2014-05-23.16:33:33.263>
    closer = 'brett.cannon'
    components = ['Library (Lib)']
    creation = <Date 2012-05-03.10:45:40.755>
    creator = 'Pavel.Aslanov'
    dependencies = []
    files = ['25442']
    hgrepos = []
    issue_num = 14710
    keywords = ['patch']
    message_count = 9.0
    messages = ['159848', '159861', '159900', '159901', '215168', '218789', '218803', '218977', '218978']
    nosy_count = 7.0
    nosy_names = ['brett.cannon', 'ncoghlan', 'eric.araujo', 'Arfrever', 'python-dev', 'eric.snow', 'Pavel.Aslanov']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue14710'
    versions = ['Python 3.4', 'Python 3.5']

    @PavelAslanov
    Copy link
    Mannequin Author

    PavelAslanov mannequin commented May 3, 2012

    if module was marked as not existing by setting sys.modules [fullname] to None, then pkgutil.get_loader (fullname) will throw AttributeError.

    Example:
    #! /usr/bin/evn python
    import unittest
    import pkgutil

    def main ():
        pkgutil.get_loader ('unittest.functools')
    
    if __name__ == '__main__':
        main ()

    Patch is attached

    @PavelAslanov PavelAslanov mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels May 3, 2012
    @brettcannon
    Copy link
    Member

    So I'm no pkgutil expert, but at least in the case of None in sys.modules, that triggers at least an ImportError in __import__ if that is come across.

    @PavelAslanov
    Copy link
    Mannequin Author

    PavelAslanov mannequin commented May 4, 2012

    Main use case of pkgutil.get_loader is to determine if its possible to load module and either return loader or None. But it throws exception more over it is AttributeErrror exception.

    @ncoghlan
    Copy link
    Contributor

    ncoghlan commented May 4, 2012

    I'm not yet sure the proposed fix in the patch is the right approach (I need to look at the surrounding code), but I believe Pavel's right that get_loader() should be returning None in this case instead of throwing an exception.

    @ncoghlan
    Copy link
    Contributor

    Update as of Python 3.4: pkgutil.get_loader() still throws AttributeError for this case, but importlib.util.find_spec() returns None as expected.

    @PavelAslanov
    Copy link
    Mannequin Author

    PavelAslanov mannequin commented May 19, 2014

    This function is broken again in version 3.4

    The way it should look is:
    Python 2.7.6 (default, Feb 26 2014, 12:07:17) 
    [GCC 4.8.2 20140206 (prerelease)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import pkgutil
    >>> pkgutil.get_loader('no_such_module') # returns None
    >>> 
    
    How it really looks:
    Python 3.4.0 (default, Apr 27 2014, 23:33:09) 
    [GCC 4.8.2 20140206 (prerelease)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import pkgutil
    >>> pkgutil.get_loader('no_such_module')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python3.4/pkgutil.py", line 467, in get_loader
        return find_loader(fullname)
      File "/usr/lib/python3.4/pkgutil.py", line 488, in find_loader
        return spec.loader
    AttributeError: 'NoneType' object has no attribute 'loader'
    >>>

    find_loader is at fault (change "return spec.loader" -> "return spec and spec.loader"). Thanks.

    @brettcannon
    Copy link
    Member

    I'll take a look the next time I have some Python time (in a week or two) and make sure this gets dealt with.

    @brettcannon brettcannon self-assigned this May 19, 2014
    @brettcannon brettcannon changed the title pkgutil.get_loader is broken pkgutil.get_loader and find_loader fail when module is missing May 23, 2014
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented May 23, 2014

    New changeset 660c82192c69 by Brett Cannon in branch '3.4':
    Issue bpo-14710: Fix both pkgutil.find_loader() and get_loader() to not
    http://hg.python.org/cpython/rev/660c82192c69

    New changeset 1adc8eb362f0 by Brett Cannon in branch 'default':
    Merge for issue bpo-14710
    http://hg.python.org/cpython/rev/1adc8eb362f0

    @brettcannon
    Copy link
    Member

    Thanks for the bug report, Pavel. It turned out pkgutil.find_loader is broken as well as pkgutil.get_loader in different ways.

    @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-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants