为什么这个Python程序的输出是这样的?
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来这就是发生的事情:
http://rhodesmill.org/brandon/ 2010/ubuntu-exception-190-modules/
基本上,apport 模块(不是标准库的一部分)在非常低的级别上与异常相关联,例如在将异常写入标准输出之前。因此,当程序无法找到“herpaderp”时,它会抛出异常并触发 apport 及其包含的所有模块的导入,并在异常之前将它们显示在输出中。
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.
对于为什么会发生这种情况,没有一个好的答案,但我测试的两个操作系统之间存在差异。
2.5.1& Windows 上的 2.6.4:
Linux 上的 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:
2.5.2 on Linux:
Sorry, that's not really an answer, but a bit too long to go in a comment!
???? 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.