Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
self._full = full
self._default = default
self._default_loop = None
self._policy = unix_events.DefaultEventLoopPolicy()
self._policy.new_event_loop = self.new_event_loop
self.get_event_loop = self._policy.get_event_loop
self.set_event_loop = self._policy.set_event_loop
self.get_child_watcher = self._policy.get_child_watcher
self._policy.set_child_watcher(GLibChildWatcher())
BaseGLibEventLoop.init_class()
if threads:
logger.info("GLib threads enabled")
GObject.threads_init()
else:
logger.info("GLib threads not used")
def __new__(cls, *k, **kw):
raise RuntimeError("GLib threads not enabled (you should use %s(threads=True)" % self.__class__.__name__)
threading.Thread.__new__ = __new__
#TODO: move it into unix_events
GLibEventLoop = BaseGLibEventLoop
class _LoopImplem:
def __init__(self, loop):
self._loop = loop
def run(self):
raise NotImplementedError()
def stop(self):
raise NotImplementedError()
class _GLibLoopImplem(_LoopImplem):
def __init__(self, loop):
super().__init__(loop)
# We use the introspected MainLoop object directly, because the
# override in pygobject tampers with SIGINT
self._mainloop = GLib._introspection_module.MainLoop.new(self._loop._context, True)
def run(self):
self._mainloop.run()
def stop(self):
self._mainloop.quit()
if Gtk:
class _GtkLoopImplem(_LoopImplem):
def __init__(self, loop):
super().__init__(loop)
# We use the introspected MainLoop object directly, because the
# override in pygobject tampers with SIGINT
self._mainloop = GLib._introspection_module.MainLoop.new(self._loop._context, True)
def run(self):
self._mainloop.run()
def stop(self):
self._mainloop.quit()
if Gtk:
class _GtkLoopImplem(_LoopImplem):
def run(self):
Gtk.main()
def stop(self):
Gtk.main_quit()
class _GApplicationLoopImplem(_LoopImplem):
def __init__(self, loop, application):
super().__init__(loop)
if not isinstance (application, Gio.Application):
raise TypeError("application must be a Gio.Application object")
self._application = application
def run(self):
self._mainloop.run()
def stop(self):
self._mainloop.quit()
if Gtk:
class _GtkLoopImplem(_LoopImplem):
def run(self):
Gtk.main()
def stop(self):
Gtk.main_quit()
class _GApplicationLoopImplem(_LoopImplem):
def __init__(self, loop, application):
super().__init__(loop)
if not isinstance (application, Gio.Application):
raise TypeError("application must be a Gio.Application object")
self._application = application
def run(self):
self._application.run(None)
def stop(self):
self._application.quit()
class GLibEventLoopPolicy(events.AbstractEventLoopPolicy):
If this flag is set, then this policy will use a glib event loop
for every thread. Use this parameter if you want your loops to
interact with modules written in other languages.
default Use the default context (default: True)
Indicates whether you want to use the GLib default context. If set,
then the loop associated with the main thread will use the default
(NULL) GLib context (instead of creating a new one).
"""
self._full = full
self._default = default
self._default_loop = None
self._policy = unix_events.DefaultEventLoopPolicy()
self._policy.new_event_loop = self.new_event_loop
self.get_event_loop = self._policy.get_event_loop
self.set_event_loop = self._policy.set_event_loop
self.get_child_watcher = self._policy.get_child_watcher
self._policy.set_child_watcher(GLibChildWatcher())
BaseGLibEventLoop.init_class()
if threads:
logger.info("GLib threads enabled")
GObject.threads_init()
else:
logger.info("GLib threads not used")
then the loop associated with the main thread will use the default
(NULL) GLib context (instead of creating a new one).
"""
self._full = full
self._default = default
self._default_loop = None
self._policy = unix_events.DefaultEventLoopPolicy()
self._policy.new_event_loop = self.new_event_loop
self.get_event_loop = self._policy.get_event_loop
self.set_event_loop = self._policy.set_event_loop
self.get_child_watcher = self._policy.get_child_watcher
self._policy.set_child_watcher(GLibChildWatcher())
BaseGLibEventLoop.init_class()
if threads:
logger.info("GLib threads enabled")
GObject.threads_init()
else:
logger.info("GLib threads not used")
def __new__(cls, *k, **kw):
raise RuntimeError("GLib threads not enabled (you should use %s(threads=True)" % self.__class__.__name__)
threading.Thread.__new__ = __new__
def _new_default_loop(self):
return GLibEventLoop(GLib.main_context_default())
def _new_default_loop(self):
return GLibEventLoop(gtk=True)
self._check_signal(sig)
try:
self._sighandlers.pop(sig).cancel()
return True
except KeyError:
return False
def get_debug(self):
pass
def set_debug(self):
pass
#TODO: move it into unix_events
GLibEventLoop = BaseGLibEventLoop
class _LoopImplem:
def __init__(self, loop):
self._loop = loop
def run(self):
raise NotImplementedError()
def stop(self):
raise NotImplementedError()
class _GLibLoopImplem(_LoopImplem):
def __init__(self, loop):
super().__init__(loop)
@staticmethod
def init_class():
if not hasattr(BaseGLibEventLoop, "_default_sigint_handler"):
BaseGLibEventLoop._default_sigint_handler = BaseGLibEventLoop.DefaultSigINTHandler()