Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(277)

Unified Diff: devil/devil/utils/signal_handler.py

Issue 2069133005: [devil] Add signal handler context manager. (Closed) Base URL: git@github.com:catapult-project/catapult.git@master
Patch Set: mikecase comment Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: devil/devil/utils/signal_handler.py
diff --git a/devil/devil/utils/signal_handler.py b/devil/devil/utils/signal_handler.py
new file mode 100644
index 0000000000000000000000000000000000000000..566bef94e93cea7db9f0b1ad2b5360e9aa35659d
--- /dev/null
+++ b/devil/devil/utils/signal_handler.py
@@ -0,0 +1,30 @@
+# Copyright 2016 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import contextlib
+import signal
+
+
+@contextlib.contextmanager
+def AddSignalHandler(signalnum, additional_handler):
+ """Adds a signal handler for the given signal in the wrapped context.
+
+ This runs the new handler after any existing handler rather than
+ replacing the existing handler.
+
+ Args:
+ signum: The signal for which a handler should be added.
+ additional_handler: The handler to add.
+ """
+ existing_handler = signal.getsignal(signalnum)
+
+ def handler(signum, frame):
+ if callable(existing_handler):
+ existing_handler(signum, frame)
+ additional_handler(signum, frame)
+
+ signal.signal(signalnum, handler)
+ yield
+ signal.signal(signalnum, existing_handler)
+
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698