summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2015-08-26 08:02:32 -0700
committerGiampaolo Rodola <g.rodola@gmail.com>2015-08-26 08:02:32 -0700
commitd592453854a6742c2e9efff25b5203d544853d7a (patch)
tree743f3bd46102b9dafa593eb3e2e0459bffae3957
parent8d946e075f5a2d760b083a7cfacd5d01bfb9f72d (diff)
downloadpsutil-d592453854a6742c2e9efff25b5203d544853d7a.tar.gz
#650: Process.cwd() return unicode for non ASCII paths
-rw-r--r--docs/index.rst3
-rw-r--r--psutil/_psutil_windows.c35
-rw-r--r--psutil/_pswindows.py2
-rw-r--r--test/_windows.py2
4 files changed, 12 insertions, 30 deletions
diff --git a/docs/index.rst b/docs/index.rst
index 9848b068..69edcead 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -744,6 +744,9 @@ Process class
The process current working directory as an absolute path.
+ *Changed in 3.2.0:* (Windows, Python 2) in case of non ASCII path the
+ returned type is unicode instead of str.
+
.. method:: username()
The name of the user that owns the process. On UNIX this is calculated by
diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c
index a6bf28bc..43ef0d47 100644
--- a/psutil/_psutil_windows.c
+++ b/psutil/_psutil_windows.c
@@ -941,9 +941,7 @@ psutil_proc_cwd(PyObject *self, PyObject *args) {
PVOID rtlUserProcParamsAddress;
UNICODE_STRING currentDirectory;
WCHAR *currentDirectoryContent = NULL;
- PyObject *returnPyObj = NULL;
- PyObject *cwd_from_wchar = NULL;
- PyObject *cwd = NULL;
+ PyObject *py_cwd = NULL;
if (! PyArg_ParseTuple(args, "l", &pid))
return NULL;
@@ -1024,36 +1022,17 @@ psutil_proc_cwd(PyObject *self, PyObject *args) {
// currentDirectory.Length is in bytes
currentDirectoryContent[(currentDirectory.Length / sizeof(WCHAR))] = '\0';
- // convert wchar array to a Python unicode string, and then to UTF8
- cwd_from_wchar = PyUnicode_FromWideChar(currentDirectoryContent,
- wcslen(currentDirectoryContent));
- if (cwd_from_wchar == NULL)
+ // convert wchar array to a Python unicode string
+ py_cwd = PyUnicode_FromWideChar(
+ currentDirectoryContent, wcslen(currentDirectoryContent));
+ if (py_cwd == NULL)
goto error;
-
-#if PY_MAJOR_VERSION >= 3
- cwd = PyUnicode_FromObject(cwd_from_wchar);
-#else
- cwd = PyUnicode_AsUTF8String(cwd_from_wchar);
-#endif
- if (cwd == NULL)
- goto error;
-
- // decrement the reference count on our temp unicode str to avoid
- // mem leak
- returnPyObj = Py_BuildValue("N", cwd);
- if (!returnPyObj)
- goto error;
-
- Py_DECREF(cwd_from_wchar);
-
CloseHandle(processHandle);
free(currentDirectoryContent);
- return returnPyObj;
+ return py_cwd;
error:
- Py_XDECREF(cwd_from_wchar);
- Py_XDECREF(cwd);
- Py_XDECREF(returnPyObj);
+ Py_XDECREF(py_cwd);
if (currentDirectoryContent != NULL)
free(currentDirectoryContent);
if (processHandle != NULL)
diff --git a/psutil/_pswindows.py b/psutil/_pswindows.py
index ca48120f..479cf770 100644
--- a/psutil/_pswindows.py
+++ b/psutil/_pswindows.py
@@ -440,7 +440,7 @@ class Process(object):
# return a normalized pathname since the native C function appends
# "\\" at the and of the path
path = cext.proc_cwd(self.pid)
- return os.path.normpath(path)
+ return py2_stringify(os.path.normpath(path))
@wrap_exceptions
def open_files(self):
diff --git a/test/_windows.py b/test/_windows.py
index 5d6fd366..8f2c7a52 100644
--- a/test/_windows.py
+++ b/test/_windows.py
@@ -495,7 +495,7 @@ class TestUnicode(unittest.TestCase):
self.assertEqual(p.cmdline(), [self.uexe])
def test_proc_cwd(self):
- tdir = tempfile.mkdtemp(prefix="psutil-è-")
+ tdir = tempfile.mkdtemp(prefix=u("psutil-è-"))
self.addCleanup(safe_rmdir, tdir)
with chdir(tdir):
p = psutil.Process()