diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2017-05-03 04:03:45 +0200 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2017-05-03 04:03:45 +0200 |
commit | ace8d289f77febf5824a7c3bff26f3c9e10f0dae (patch) | |
tree | 0b9fb1857be13e6af14e1c711f67638e23cf1536 | |
parent | c2e8ee39c05d007f01fc66b5e99dc3593830cc4d (diff) | |
download | psutil-ace8d289f77febf5824a7c3bff26f3c9e10f0dae.tar.gz |
re #1040: bif one win/ unicode / memory_maps()
Fix memory_maps() which was returning an invalid encoded path in case of
non ASCII path on both Python 2 and 3. Use GetMappedFileNameW instead of GetMappedFilenameA in
order to ask the system an actual unicode path. Also, on Windows
encode unicode back to str/bytes by using default fs-encoding + "replace"
error handler.
This paves the way for fixing other APIs in an identical manner.
-rw-r--r-- | psutil/_psutil_windows.c | 15 | ||||
-rw-r--r-- | psutil/_pswindows.py | 8 | ||||
-rwxr-xr-x | psutil/tests/test_misc.py | 1 |
3 files changed, 12 insertions, 12 deletions
diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 1c742afb..c55aedd7 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -2855,7 +2855,7 @@ psutil_proc_memory_maps(PyObject *self, PyObject *args) { HANDLE hProcess = NULL; PVOID baseAddress; PVOID previousAllocationBase; - CHAR mappedFileName[MAX_PATH]; + LPWSTR mappedFileName[MAX_PATH]; SYSTEM_INFO system_info; LPVOID maxAddr; PyObject *py_retlist = PyList_New(0); @@ -2881,20 +2881,13 @@ psutil_proc_memory_maps(PyObject *self, PyObject *args) { py_tuple = NULL; if (baseAddress > maxAddr) break; - if (GetMappedFileNameA(hProcess, baseAddress, mappedFileName, + if (GetMappedFileNameW(hProcess, baseAddress, mappedFileName, sizeof(mappedFileName))) { - -#if PY_MAJOR_VERSION >= 3 - py_str = PyUnicode_Decode( - mappedFileName, _tcslen(mappedFileName), - Py_FileSystemDefaultEncoding, "surrogateescape"); -#else - py_str = Py_BuildValue("s", mappedFileName); -#endif + py_str = PyUnicode_FromWideChar(mappedFileName, + wcslen(mappedFileName)); if (py_str == NULL) goto error; - #ifdef _WIN64 py_tuple = Py_BuildValue( "(KsOI)", diff --git a/psutil/_pswindows.py b/psutil/_pswindows.py index 54b094b3..67fdcc87 100644 --- a/psutil/_pswindows.py +++ b/psutil/_pswindows.py @@ -36,11 +36,12 @@ from ._common import isfile_strict from ._common import parse_environ_block from ._common import sockfam_to_enum from ._common import socktype_to_enum -from ._common import usage_percent from ._common import memoize_when_activated +from ._common import usage_percent from ._compat import long from ._compat import lru_cache from ._compat import PY3 +from ._compat import unicode from ._compat import xrange from ._psutil_windows import ABOVE_NORMAL_PRIORITY_CLASS from ._psutil_windows import BELOW_NORMAL_PRIORITY_CLASS @@ -754,6 +755,11 @@ class Process(object): raise else: for addr, perm, path, rss in raw: + # TODO: refactor + assert isinstance(path, unicode), path + if not PY3: + path = path.encode( + sys.getfilesystemencoding(), errors='replace') path = convert_dos_path(path) addr = hex(addr) yield (addr, perm, path, rss) diff --git a/psutil/tests/test_misc.py b/psutil/tests/test_misc.py index 29012cd4..272253b6 100755 --- a/psutil/tests/test_misc.py +++ b/psutil/tests/test_misc.py @@ -503,6 +503,7 @@ class TestScripts(unittest.TestCase): self.assert_stdout('fans.py') @unittest.skipIf(not HAS_SENSORS_BATTERY, "not supported") + @unittest.skipIf(TRAVIS, "not battery on TRAVIS") def test_battery(self): self.assert_stdout('battery.py') |