"""Faux ``threading`` version using ``dummy_thread`` instead of ``thread``.The module ``_dummy_threading`` is added to ``sys.modules`` in orderto not have ``threading`` considered imported. Had ``threading`` beendirectly imported it would have made all subsequent imports succeedregardless of whether ``_thread`` was available which is not desired."""from sys import modules as sys_modulesimport _dummy_thread# Declaring now so as to not have to nest ``try``s to get proper clean-up.holding_thread = Falseholding_threading = Falseholding__threading_local = Falsetry:# Could have checked if ``_thread`` was not in sys.modules and gone# a different route, but decided to mirror technique used with# ``threading`` below.if '_thread' in sys_modules:held_thread = sys_modules['_thread']holding_thread = True# Must have some module named ``_thread`` that implements its API# in order to initially import ``threading``.sys_modules['_thread'] = sys_modules['_dummy_thread']if 'threading' in sys_modules:# If ``threading`` is already imported, might as well prevent# trying to import it more than needed by saving it if it is# already imported before deleting it.held_threading = sys_modules['threading']holding_threading = Truedel sys_modules['threading']if '_threading_local' in sys_modules:# If ``_threading_local`` is already imported, might as well prevent# trying to import it more than needed by saving it if it is# already imported before deleting it.held__threading_local = sys_modules['_threading_local']holding__threading_local = Truedel sys_modules['_threading_local']import threading# Need a copy of the code kept somewhere...sys_modules['_dummy_threading'] = sys_modules['threading']del sys_modules['threading']sys_modules['_dummy__threading_local'] = sys_modules['_threading_local']del sys_modules['_threading_local']from _dummy_threading import *from _dummy_threading import __all__finally:# Put back ``threading`` if we overwrote earlierif holding_threading:sys_modules['threading'] = held_threadingdel held_threadingdel holding_threading# Put back ``_threading_local`` if we overwrote earlierif holding__threading_local:sys_modules['_threading_local'] = held__threading_localdel held__threading_localdel holding__threading_local# Put back ``thread`` if we overwrote, else del the entry we madeif holding_thread:sys_modules['_thread'] = held_threaddel held_threadelse:del sys_modules['_thread']del holding_threaddel _dummy_threaddel sys_modules
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。