Skip to content

Commit

Permalink
Make psutil reloadable on linux
Browse files Browse the repository at this point in the history
For safety reasons, reloading a module does not reinitialize c
extensions. For this reason, `del <c_extension>` in `__init__.py`
yields the following on reimport:

```
Traceback (most recent call last):
  File "/src/test/_linux.py", line 479, in test_psutil_is_reloadable
    imp.reload(psutil)
  File "psutil/__init__.py", line 89, in <module>
    from . import _psutil_linux
ImportError: cannot import name _psutil_linux
```

As none of the other platform-specific extensions are removed from
the module dict post-import, we simply omit the `del` for linux.
  • Loading branch information
sethp-jive committed Dec 3, 2015
1 parent 1152f96 commit a05dccb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
1 change: 0 additions & 1 deletion psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@
RLIMIT_SIGPENDING = _psutil_linux.RLIMIT_SIGPENDING
except AttributeError:
pass
del _psutil_linux

elif sys.platform.startswith("win32"):
from . import _pswindows as _psplatform
Expand Down
7 changes: 7 additions & 0 deletions test/_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
from test_psutil import unittest
from test_psutil import which

if PY3:
import importlib as imp
else:
import imp

# procps-ng 3.3.10 changed the output format of free
# and removed the 'buffers/cache line'
Expand Down Expand Up @@ -471,6 +475,9 @@ def test_procfs_path(self):
psutil.PROCFS_PATH = "/proc"
os.rmdir(tdir)

def test_psutil_is_reloadable(self):
imp.reload(psutil)

# --- tests for specific kernel versions

@unittest.skipUnless(
Expand Down

0 comments on commit a05dccb

Please sign in to comment.