dummy_threading.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. :Author: Brett Cannon
  7. :Contact: [email protected]
  8. XXX: Try to get rid of ``_dummy_threading``.
  9. """
  10. from sys import modules as sys_modules
  11. import dummy_thread
  12. # Declaring now so as to not have to nest ``try``s to get proper clean-up.
  13. holding_thread = False
  14. holding_threading = False
  15. holding__threading_local = False
  16. try:
  17. # Could have checked if ``thread`` was not in sys.modules and gone
  18. # a different route, but decided to mirror technique used with
  19. # ``threading`` below.
  20. if 'thread' in sys_modules:
  21. held_thread = sys_modules['thread']
  22. holding_thread = True
  23. # Must have some module named ``thread`` that implements its API
  24. # in order to initially import ``threading``.
  25. sys_modules['thread'] = sys_modules['dummy_thread']
  26. if 'threading' in sys_modules:
  27. # If ``threading`` is already imported, might as well prevent
  28. # trying to import it more than needed by saving it if it is
  29. # already imported before deleting it.
  30. held_threading = sys_modules['threading']
  31. holding_threading = True
  32. del sys_modules['threading']
  33. if '_threading_local' in sys_modules:
  34. # If ``_threading_local`` is already imported, might as well prevent
  35. # trying to import it more than needed by saving it if it is
  36. # already imported before deleting it.
  37. held__threading_local = sys_modules['_threading_local']
  38. holding__threading_local = True
  39. del sys_modules['_threading_local']
  40. import threading
  41. # Need a copy of the code kept somewhere...
  42. sys_modules['_dummy_threading'] = sys_modules['threading']
  43. del sys_modules['threading']
  44. sys_modules['_dummy__threading_local'] = sys_modules['_threading_local']
  45. del sys_modules['_threading_local']
  46. from _dummy_threading import *
  47. from _dummy_threading import __all__
  48. finally:
  49. # Put back ``threading`` if we overwrote earlier
  50. if holding_threading:
  51. sys_modules['threading'] = held_threading
  52. del held_threading
  53. del holding_threading
  54. # Put back ``_threading_local`` if we overwrote earlier
  55. if holding__threading_local:
  56. sys_modules['_threading_local'] = held__threading_local
  57. del held__threading_local
  58. del holding__threading_local
  59. # Put back ``thread`` if we overwrote, else del the entry we made
  60. if holding_thread:
  61. sys_modules['thread'] = held_thread
  62. del held_thread
  63. else:
  64. del sys_modules['thread']
  65. del holding_thread
  66. del dummy_thread
  67. del sys_modules