Skip to content

Commit

Permalink
fix #564: add a sanity check to make sure the python module is not us…
Browse files Browse the repository at this point in the history
…ing a C extension module whose version is different than the current one
  • Loading branch information
giampaolo committed Jan 2, 2015
1 parent 9286baf commit 2567366
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 2 deletions.
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Bug tracker at https://github.com/giampaolo/psutil/issues

- #521: drop support for Python 2.4 and 2.5.
- #553: new examples/pstree.py script.
- #564: C extension version mismatch in case the user messed up with psutil
installation or with sys.path is now detected at import time.
- #569: [FreeBSD] add support for process CPU affinity.

**Bug fixes**
Expand Down
14 changes: 14 additions & 0 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,20 @@
_timer = getattr(time, 'monotonic', time.time)


# Sanity check in case the user messed up with psutil installation
# or did something weird with sys.path. In this case we might end
# up importing a python module using a C extension module which
# was compiled for a different version of psutil.
# We want to prevent that by failing sooner rather than later.
# See: https://github.com/giampaolo/psutil/issues/564
if (int(__version__.replace('.', '')) !=
getattr(_psplatform.cext, 'version', None)):
msg = "version conflict: %r C extension module was built for another " \
"version of psutil (different than %s)" % (_psplatform.cext.__file__,
__version__)
raise ImportError(msg)


# =====================================================================
# --- exceptions
# =====================================================================
Expand Down
2 changes: 2 additions & 0 deletions psutil/_psutil_bsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -2308,6 +2308,8 @@ void init_psutil_bsd(void)
#else
PyObject *module = Py_InitModule("_psutil_bsd", PsutilMethods);
#endif
PyModule_AddIntConstant(module, "version", _PSUTIL_VERSION);

// process status constants
PyModule_AddIntConstant(module, "SSTOP", SSTOP);
PyModule_AddIntConstant(module, "SSLEEP", SSLEEP);
Expand Down
1 change: 1 addition & 0 deletions psutil/_psutil_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ void init_psutil_linux(void)
#endif


PyModule_AddIntConstant(module, "version", _PSUTIL_VERSION);
#if PSUTIL_HAVE_PRLIMIT
PyModule_AddIntConstant(module, "RLIM_INFINITY", RLIM_INFINITY);
PyModule_AddIntConstant(module, "RLIMIT_AS", RLIMIT_AS);
Expand Down
1 change: 1 addition & 0 deletions psutil/_psutil_osx.c
Original file line number Diff line number Diff line change
Expand Up @@ -1849,6 +1849,7 @@ init_psutil_osx(void)
#else
PyObject *module = Py_InitModule("_psutil_osx", PsutilMethods);
#endif
PyModule_AddIntConstant(module, "version", _PSUTIL_VERSION);
// process status constants, defined in:
// http://fxr.watson.org/fxr/source/bsd/sys/proc.h?v=xnu-792.6.70#L149
PyModule_AddIntConstant(module, "SIDL", SIDL);
Expand Down
2 changes: 2 additions & 0 deletions psutil/_psutil_sunos.c
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,8 @@ void init_psutil_sunos(void)
#else
PyObject *module = Py_InitModule("_psutil_sunos", PsutilMethods);
#endif
PyModule_AddIntConstant(module, "version", _PSUTIL_VERSION);

PyModule_AddIntConstant(module, "SSLEEP", SSLEEP);
PyModule_AddIntConstant(module, "SRUN", SRUN);
PyModule_AddIntConstant(module, "SZOMB", SZOMB);
Expand Down
2 changes: 2 additions & 0 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -3163,6 +3163,8 @@ void init_psutil_windows(void)
INITERROR;
}

PyModule_AddIntConstant(module, "version", _PSUTIL_VERSION);

// process status constants
// http://msdn.microsoft.com/en-us/library/ms683211(v=vs.85).aspx
PyModule_AddIntConstant(
Expand Down
13 changes: 11 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def get_description():
return f.read()


VERSION = get_version()
VERSION_MACRO = ('_PSUTIL_VERSION', int(VERSION.replace('.', '')))


# POSIX
if os.name == 'posix':
posix_extension = Extension(
Expand All @@ -63,6 +67,7 @@ def get_winver():
'psutil/arch/windows/security.c',
],
define_macros=[
VERSION_MACRO,
# be nice to mingw, see:
# http://www.mingw.org/wiki/Use_more_recent_defined_functions
('_WIN32_WINNT', get_winver()),
Expand All @@ -86,6 +91,7 @@ def get_winver():
'psutil/_psutil_common.c',
'psutil/arch/osx/process_info.c'
],
define_macros=[VERSION_MACRO],
extra_link_args=[
'-framework', 'CoreFoundation', '-framework', 'IOKit'
],
Expand All @@ -101,21 +107,24 @@ def get_winver():
'psutil/_psutil_common.c',
'psutil/arch/bsd/process_info.c'
],
define_macros=[VERSION_MACRO],
libraries=["devstat"]),
posix_extension,
]
# Linux
elif sys.platform.startswith("linux"):
extensions = [Extension(
'_psutil_linux',
sources=['psutil/_psutil_linux.c']),
sources=['psutil/_psutil_linux.c'],
define_macros=[VERSION_MACRO]),
posix_extension,
]
# Solaris
elif sys.platform.lower().startswith('sunos'):
extensions = [Extension(
'_psutil_sunos',
sources=['psutil/_psutil_sunos.c'],
define_macros=[VERSION_MACRO],
libraries=['kstat', 'nsl'],),
posix_extension,
]
Expand All @@ -126,7 +135,7 @@ def get_winver():
def main():
setup_args = dict(
name='psutil',
version=get_version(),
version=VERSION,
description=__doc__.replace('\n', '').strip(),
long_description=get_description(),
keywords=[
Expand Down

0 comments on commit 2567366

Please sign in to comment.