_glibbase.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. # -*- test-case-name: twisted.internet.test -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. This module provides base support for Twisted to interact with the glib/gtk
  6. mainloops.
  7. The classes in this module should not be used directly, but rather you should
  8. import gireactor or gtk3reactor for GObject Introspection based applications,
  9. or glib2reactor or gtk2reactor for applications using legacy static bindings.
  10. """
  11. import sys
  12. from typing import Any, Callable, Dict, Set
  13. from zope.interface import implementer
  14. from twisted.internet import posixbase
  15. from twisted.internet.abstract import FileDescriptor
  16. from twisted.internet.interfaces import IReactorFDSet, IReadDescriptor, IWriteDescriptor
  17. from twisted.python import log
  18. from twisted.python.monkey import MonkeyPatcher
  19. from ._signals import _IWaker, _UnixWaker
  20. def ensureNotImported(moduleNames, errorMessage, preventImports=[]):
  21. """
  22. Check whether the given modules were imported, and if requested, ensure
  23. they will not be importable in the future.
  24. @param moduleNames: A list of module names we make sure aren't imported.
  25. @type moduleNames: C{list} of C{str}
  26. @param preventImports: A list of module name whose future imports should
  27. be prevented.
  28. @type preventImports: C{list} of C{str}
  29. @param errorMessage: Message to use when raising an C{ImportError}.
  30. @type errorMessage: C{str}
  31. @raise ImportError: with given error message if a given module name
  32. has already been imported.
  33. """
  34. for name in moduleNames:
  35. if sys.modules.get(name) is not None:
  36. raise ImportError(errorMessage)
  37. # Disable module imports to avoid potential problems.
  38. for name in preventImports:
  39. sys.modules[name] = None
  40. class GlibWaker(_UnixWaker):
  41. """
  42. Run scheduled events after waking up.
  43. """
  44. def __init__(self, reactor):
  45. super().__init__()
  46. self.reactor = reactor
  47. def doRead(self) -> None:
  48. super().doRead()
  49. self.reactor._simulate()
  50. def _signalGlue():
  51. """
  52. Integrate glib's wakeup file descriptor usage and our own.
  53. Python supports only one wakeup file descriptor at a time and both Twisted
  54. and glib want to use it.
  55. This is a context manager that can be wrapped around the whole glib
  56. reactor main loop which makes our signal handling work with glib's signal
  57. handling.
  58. """
  59. from gi import _ossighelper as signalGlue
  60. patcher = MonkeyPatcher()
  61. patcher.addPatch(signalGlue, "_wakeup_fd_is_active", True)
  62. return patcher
  63. def _loopQuitter(
  64. idleAdd: Callable[[Callable[[], None]], None], loopQuit: Callable[[], None]
  65. ) -> Callable[[], None]:
  66. """
  67. Combine the C{glib.idle_add} and C{glib.MainLoop.quit} functions into a
  68. function suitable for crashing the reactor.
  69. """
  70. return lambda: idleAdd(loopQuit)
  71. @implementer(IReactorFDSet)
  72. class GlibReactorBase(posixbase.PosixReactorBase, posixbase._PollLikeMixin):
  73. """
  74. Base class for GObject event loop reactors.
  75. Notification for I/O events (reads and writes on file descriptors) is done
  76. by the gobject-based event loop. File descriptors are registered with
  77. gobject with the appropriate flags for read/write/disconnect notification.
  78. Time-based events, the results of C{callLater} and C{callFromThread}, are
  79. handled differently. Rather than registering each event with gobject, a
  80. single gobject timeout is registered for the earliest scheduled event, the
  81. output of C{reactor.timeout()}. For example, if there are timeouts in 1, 2
  82. and 3.4 seconds, a single timeout is registered for 1 second in the
  83. future. When this timeout is hit, C{_simulate} is called, which calls the
  84. appropriate Twisted-level handlers, and a new timeout is added to gobject
  85. by the C{_reschedule} method.
  86. To handle C{callFromThread} events, we use a custom waker that calls
  87. C{_simulate} whenever it wakes up.
  88. @ivar _sources: A dictionary mapping L{FileDescriptor} instances to
  89. GSource handles.
  90. @ivar _reads: A set of L{FileDescriptor} instances currently monitored for
  91. reading.
  92. @ivar _writes: A set of L{FileDescriptor} instances currently monitored for
  93. writing.
  94. @ivar _simtag: A GSource handle for the next L{simulate} call.
  95. """
  96. # Install a waker that knows it needs to call C{_simulate} in order to run
  97. # callbacks queued from a thread:
  98. def _wakerFactory(self) -> _IWaker:
  99. return GlibWaker(self)
  100. def __init__(self, glib_module: Any, gtk_module: Any, useGtk: bool = False) -> None:
  101. self._simtag = None
  102. self._reads: Set[IReadDescriptor] = set()
  103. self._writes: Set[IWriteDescriptor] = set()
  104. self._sources: Dict[FileDescriptor, int] = {}
  105. self._glib = glib_module
  106. self._POLL_DISCONNECTED = (
  107. glib_module.IOCondition.HUP
  108. | glib_module.IOCondition.ERR
  109. | glib_module.IOCondition.NVAL
  110. )
  111. self._POLL_IN = glib_module.IOCondition.IN
  112. self._POLL_OUT = glib_module.IOCondition.OUT
  113. # glib's iochannel sources won't tell us about any events that we haven't
  114. # asked for, even if those events aren't sensible inputs to the poll()
  115. # call.
  116. self.INFLAGS = self._POLL_IN | self._POLL_DISCONNECTED
  117. self.OUTFLAGS = self._POLL_OUT | self._POLL_DISCONNECTED
  118. super().__init__()
  119. self._source_remove = self._glib.source_remove
  120. self._timeout_add = self._glib.timeout_add
  121. self.context = self._glib.main_context_default()
  122. self._pending = self.context.pending
  123. self._iteration = self.context.iteration
  124. self.loop = self._glib.MainLoop()
  125. self._crash = _loopQuitter(self._glib.idle_add, self.loop.quit)
  126. self._run = self.loop.run
  127. def _reallyStartRunning(self):
  128. """
  129. Make sure the reactor's signal handlers are installed despite any
  130. outside interference.
  131. """
  132. # First, install SIGINT and friends:
  133. super()._reallyStartRunning()
  134. # Next, since certain versions of gtk will clobber our signal handler,
  135. # set all signal handlers again after the event loop has started to
  136. # ensure they're *really* set.
  137. #
  138. # We don't actually know which versions of gtk do this so this might
  139. # be obsolete. If so, that would be great and this whole method can
  140. # go away. Someone needs to find out, though.
  141. #
  142. # https://github.com/twisted/twisted/issues/11762
  143. def reinitSignals():
  144. self._signals.uninstall()
  145. self._signals.install()
  146. self.callLater(0, reinitSignals)
  147. # The input_add function in pygtk1 checks for objects with a
  148. # 'fileno' method and, if present, uses the result of that method
  149. # as the input source. The pygtk2 input_add does not do this. The
  150. # function below replicates the pygtk1 functionality.
  151. # In addition, pygtk maps gtk.input_add to _gobject.io_add_watch, and
  152. # g_io_add_watch() takes different condition bitfields than
  153. # gtk_input_add(). We use g_io_add_watch() here in case pygtk fixes this
  154. # bug.
  155. def input_add(self, source, condition, callback):
  156. if hasattr(source, "fileno"):
  157. # handle python objects
  158. def wrapper(ignored, condition):
  159. return callback(source, condition)
  160. fileno = source.fileno()
  161. else:
  162. fileno = source
  163. wrapper = callback
  164. return self._glib.io_add_watch(
  165. fileno,
  166. self._glib.PRIORITY_DEFAULT_IDLE,
  167. condition,
  168. wrapper,
  169. )
  170. def _ioEventCallback(self, source, condition):
  171. """
  172. Called by event loop when an I/O event occurs.
  173. """
  174. log.callWithLogger(source, self._doReadOrWrite, source, source, condition)
  175. return True # True = don't auto-remove the source
  176. def _add(self, source, primary, other, primaryFlag, otherFlag):
  177. """
  178. Add the given L{FileDescriptor} for monitoring either for reading or
  179. writing. If the file is already monitored for the other operation, we
  180. delete the previous registration and re-register it for both reading
  181. and writing.
  182. """
  183. if source in primary:
  184. return
  185. flags = primaryFlag
  186. if source in other:
  187. self._source_remove(self._sources[source])
  188. flags |= otherFlag
  189. self._sources[source] = self.input_add(source, flags, self._ioEventCallback)
  190. primary.add(source)
  191. def addReader(self, reader):
  192. """
  193. Add a L{FileDescriptor} for monitoring of data available to read.
  194. """
  195. self._add(reader, self._reads, self._writes, self.INFLAGS, self.OUTFLAGS)
  196. def addWriter(self, writer):
  197. """
  198. Add a L{FileDescriptor} for monitoring ability to write data.
  199. """
  200. self._add(writer, self._writes, self._reads, self.OUTFLAGS, self.INFLAGS)
  201. def getReaders(self):
  202. """
  203. Retrieve the list of current L{FileDescriptor} monitored for reading.
  204. """
  205. return list(self._reads)
  206. def getWriters(self):
  207. """
  208. Retrieve the list of current L{FileDescriptor} monitored for writing.
  209. """
  210. return list(self._writes)
  211. def removeAll(self):
  212. """
  213. Remove monitoring for all registered L{FileDescriptor}s.
  214. """
  215. return self._removeAll(self._reads, self._writes)
  216. def _remove(self, source, primary, other, flags):
  217. """
  218. Remove monitoring the given L{FileDescriptor} for either reading or
  219. writing. If it's still monitored for the other operation, we
  220. re-register the L{FileDescriptor} for only that operation.
  221. """
  222. if source not in primary:
  223. return
  224. self._source_remove(self._sources[source])
  225. primary.remove(source)
  226. if source in other:
  227. self._sources[source] = self.input_add(source, flags, self._ioEventCallback)
  228. else:
  229. self._sources.pop(source)
  230. def removeReader(self, reader):
  231. """
  232. Stop monitoring the given L{FileDescriptor} for reading.
  233. """
  234. self._remove(reader, self._reads, self._writes, self.OUTFLAGS)
  235. def removeWriter(self, writer):
  236. """
  237. Stop monitoring the given L{FileDescriptor} for writing.
  238. """
  239. self._remove(writer, self._writes, self._reads, self.INFLAGS)
  240. def iterate(self, delay=0):
  241. """
  242. One iteration of the event loop, for trial's use.
  243. This is not used for actual reactor runs.
  244. """
  245. self.runUntilCurrent()
  246. while self._pending():
  247. self._iteration(0)
  248. def crash(self):
  249. """
  250. Crash the reactor.
  251. """
  252. posixbase.PosixReactorBase.crash(self)
  253. self._crash()
  254. def stop(self):
  255. """
  256. Stop the reactor.
  257. """
  258. posixbase.PosixReactorBase.stop(self)
  259. # The base implementation only sets a flag, to ensure shutting down is
  260. # not reentrant. Unfortunately, this flag is not meaningful to the
  261. # gobject event loop. We therefore call wakeUp() to ensure the event
  262. # loop will call back into Twisted once this iteration is done. This
  263. # will result in self.runUntilCurrent() being called, where the stop
  264. # flag will trigger the actual shutdown process, eventually calling
  265. # crash() which will do the actual gobject event loop shutdown.
  266. self.wakeUp()
  267. def run(self, installSignalHandlers=True):
  268. """
  269. Run the reactor.
  270. """
  271. with _signalGlue():
  272. self.callWhenRunning(self._reschedule)
  273. self.startRunning(installSignalHandlers=installSignalHandlers)
  274. if self._started:
  275. self._run()
  276. def callLater(self, *args, **kwargs):
  277. """
  278. Schedule a C{DelayedCall}.
  279. """
  280. result = posixbase.PosixReactorBase.callLater(self, *args, **kwargs)
  281. # Make sure we'll get woken up at correct time to handle this new
  282. # scheduled call:
  283. self._reschedule()
  284. return result
  285. def _reschedule(self):
  286. """
  287. Schedule a glib timeout for C{_simulate}.
  288. """
  289. if self._simtag is not None:
  290. self._source_remove(self._simtag)
  291. self._simtag = None
  292. timeout = self.timeout()
  293. if timeout is not None:
  294. self._simtag = self._timeout_add(
  295. int(timeout * 1000),
  296. self._simulate,
  297. priority=self._glib.PRIORITY_DEFAULT_IDLE,
  298. )
  299. def _simulate(self):
  300. """
  301. Run timers, and then reschedule glib timeout for next scheduled event.
  302. """
  303. self.runUntilCurrent()
  304. self._reschedule()