summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2015-08-30 07:31:10 -0700
committerGiampaolo Rodola <g.rodola@gmail.com>2015-08-30 07:31:10 -0700
commit2c16ca797d286ab550726ea6c82a7378a10d6910 (patch)
tree8a834afd10445cf595c43edb5217d71d05dbc211
parenta73272ab06e99947fb3cea59a650c7fb458a1f57 (diff)
downloadpsutil-2c16ca797d286ab550726ea6c82a7378a10d6910.tar.gz
fix #644: add support for CTRL_* signals on Windows
-rw-r--r--HISTORY.rst4
-rw-r--r--docs/index.rst10
-rw-r--r--psutil/__init__.py8
-rw-r--r--psutil/_pswindows.py4
-rw-r--r--test/_windows.py13
5 files changed, 34 insertions, 5 deletions
diff --git a/HISTORY.rst b/HISTORY.rst
index f283932a..d4aea085 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -1,10 +1,12 @@
Bug tracker at https://github.com/giampaolo/psutil/issues
-3.1.2 - XXXX-XX-XX
+3.2.0 - XXXX-XX-XX
==================
**Enhancements**
+- #644: [Windows] added support for CTRL_C_EVENT and CTRL_BREAK_EVENT signals
+ to use with Process.send_signal().
- #648: CI test integration for OSX. (patch by Jeff Tang)
- #663: net_if_addrs() now returns point-to-point addresses (for VPNs).
- #655: [Windows] different issues regarding unicode handling were fixed. On
diff --git a/docs/index.rst b/docs/index.rst
index 4a2a5b94..a27ac2fb 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -457,7 +457,7 @@ Network
*New in 3.0.0*
- *Changed in 3.1.2:* *ptp* field was added.
+ *Changed in 3.2.0:* *ptp* field was added.
.. function:: net_if_stats()
@@ -1183,10 +1183,14 @@ Process class
Send a signal to process (see
`signal module <http://docs.python.org//library/signal.html>`__
constants) pre-emptively checking whether PID has been reused.
- This is the same as ``os.kill(pid, sig)``.
- On Windows only **SIGTERM** is valid and is treated as an alias for
+ On UNIX this is the same as ``os.kill(pid, sig)``.
+ On Windows only **SIGTERM**, **CTRL_C_EVENT ** and **CTRL_BREAK_EVENT**
+ signals are supported and **SIGTERM** is treated as an alias for
:meth:`kill()`.
+ *Changed in 3.2.0:* support for CTRL_C_EVENT and CTRL_BREAK_EVENT signals
+ was added.
+
.. method:: suspend()
Suspend process execution with **SIGSTOP** signal pre-emptively checking
diff --git a/psutil/__init__.py b/psutil/__init__.py
index 16688362..d85f86d4 100644
--- a/psutil/__init__.py
+++ b/psutil/__init__.py
@@ -1039,8 +1039,14 @@ class Process(object):
else:
if sig == signal.SIGTERM:
self._proc.kill()
+ # py >= 2.7
+ elif sig in (getattr(signal, "CTRL_C_EVENT", object()),
+ getattr(signal, "CTRL_BREAK_EVENT", object())):
+ self._proc.send_signal(sig)
else:
- raise ValueError("only SIGTERM is supported on Windows")
+ raise ValueError(
+ "only SIGTERM, CTRL_C_EVENT and CTRL_BREAK_EVENT signals "
+ "are supported on Windows")
@_assert_pid_not_reused
def suspend(self):
diff --git a/psutil/_pswindows.py b/psutil/_pswindows.py
index 2d92486f..30b4d9fa 100644
--- a/psutil/_pswindows.py
+++ b/psutil/_pswindows.py
@@ -388,6 +388,10 @@ class Process(object):
return cext.proc_kill(self.pid)
@wrap_exceptions
+ def send_signal(self, sig):
+ os.kill(self.pid, sig)
+
+ @wrap_exceptions
def wait(self, timeout=None):
if timeout is None:
timeout = cext.INFINITE
diff --git a/test/_windows.py b/test/_windows.py
index 0d2b2e15..885992be 100644
--- a/test/_windows.py
+++ b/test/_windows.py
@@ -295,6 +295,19 @@ class WindowsSpecificTestCase(unittest.TestCase):
except psutil.NoSuchProcess:
pass
+ @unittest.skipUnless(sys.version_info >= (2, 7),
+ "CTRL_* signals not supported")
+ def test_ctrl_signals(self):
+ p = psutil.Process(get_test_subprocess().pid)
+ p.send_signal(signal.CTRL_C_EVENT)
+ p.send_signal(signal.CTRL_BREAK_EVENT)
+ p.kill()
+ p.wait()
+ self.assertRaises(psutil.NoSuchProcess,
+ p.send_signal, signal.CTRL_C_EVENT)
+ self.assertRaises(psutil.NoSuchProcess,
+ p.send_signal, signal.CTRL_BREAK_EVENT)
+
@unittest.skipUnless(WINDOWS, "not a Windows system")
class TestDualProcessImplementation(unittest.TestCase):