Blame Lib/dummy_threading.py

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