为什么这个Python程序的输出是这样的?

发布于 2024-09-03 02:45:07 字数 1331 浏览 3 评论 0原文

#python 的某人建议它正在搜索模块“herpaderp”并找到所有列出的搜索模块。如果是这种情况,为什么它不在引发 ImportError 之前列出我系统上的每个模块?有人可以解释一下这里发生的事情吗?

import sys

class TempLoader(object):     
    def __init__(self, path_entry):
        if path_entry == 'test': return
        raise ImportError

    def find_module(self, fullname, path=None):
        print fullname, path
        return None

sys.path.insert(0, 'test')
sys.path_hooks.append(TempLoader)
import herpaderp

输出:

16:00:55 $> python wtf.py
herpaderp None
apport None
subprocess None
traceback None
pickle None
struct None
re None
sre_compile None
sre_parse None
sre_constants None
org None
tempfile None
random None
__future__ None
urllib None
string None
socket None
_ssl None
urlparse None
collections None
keyword None
ssl None
textwrap None
base64 None
fnmatch None
glob None
atexit None
xml None
_xmlplus None
copy None
org None
pyexpat None
problem_report None
gzip None
email None
quopri None
uu None
unittest None
ConfigParser None
shutil None
apt None
apt_pkg None
gettext None
locale None
functools None
httplib None
mimetools None
rfc822 None
urllib2 None
hashlib None
_hashlib None
bisect None
Traceback (most recent call last):
  File "wtf.py", line 14, in <module>
    import herpaderp
ImportError: No module named herpaderp

Someone from #python suggested that it's searching for module "herpaderp" and finding all the ones listed as its searching. If this is the case, why doesn't it list every module on my system before raising ImportError? Can someone shed some light on what's happening here?

import sys

class TempLoader(object):     
    def __init__(self, path_entry):
        if path_entry == 'test': return
        raise ImportError

    def find_module(self, fullname, path=None):
        print fullname, path
        return None

sys.path.insert(0, 'test')
sys.path_hooks.append(TempLoader)
import herpaderp

output:

16:00:55 
gt; python wtf.py
herpaderp None
apport None
subprocess None
traceback None
pickle None
struct None
re None
sre_compile None
sre_parse None
sre_constants None
org None
tempfile None
random None
__future__ None
urllib None
string None
socket None
_ssl None
urlparse None
collections None
keyword None
ssl None
textwrap None
base64 None
fnmatch None
glob None
atexit None
xml None
_xmlplus None
copy None
org None
pyexpat None
problem_report None
gzip None
email None
quopri None
uu None
unittest None
ConfigParser None
shutil None
apt None
apt_pkg None
gettext None
locale None
functools None
httplib None
mimetools None
rfc822 None
urllib2 None
hashlib None
_hashlib None
bisect None
Traceback (most recent call last):
  File "wtf.py", line 14, in <module>
    import herpaderp
ImportError: No module named herpaderp

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沫离伤花 2024-09-10 02:45:07

看起来这就是发生的事情:

http://rhodesmill.org/brandon/ 2010/ubuntu-exception-190-modules/

基本上,apport 模块(不是标准库的一部分)在非常低的级别上与异常相关联,例如在将异常写入标准输出之前。因此,当程序无法找到“herpaderp”时,它会抛出异常并触发 apport 及其包含的所有模块的导入,并在异常之前将它们显示在输出中。

解决方案?我已经删除了
“python-apport”包,以及
“ubuntuone-client”套件
取决于它。卸载后,
例外是——奇妙的是——
不会导致新的单次导入
模块!现在我终于可以继续了
平静地编写我的导入钩子。

Looks like this is what's happening:

http://rhodesmill.org/brandon/2010/ubuntu-exception-190-modules/

Basically, the apport module (not part of the standard lib) gets tied in at a really low level to exceptions, like, before the exception is written to the stdout. So when the program fails to find "herpaderp", it throws an exception and triggers the import of apport and all the modules it contains and displays them in the output before the exception.

The solution? I have removed the
“python-apport” package, along with
the “ubuntuone-client” suite that
depends on it. After the uninstall,
exceptions are — wonderfully enough —
not causing a single import of a new
module! Now, finally, I can continue
writing my import hook in peace.

三人与歌 2024-09-10 02:45:07

对于为什么会发生这种情况,没有一个好的答案,但我测试的两个操作系统之间存在差异。

2.5.1& Windows 上的 2.6.4:

E:\work\python>python wtf.py
herpaderp None
Traceback (most recent call last):
  File "wtf.py", line 14, in <module>
    import herpaderp
ImportError: No module named herpaderp

E:\work\python>python --version
Python 2.5.1

Linux 上的 2.5.2:

$ python wtf.py
herpaderp None
apport None
subprocess None
... etc etc
_locale None
operator None
shutil None
Traceback (most recent call last):
  File "wtf.py", line 14, in <module>
    import herpaderp
ImportError: No module named herpaderp
$ python --version
Python 2.5.2

抱歉,这不是真正的答案,但评论有点太长了!

Don't have a good answer as to why this is happening, but it differs between the two OSs I tested it on.

2.5.1 & 2.6.4 on Windows:

E:\work\python>python wtf.py
herpaderp None
Traceback (most recent call last):
  File "wtf.py", line 14, in <module>
    import herpaderp
ImportError: No module named herpaderp

E:\work\python>python --version
Python 2.5.1

2.5.2 on Linux:

$ python wtf.py
herpaderp None
apport None
subprocess None
... etc etc
_locale None
operator None
shutil None
Traceback (most recent call last):
  File "wtf.py", line 14, in <module>
    import herpaderp
ImportError: No module named herpaderp
$ python --version
Python 2.5.2

Sorry, that's not really an answer, but a bit too long to go in a comment!

断桥再见 2024-09-10 02:45:07

???? that’s part of a pupy python module imho, it’s for process injection. It won’t work without a proper rpyc session which can supply the necessary modules as packaged messages which it unpacks and loads dynamically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文