--- /dev/null
+"""Faux ``threading`` version using ``dummy_thread`` instead of ``thread``.\r
+\r
+The module ``_dummy_threading`` is added to ``sys.modules`` in order\r
+to not have ``threading`` considered imported. Had ``threading`` been\r
+directly imported it would have made all subsequent imports succeed\r
+regardless of whether ``thread`` was available which is not desired.\r
+\r
+"""\r
+from sys import modules as sys_modules\r
+\r
+import dummy_thread\r
+\r
+# Declaring now so as to not have to nest ``try``s to get proper clean-up.\r
+holding_thread = False\r
+holding_threading = False\r
+holding__threading_local = False\r
+\r
+try:\r
+ # Could have checked if ``thread`` was not in sys.modules and gone\r
+ # a different route, but decided to mirror technique used with\r
+ # ``threading`` below.\r
+ if 'thread' in sys_modules:\r
+ held_thread = sys_modules['thread']\r
+ holding_thread = True\r
+ # Must have some module named ``thread`` that implements its API\r
+ # in order to initially import ``threading``.\r
+ sys_modules['thread'] = sys_modules['dummy_thread']\r
+\r
+ if 'threading' in sys_modules:\r
+ # If ``threading`` is already imported, might as well prevent\r
+ # trying to import it more than needed by saving it if it is\r
+ # already imported before deleting it.\r
+ held_threading = sys_modules['threading']\r
+ holding_threading = True\r
+ del sys_modules['threading']\r
+\r
+ if '_threading_local' in sys_modules:\r
+ # If ``_threading_local`` is already imported, might as well prevent\r
+ # trying to import it more than needed by saving it if it is\r
+ # already imported before deleting it.\r
+ held__threading_local = sys_modules['_threading_local']\r
+ holding__threading_local = True\r
+ del sys_modules['_threading_local']\r
+\r
+ import threading\r
+ # Need a copy of the code kept somewhere...\r
+ sys_modules['_dummy_threading'] = sys_modules['threading']\r
+ del sys_modules['threading']\r
+ sys_modules['_dummy__threading_local'] = sys_modules['_threading_local']\r
+ del sys_modules['_threading_local']\r
+ from _dummy_threading import *\r
+ from _dummy_threading import __all__\r
+\r
+finally:\r
+ # Put back ``threading`` if we overwrote earlier\r
+\r
+ if holding_threading:\r
+ sys_modules['threading'] = held_threading\r
+ del held_threading\r
+ del holding_threading\r
+\r
+ # Put back ``_threading_local`` if we overwrote earlier\r
+\r
+ if holding__threading_local:\r
+ sys_modules['_threading_local'] = held__threading_local\r
+ del held__threading_local\r
+ del holding__threading_local\r
+\r
+ # Put back ``thread`` if we overwrote, else del the entry we made\r
+ if holding_thread:\r
+ sys_modules['thread'] = held_thread\r
+ del held_thread\r
+ else:\r
+ del sys_modules['thread']\r
+ del holding_thread\r
+\r
+ del dummy_thread\r
+ del sys_modules\r