summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2017-05-02 07:10:46 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2017-05-02 07:10:46 +0200
commit4c023838c0459f77e8d528ce31b2f99ff96944fc (patch)
treeab2ad455e6924ab48623a7205d897890c6ac1d22
parent1bbbda57700681c5d311132e04d074257a7f08a8 (diff)
downloadpsutil-4c023838c0459f77e8d528ce31b2f99ff96944fc.tar.gz
refactor copyload_shared_lib
-rw-r--r--psutil/tests/__init__.py11
-rwxr-xr-xpsutil/tests/test_process.py8
-rw-r--r--psutil/tests/test_unicode.py6
-rwxr-xr-xscripts/internal/winmake.py2
4 files changed, 17 insertions, 10 deletions
diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py
index 3ccb3044..7c14680b 100644
--- a/psutil/tests/__init__.py
+++ b/psutil/tests/__init__.py
@@ -1025,16 +1025,19 @@ def copyload_shared_lib(dst_prefix=TESTFILE_PREFIX):
"""Ctx manager which picks up a random shared so/dll lib used
by this process, copies it in another location and loads it
in memory via ctypes.
- Return the new absolutized path.
+ Return the new absolutized, normcased path.
"""
ext = ".so" if POSIX else ".dll"
dst = tempfile.mktemp(prefix=dst_prefix, suffix=ext)
libs = [x.path for x in psutil.Process().memory_maps()
- if os.path.normcase(x.path).endswith(ext)]
+ if os.path.normcase(os.path.splitext(x.path)[1]) == ext]
src = random.choice(libs)
+ cfile = None
try:
shutil.copyfile(src, dst)
- ctypes.CDLL(dst)
- yield os.path.realpath(dst)
+ cfile = ctypes.CDLL(dst)
+ yield dst
finally:
+ if WINDOWS and cfile is not None:
+ ctypes.windll.kernel32.FreeLibrary(cfile._handle)
safe_rmpath(dst)
diff --git a/psutil/tests/test_process.py b/psutil/tests/test_process.py
index 86a784d2..7bb83b63 100755
--- a/psutil/tests/test_process.py
+++ b/psutil/tests/test_process.py
@@ -656,11 +656,13 @@ class TestProcess(unittest.TestCase):
@unittest.skipIf(not HAS_MEMORY_MAPS, "not supported")
def test_memory_maps_lists_lib(self):
+ # Make sure a newly loaded shared lib is listed.
with copyload_shared_lib() as path:
- # Make sure a newly loaded shared lib is listed.
- libpaths = [os.path.realpath(os.path.normcase(x.path))
+ def normpath(p):
+ return os.path.realpath(os.path.normcase(p))
+ libpaths = [normpath(x.path)
for x in psutil.Process().memory_maps()]
- self.assertIn(path, libpaths)
+ self.assertIn(normpath(path), libpaths)
def test_memory_percent(self):
p = psutil.Process()
diff --git a/psutil/tests/test_unicode.py b/psutil/tests/test_unicode.py
index 2599b2f8..f67e14c2 100644
--- a/psutil/tests/test_unicode.py
+++ b/psutil/tests/test_unicode.py
@@ -257,9 +257,11 @@ class _BaseFSAPIsTests(object):
raise unittest.SkipTest("ctypes can't handle unicode")
with copyload_shared_lib(dst_prefix=self.funky_name) as funky_path:
- libpaths = [os.path.realpath(os.path.normcase(x.path))
+ def normpath(p):
+ return os.path.realpath(os.path.normcase(p))
+ libpaths = [normpath(x.path)
for x in psutil.Process().memory_maps()]
- self.assertIn(funky_path, libpaths)
+ self.assertIn(normpath(funky_path), libpaths)
for path in libpaths:
self.assertIsInstance(path, str)
diff --git a/scripts/internal/winmake.py b/scripts/internal/winmake.py
index b88149a1..69d4d972 100755
--- a/scripts/internal/winmake.py
+++ b/scripts/internal/winmake.py
@@ -60,7 +60,7 @@ def safe_print(text, file=sys.stdout, flush=False):
Works with Python 2 and 3.
"""
if not isinstance(text, basestring):
- return print(text, file=file, flush=flush)
+ return print(text, file=file)
try:
file.write(text)
except UnicodeEncodeError: