Skip to content

Commit

Permalink
Merge branch 'master' of github.com:giampaolo/psutil
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Feb 12, 2015
2 parents dbaa3b9 + 72be6f4 commit 7dfa88d
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 48 deletions.
3 changes: 2 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Bug tracker at https://github.com/giampaolo/psutil/issues

- #376: new psutil.net_if_addrs() returning all NIC addresses a-la ifconfig.
- #469: on Python >= 3.4 ``IOPRIO_CLASS_*`` and ``*_PRIORITY_CLASS`` constants
returned by psutil.Process.ionice() are enums instead of plain integers.
returned by psutil.Process' ionice() and nice() methods are enums instead of
plain integers.
- #581: add .gitignore. (patch by Gabi Davar)
- #582: connection constants returned by psutil.net_connections() and
psutil.Process.connections() were turned from int to enums on Python > 3.4.
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ build: clean
$(PYTHON) setup.py build
@# copies *.so files in ./psutil directory in order to allow
@# "import psutil" when using the interactive interpreter from within
@# this directory .
@# this directory.
$(PYTHON) setup.py build_ext -i

install: build
Expand Down
94 changes: 62 additions & 32 deletions make.bat
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ rem ...therefore it might not work on your Windows installation.
rem
rem By default C:\Python27\python.exe is used.
rem To compile for a specific Python version run:
rem set PYTHON=C:\Python34\python.exe & make.bat build
rem
rem set PYTHON=C:\Python26\python.exe & make.bat build
rem
rem To use a different test script:
rem set PYTHON=C:\Python34\python.exe & set TSCRIPT=foo.py & make.bat test
rem ==========================================================================

if "%PYTHON%" == "" (
Expand All @@ -39,6 +40,7 @@ if "%1" == "help" (
echo build compile without installing
echo build-exes create exe installers in dist directory
echo build-wheels create wheel installers in dist directory
echo build-all build exes + wheels
echo clean clean build files
echo install compile and install
echo setup-env install pip, unittest2, wheels for all python versions
Expand All @@ -49,6 +51,7 @@ if "%1" == "help" (
echo uninstall uninstall
echo upload-exes upload exe installers on pypi
echo upload-wheels upload wheel installers on pypi
echo upload-all upload exes + wheels
goto :eof
)

Expand All @@ -69,13 +72,19 @@ if "%1" == "clean" (
if "%1" == "build" (
:build
%PYTHON% setup.py build
if %errorlevel% neq 0 goto :error
rem copies *.pyd files in ./psutil directory in order to allow
rem "import psutil" when using the interactive interpreter from
rem within this directory.
%PYTHON% setup.py build_ext -i
if %errorlevel% neq 0 goto :error
goto :eof
)

if "%1" == "install" (
:install
%PYTHON% setup.py build install
call :build
%PYTHON% setup.py install
goto :eof
)

Expand Down Expand Up @@ -137,35 +146,6 @@ if "%1" == "build-exes" (
goto :eof
)

if "%1" == "upload-exes" (
:upload-exes
rem "standard" 32 bit versions, using VS 2008 (2.6, 2.7) or VS 2010 (3.3+)
C:\Python26\python.exe setup.py bdist_wininst upload || goto :error
C:\Python27\python.exe setup.py bdist_wininst upload || goto :error
C:\Python33\python.exe setup.py bdist_wininst upload || goto :error
C:\Python34\python.exe setup.py bdist_wininst upload || goto :error
rem 64 bit versions
C:\Python27-64\python.exe setup.py build bdist_wininst upload || goto :error
C:\Python33-64\python.exe setup.py build bdist_wininst upload || goto :error
C:\Python34-64\python.exe setup.py build bdist_wininst upload || goto :error
echo OK
goto :eof
)

if "%1" == "setup-env" (
:setup-env
C:\python27\python.exe -c "import urllib2; url = urllib2.urlopen('https://raw.github.com/pypa/pip/master/contrib/get-pip.py'); data = url.read(); f = open('get-pip.py', 'w'); f.write(data)"
C:\python26\python.exe get-pip.py & C:\python26\scripts\pip install unittest2 wheel --upgrade
C:\python27\python.exe get-pip.py & C:\python27\scripts\pip install wheel --upgrade
C:\python33\python.exe get-pip.py & C:\python33\scripts\pip install wheel --upgrade
C:\python34\scripts\easy_install.exe wheel
rem 64-bit versions
C:\python27-64\python.exe get-pip.py & C:\python27-64\scripts\pip install wheel --upgrade
C:\python33-64\python.exe get-pip.py & C:\python33-64\scripts\pip install wheel --upgrade
C:\python34-64\scripts\easy_install.exe wheel
goto :eof
)

if "%1" == "build-wheels" (
:build-wheels
C:\Python26\python.exe setup.py build bdist_wheel || goto :error
Expand All @@ -184,6 +164,30 @@ if "%1" == "build-wheels" (
goto :eof
)

if "%1" == "build-all" (
:build-all
rem for some reason this needs to be called twice (f**king windows...)
call :build-exes
call :build-exes
echo OK
goto :eof
)

if "%1" == "upload-exes" (
:upload-exes
rem "standard" 32 bit versions, using VS 2008 (2.6, 2.7) or VS 2010 (3.3+)
C:\Python26\python.exe setup.py bdist_wininst upload || goto :error
C:\Python27\python.exe setup.py bdist_wininst upload || goto :error
C:\Python33\python.exe setup.py bdist_wininst upload || goto :error
C:\Python34\python.exe setup.py bdist_wininst upload || goto :error
rem 64 bit versions
C:\Python27-64\python.exe setup.py build bdist_wininst upload || goto :error
C:\Python33-64\python.exe setup.py build bdist_wininst upload || goto :error
C:\Python34-64\python.exe setup.py build bdist_wininst upload || goto :error
echo OK
goto :eof
)

if "%1" == "upload-wheels" (
:build-wheels
C:\Python26\python.exe setup.py build bdist_wheel upload || goto :error
Expand All @@ -202,6 +206,32 @@ if "%1" == "upload-wheels" (
goto :eof
)

if "%1" == "upload-all" (
:build-all
call :upload-exes
call :upload-wheels
echo OK
goto :eof
)

if "%1" == "setup-env" (
:setup-env
echo downloading pip installer
C:\python27\python.exe -c "import urllib2; url = urllib2.urlopen('https://raw.github.com/pypa/pip/master/contrib/get-pip.py'); data = url.read(); f = open('get-pip.py', 'w'); f.write(data)"
C:\python26\python.exe get-pip.py & C:\python26\scripts\pip install unittest2 wheel ipaddress --upgrade
C:\python27\python.exe get-pip.py & C:\python27\scripts\pip install wheel ipaddress --upgrade
C:\python33\python.exe get-pip.py & C:\python33\scripts\pip install wheel ipaddress --upgrade
C:\python34\scripts\easy_install.exe wheel
rem 64-bit versions
C:\python27-64\python.exe get-pip.py & C:\python27-64\scripts\pip install wheel ipaddress --upgrade
C:\python33-64\python.exe get-pip.py & C:\python33-64\scripts\pip install wheel ipaddress --upgrade
C:\python34-64\scripts\easy_install.exe wheel
rem install ipdb only for py 2.7 and 3.4
C:\python27\scripts\pip install ipdb --upgrade
C:\python34\scripts\easy_install.exe ipdb
goto :eof
)

goto :help

:error
Expand Down
3 changes: 2 additions & 1 deletion psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1850,7 +1850,8 @@ def net_if_addrs():
try:
fam = socket.AddressFamily(fam)
except ValueError:
pass
if os.name == 'nt' and fam == -1:
fam = _psplatform.AF_LINK
ret[name].append(_common.snic(fam, addr, mask, broadcast))
return dict(ret)

Expand Down
11 changes: 11 additions & 0 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -3069,7 +3069,14 @@ psutil_net_if_addrs(PyObject *self, PyObject *args)
}
*--ptr = '\0';

#if PY_MAJOR_VERSION >= 3
py_mac_address = PyUnicode_FromString(buff);
#else
py_mac_address = PyString_FromString(buff);
#endif
if (py_mac_address == NULL)
goto error;

Py_INCREF(Py_None);
Py_INCREF(Py_None);
py_tuple = Py_BuildValue(
Expand Down Expand Up @@ -3114,7 +3121,11 @@ psutil_net_if_addrs(PyObject *self, PyObject *args)
PyErr_SetFromWindowsErr(GetLastError());
goto error;
}
#if PY_MAJOR_VERSION >= 3
py_address = PyUnicode_FromString(buff);
#else
py_address = PyString_FromString(buff);
#endif
if (py_address == NULL)
goto error;

Expand Down
30 changes: 19 additions & 11 deletions psutil/_pswindows.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from . import _psutil_windows as cext
from ._common import conn_tmap, usage_percent, isfile_strict
from ._common import sockfam_to_enum, socktype_to_enum
from ._compat import PY3, xrange, lru_cache
from ._compat import PY3, xrange, lru_cache, long
from ._psutil_windows import (ABOVE_NORMAL_PRIORITY_CLASS,
BELOW_NORMAL_PRIORITY_CLASS,
HIGH_PRIORITY_CLASS,
Expand All @@ -34,8 +34,8 @@
__extra__all__ = ["ABOVE_NORMAL_PRIORITY_CLASS", "BELOW_NORMAL_PRIORITY_CLASS",
"HIGH_PRIORITY_CLASS", "IDLE_PRIORITY_CLASS",
"NORMAL_PRIORITY_CLASS", "REALTIME_PRIORITY_CLASS",
#
"CONN_DELETE_TCB",
"AF_LINK",
]

# --- module level constants (gets pushed up to psutil module)
Expand All @@ -44,7 +44,11 @@
WAIT_TIMEOUT = 0x00000102 # 258 in decimal
ACCESS_DENIED_SET = frozenset([errno.EPERM, errno.EACCES,
cext.ERROR_ACCESS_DENIED])
AF_LINK = -1
if enum is None:
AF_LINK = -1
else:
AddressFamily = enum.IntEnum('AddressFamily', {'AF_LINK': -1})
AF_LINK = AddressFamily.AF_LINK

TCP_STATUSES = {
cext.MIB_TCP_STATE_ESTAB: _common.CONN_ESTABLISHED,
Expand All @@ -63,15 +67,15 @@
}

if enum is not None:
class IOPriority(enum.IntEnum):
class Priority(enum.IntEnum):
ABOVE_NORMAL_PRIORITY_CLASS = ABOVE_NORMAL_PRIORITY_CLASS
BELOW_NORMAL_PRIORITY_CLASS = BELOW_NORMAL_PRIORITY_CLASS
HIGH_PRIORITY_CLASS = HIGH_PRIORITY_CLASS
IDLE_PRIORITY_CLASS = IDLE_PRIORITY_CLASS
NORMAL_PRIORITY_CLASS = NORMAL_PRIORITY_CLASS
REALTIME_PRIORITY_CLASS = REALTIME_PRIORITY_CLASS

globals().update(IOPriority.__members__)
globals().update(Priority.__members__)


scputimes = namedtuple('scputimes', ['user', 'system', 'idle'])
Expand Down Expand Up @@ -429,7 +433,10 @@ def connections(self, kind='inet'):

@wrap_exceptions
def nice_get(self):
return cext.proc_priority_get(self.pid)
value = cext.proc_priority_get(self.pid)
if enum is not None:
value = Priority(value)
return value

@wrap_exceptions
def nice_set(self, value):
Expand All @@ -439,10 +446,7 @@ def nice_set(self, value):
if hasattr(cext, "proc_io_priority_get"):
@wrap_exceptions
def ionice_get(self):
value = cext.proc_io_priority_get(self.pid)
if enum is not None:
value = IOPriority(value)
return value
return cext.proc_io_priority_get(self.pid)

@wrap_exceptions
def ionice_set(self, value, _):
Expand Down Expand Up @@ -496,7 +500,11 @@ def to_bitmask(l):
allcpus = list(range(len(per_cpu_times())))
for cpu in value:
if cpu not in allcpus:
raise ValueError("invalid CPU %r" % cpu)
if not isinstance(cpu, (int, long)):
raise TypeError(
"invalid CPU %r; an integer is required" % cpu)
else:
raise ValueError("invalid CPU %r" % cpu)

bitmask = to_bitmask(value)
cext.proc_cpu_affinity_set(self.pid, bitmask)
Expand Down
10 changes: 8 additions & 2 deletions test/test_psutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ def test_net_if_addrs(self):
self.assertIsInstance(addr.broadcast, (str, type(None)))
self.assertIn(addr.family, families)
if sys.version_info >= (3, 4):
self.assertIsInstance(addr.family, socket.AddressFamily)
self.assertIsInstance(addr.family, enum.IntEnum)
if addr.family == socket.AF_INET:
s = socket.socket(addr.family)
with contextlib.closing(s):
Expand Down Expand Up @@ -1405,6 +1405,7 @@ def test_ionice(self):
else:
p = psutil.Process()
original = p.ionice()
self.assertIsInstance(original, int)
try:
value = 0 # very low
if original == value:
Expand Down Expand Up @@ -1619,7 +1620,12 @@ def test_nice(self):
self.assertRaises(TypeError, p.nice, "str")
if WINDOWS:
try:
self.assertEqual(p.nice(), psutil.NORMAL_PRIORITY_CLASS)
init = p.nice()
if sys.version_info > (3, 4):
self.assertIsInstance(init, enum.IntEnum)
else:
self.assertIsInstance(init, int)
self.assertEqual(init, psutil.NORMAL_PRIORITY_CLASS)
p.nice(psutil.HIGH_PRIORITY_CLASS)
self.assertEqual(p.nice(), psutil.HIGH_PRIORITY_CLASS)
p.nice(psutil.NORMAL_PRIORITY_CLASS)
Expand Down

0 comments on commit 7dfa88d

Please sign in to comment.