summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--psutil/_psutil_windows.c12
-rw-r--r--psutil/_pswindows.py3
2 files changed, 6 insertions, 9 deletions
diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c
index 215398c8..8c5e697d 100644
--- a/psutil/_psutil_windows.c
+++ b/psutil/_psutil_windows.c
@@ -408,16 +408,14 @@ psutil_proc_wait(PyObject *self, PyObject *args) {
static PyObject *
psutil_proc_cpu_times(PyObject *self, PyObject *args) {
long pid;
- HANDLE hProcess;
+ unsigned long handle;
FILETIME ftCreate, ftExit, ftKernel, ftUser;
- if (! PyArg_ParseTuple(args, "l", &pid))
+ if (! PyArg_ParseTuple(args, "lk", &pid, &handle))
return NULL;
- hProcess = psutil_handle_from_pid(pid);
- if (hProcess == NULL)
- return NULL;
- if (! GetProcessTimes(hProcess, &ftCreate, &ftExit, &ftKernel, &ftUser)) {
+ if (! GetProcessTimes(
+ (HANDLE)handle, &ftCreate, &ftExit, &ftKernel, &ftUser)) {
CloseHandle(hProcess);
if (GetLastError() == ERROR_ACCESS_DENIED) {
// usually means the process has died so we throw a NoSuchProcess
@@ -430,8 +428,6 @@ psutil_proc_cpu_times(PyObject *self, PyObject *args) {
}
}
- CloseHandle(hProcess);
-
/*
* User and kernel times are represented as a FILETIME structure
* wich contains a 64-bit value representing the number of
diff --git a/psutil/_pswindows.py b/psutil/_pswindows.py
index 02d81287..65ebacb4 100644
--- a/psutil/_pswindows.py
+++ b/psutil/_pswindows.py
@@ -735,7 +735,8 @@ class Process(object):
@wrap_exceptions
def cpu_times(self):
try:
- user, system = cext.proc_cpu_times(self.pid)
+ with self.handle_ctx() as handle:
+ user, system = cext.proc_cpu_times(self.pid, handle)
except OSError as err:
if err.errno in ACCESS_DENIED_SET:
nt = ntpinfo(*cext.proc_info(self.pid))