Can't find thread.py
What does this开发者_运维百科 code mean?
try:
import thread
except ImportError:
del _sys.modules[__name__]#why
raise
But I can't find thread.py.
why,del _sys.modules[__name__]
That code from Python's threading.py
is trying to load the C implementation of the thread
module. If it fails (for some reason this hasn't been compiled, as it would be with all Python distributions) then it deletes itself from the list of modules so that threading
doesn't show up as having been imported.
If you're looking for the source of the thread
module, it's here: http://svn.python.org/projects/python/trunk/Python/thread.c
Must be some custom logic - keeping track of what is imported and where. In regular Python 3.1 this standalone code does not work:
>>> try:
import thread
except:
del _sys.modules[__name__]
raise
Traceback (most recent call last):
File "<pyshell#10>", line 4, in <module>
del _sys.modules[__name__]
NameError: name '_sys' is not defined
>>>
Where did you find this?
精彩评论