[PATCH 2/2] examples: modem-watcher: get rid of global variables

yegorslists at googlemail.com yegorslists at googlemail.com
Mon Feb 15 16:53:39 UTC 2021


From: Yegor Yefremov <yegorslists at googlemail.com>

Move the code into the main() routine and pass main_loop as
a parameter to the signal handler.
---
 .../modem-watcher-python/modem-watcher-python | 25 ++++++++++++-------
 1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/examples/modem-watcher-python/modem-watcher-python b/examples/modem-watcher-python/modem-watcher-python
index 4884beef..f7dd1fc6 100755
--- a/examples/modem-watcher-python/modem-watcher-python
+++ b/examples/modem-watcher-python/modem-watcher-python
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 # -*- Mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 #
 # This program is free software; you can redistribute it and/or modify it under
@@ -24,21 +24,28 @@ from gi.repository import GLib
 
 import ModemWatcher
 
-main_loop = None
-watcher = None
 
-def signal_handler(data):
-    main_loop.quit()
+def signal_handler(loop):
+    """SIGHUP and SIGINT handler."""
+    loop.quit()
 
-if __name__ == "__main__":
+
+def main():
+    """Main routine."""
     # Create modem watcher
-    watcher = ModemWatcher.ModemWatcher()
+    ModemWatcher.ModemWatcher()
 
     # Main loop
     main_loop = GLib.MainLoop()
-    GLib.unix_signal_add(GLib.PRIORITY_HIGH, signal.SIGHUP, signal_handler, None)
-    GLib.unix_signal_add(GLib.PRIORITY_HIGH, signal.SIGTERM, signal_handler, None)
+    GLib.unix_signal_add(
+        GLib.PRIORITY_HIGH, signal.SIGHUP, signal_handler, main_loop)
+    GLib.unix_signal_add(
+        GLib.PRIORITY_HIGH, signal.SIGTERM, signal_handler, main_loop)
     try:
         main_loop.run()
     except KeyboardInterrupt:
         pass
+
+
+if __name__ == "__main__":
+    main()
-- 
2.17.0



More information about the ModemManager-devel mailing list