dummy_threading.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. """Faux ``threading`` version using ``dummy_thread`` instead of ``thread``.
  2. The module ``_dummy_threading`` is added to ``sys.modules`` in order
  3. to not have ``threading`` considered imported. Had ``threading`` been
  4. directly imported it would have made all subsequent imports succeed
  5. regardless of whether ``thread`` was available which is not desired.
  6. """
  7. from sys import modules as sys_modules
  8. import dummy_thread
  9. # Declaring now so as to not have to nest ``try``s to get proper clean-up.
  10. holding_thread = False
  11. holding_threading = False
  12. holding__threading_local = False
  13. try:
  14. # Could have checked if ``thread`` was not in sys.modules and gone
  15. # a different route, but decided to mirror technique used with
  16. # ``threading`` below.
  17. if 'thread' in sys_modules:
  18. held_thread = sys_modules['thread']
  19. holding_thread = True
  20. # Must have some module named ``thread`` that implements its API
  21. # in order to initially import ``threading``.
  22. sys_modules['thread'] = sys_modules['dummy_thread']
  23. if 'threading' in sys_modules:
  24. # If ``threading`` is already imported, might as well prevent
  25. # trying to import it more than needed by saving it if it is
  26. # already imported before deleting it.
  27. held_threading = sys_modules['threading']
  28. holding_threading = True
  29. del sys_modules['threading']
  30. if '_threading_local' in sys_modules:
  31. # If ``_threading_local`` is already imported, might as well prevent
  32. # trying to import it more than needed by saving it if it is
  33. # already imported before deleting it.
  34. held__threading_local = sys_modules['_threading_local']
  35. holding__threading_local = True
  36. del sys_modules['_threading_local']
  37. import threading
  38. # Need a copy of the code kept somewhere...
  39. sys_modules['_dummy_threading'] = sys_modules['threading']
  40. del sys_modules['threading']
  41. sys_modules['_dummy__threading_local'] = sys_modules['_threading_local']
  42. del sys_modules['_threading_local']
  43. from _dummy_threading import *
  44. from _dummy_threading import __all__
  45. finally:
  46. # Put back ``threading`` if we overwrote earlier
  47. if holding_threading:
  48. sys_modules['threading'] = held_threading
  49. del held_threading
  50. del holding_threading
  51. # Put back ``_threading_local`` if we overwrote earlier
  52. if holding__threading_local:
  53. sys_modules['_threading_local'] = held__threading_local
  54. del held__threading_local
  55. del holding__threading_local
  56. # Put back ``thread`` if we overwrote, else del the entry we made
  57. if holding_thread:
  58. sys_modules['thread'] = held_thread
  59. del held_thread
  60. else:
  61. del sys_modules['thread']
  62. del holding_thread
  63. del dummy_thread
  64. del sys_modules