Skip to content

Commit

Permalink
re #1040: bif one win/ unicode / memory_maps()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
giampaolo committed May 3, 2017
1 parent c2e8ee3 commit ace8d28
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
15 changes: 4 additions & 11 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)",
Expand Down
8 changes: 7 additions & 1 deletion psutil/_pswindows.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@
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
Expand Down Expand Up @@ -754,6 +755,11 @@ def memory_maps(self):
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)
Expand Down
1 change: 1 addition & 0 deletions psutil/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ def test_fans(self):
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')

Expand Down

0 comments on commit ace8d28

Please sign in to comment.