Issue 14710: pkgutil.get_loader and find_loader fail when module is missing (original) (raw)
Created on 2012-05-03 10:45 by Pavel.Aslanov, last changed 2022-04-11 14:57 by admin. This issue is now closed.
Messages (9)
Author: Pavel Aslanov (Pavel.Aslanov)
Date: 2012-05-03 10:45
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
Author: Brett Cannon (brett.cannon) *
Date: 2012-05-03 15:19
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.
Author: Pavel Aslanov (Pavel.Aslanov)
Date: 2012-05-04 04:17
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.
Author: Alyssa Coghlan (ncoghlan) *
Date: 2012-05-04 04:36
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.
Author: Alyssa Coghlan (ncoghlan) *
Date: 2014-03-30 05:58
Update as of Python 3.4: pkgutil.get_loader() still throws AttributeError for this case, but importlib.util.find_spec() returns None as expected.
Author: Pavel Aslanov (Pavel.Aslanov)
Date: 2014-05-19 11:14
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 "", line 1, in 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.
Author: Brett Cannon (brett.cannon) *
Date: 2014-05-19 17:46
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.
Author: Roundup Robot (python-dev)
Date: 2014-05-23 16:32
New changeset 660c82192c69 by Brett Cannon in branch '3.4': Issue #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 #14710 http://hg.python.org/cpython/rev/1adc8eb362f0
Author: Brett Cannon (brett.cannon) *
Date: 2014-05-23 16:33
Thanks for the bug report, Pavel. It turned out pkgutil.find_loader is broken as well as pkgutil.get_loader in different ways.
History
Date
User
Action
Args
2022-04-11 14:57:29
admin
set
github: 58915
2014-05-23 16:33:33
brett.cannon
set
status: open -> closed
messages: +
stage: resolved
2014-05-23 16:32:42
python-dev
set
nosy: + python-dev
messages: +
2014-05-23 16:27:59
brett.cannon
set
versions: - Python 2.7
2014-05-23 16:27:49
brett.cannon
set
title: pkgutil.get_loader is broken -> pkgutil.get_loader and find_loader fail when module is missing
2014-05-19 19:07:59
eric.snow
set
nosy: + eric.snow
2014-05-19 17:46:34
brett.cannon
set
assignee: brett.cannon
messages: +
2014-05-19 11:14:37
Pavel.Aslanov
set
messages: +
2014-03-30 05:58:01
ncoghlan
set
messages: +
versions: + Python 3.4, Python 3.5
2012-05-04 16:38:04
eric.araujo
set
nosy: + eric.araujo
2012-05-04 04:41:48
Arfrever
set
nosy: + Arfrever
2012-05-04 04:36:28
ncoghlan
set
messages: +
2012-05-04 04:17:18
Pavel.Aslanov
set
messages: +
2012-05-03 15:19:27
brett.cannon
set
messages: +
2012-05-03 10:54:44
pitrou
set
nosy: + brett.cannon, ncoghlan
2012-05-03 10:45:40
Pavel.Aslanov
create