Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

modulehelpers.py 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Testing helpers related to the module system.
  5. """
  6. __all__ = ["NoReactor", "AlternateReactor"]
  7. import sys
  8. import twisted.internet
  9. from twisted.test.test_twisted import SetAsideModule
  10. class NoReactor(SetAsideModule):
  11. """
  12. Context manager that uninstalls the reactor, if any, and then restores it
  13. afterwards.
  14. """
  15. def __init__(self):
  16. SetAsideModule.__init__(self, "twisted.internet.reactor")
  17. def __enter__(self):
  18. SetAsideModule.__enter__(self)
  19. if "twisted.internet.reactor" in self.modules:
  20. del twisted.internet.reactor
  21. def __exit__(self, excType, excValue, traceback):
  22. SetAsideModule.__exit__(self, excType, excValue, traceback)
  23. # Clean up 'reactor' attribute that may have been set on
  24. # twisted.internet:
  25. reactor = self.modules.get("twisted.internet.reactor", None)
  26. if reactor is not None:
  27. twisted.internet.reactor = reactor
  28. else:
  29. try:
  30. del twisted.internet.reactor
  31. except AttributeError:
  32. pass
  33. class AlternateReactor(NoReactor):
  34. """
  35. A context manager which temporarily installs a different object as the
  36. global reactor.
  37. """
  38. def __init__(self, reactor):
  39. """
  40. @param reactor: Any object to install as the global reactor.
  41. """
  42. NoReactor.__init__(self)
  43. self.alternate = reactor
  44. def __enter__(self):
  45. NoReactor.__enter__(self)
  46. twisted.internet.reactor = self.alternate
  47. sys.modules["twisted.internet.reactor"] = self.alternate