Skip to content

Commit

Permalink
Updated to use better if/else/endif values (my bad)
Browse files Browse the repository at this point in the history
Updated HISTORY to explain better that Win XP still uses 32bit values
Reverted test code, will add in a different PR
  • Loading branch information
jomann09 committed Jun 12, 2016
1 parent c105688 commit 7769dbc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 32 deletions.
4 changes: 4 additions & 0 deletions CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,7 @@ I: 776
N: Farhan Khan
E: [email protected]
I: 823

N: Jake Omann
E: https://github.com/jhomann
I: 816
4 changes: 2 additions & 2 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Bug tracker at https://github.com/giampaolo/psutil/issues
- #823: [NetBSD] virtual_memory() raises TypeError on Python 3.
- #829: [UNIX] psutil.disk_usage() percent field takes root reserved space
into account.
- #816: [Windows] fixed net_io_counter() wrapping after 4.3GB in any fields
on Windows Vista and above.
- #816: [Windows] fixed net_io_counter() values wrapping after 4.3GB in
Windows Vista (NT 6.0) and above using 64bit values from newer win APIs.


4.2.0 - 2016-05-14
Expand Down
37 changes: 18 additions & 19 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@
#include <tchar.h>
#include <tlhelp32.h>
#include <winsock2.h>
#if (_WIN32_WINNT >= 0x0600) // Windows Vista, 7, 8, 8.1, 10
#if (_WIN32_WINNT >= 0x0600) // Windows Vista and above
#include <ws2tcpip.h>
#endif
#include <iphlpapi.h>
#include <wtsapi32.h>
#if (_WIN32_WINNT < 0x0600) // Windows XP / 2000
#include <ws2tcpip.h>
#endif
#include <Winsvc.h>

// Link with Iphlpapi.lib
Expand Down Expand Up @@ -90,7 +87,7 @@ typedef struct _DISK_PERFORMANCE_WIN_2008 {

// --- network connections mingw32 support
#ifndef _IPRTRMIB_H
#if (_WIN32_WINNT < 0x0600) // Windows XP / 2000
#if (_WIN32_WINNT < 0x0600) // Windows XP
typedef struct _MIB_TCP6ROW_OWNER_PID {
UCHAR ucLocalAddr[16];
DWORD dwLocalScopeId;
Expand Down Expand Up @@ -135,7 +132,7 @@ typedef struct _MIB_UDPTABLE_OWNER_PID {
} MIB_UDPTABLE_OWNER_PID, *PMIB_UDPTABLE_OWNER_PID;
#endif

#if (_WIN32_WINNT < 0x0600) // Windows XP / 2000
#if (_WIN32_WINNT < 0x0600) // Windows XP
typedef struct _MIB_UDP6ROW_OWNER_PID {
UCHAR ucLocalAddr[16];
DWORD dwLocalScopeId;
Expand Down Expand Up @@ -2176,12 +2173,13 @@ psutil_disk_usage(PyObject *self, PyObject *args) {
static PyObject *
psutil_net_io_counters(PyObject *self, PyObject *args) {
DWORD dwRetVal = 0;
#if (_WIN32_WINNT >= 0x0600) // Windows Vista, 7, 8, 8.1, 10

#if (_WIN32_WINNT >= 0x0600) // Windows Vista and above
MIB_IF_ROW2 *pIfRow = NULL;
#endif
#if (_WIN32_WINNT < 0x0600) // Windows 2000 / XP
#else // Windows XP
MIB_IFROW *pIfRow = NULL;
#endif

PIP_ADAPTER_ADDRESSES pAddresses = NULL;
PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL;
PyObject *py_retdict = PyDict_New();
Expand All @@ -2198,32 +2196,34 @@ psutil_net_io_counters(PyObject *self, PyObject *args) {
while (pCurrAddresses) {
py_nic_name = NULL;
py_nic_info = NULL;
#if (_WIN32_WINNT >= 0x0600) // Windows Vista, 7, 8, 8.1, 10

#if (_WIN32_WINNT >= 0x0600) // Windows Vista and above
pIfRow = (MIB_IF_ROW2 *) malloc(sizeof(MIB_IF_ROW2));
#endif
#if (_WIN32_WINNT < 0x0600) // Windows 2000 / XP
#else // Windows XP
pIfRow = (MIB_IFROW *) malloc(sizeof(MIB_IFROW));
#endif

if (pIfRow == NULL) {
PyErr_NoMemory();
goto error;
}
#if (_WIN32_WINNT >= 0x0600) // Windows Vista, 7, 8, 8.1, 10

#if (_WIN32_WINNT >= 0x0600) // Windows Vista and above
SecureZeroMemory((PVOID)pIfRow, sizeof(MIB_IF_ROW2));
pIfRow->InterfaceIndex = pCurrAddresses->IfIndex;
dwRetVal = GetIfEntry2(pIfRow);
#endif
#if (_WIN32_WINNT < 0x0600) // Windows 2000 / XP
#else // Windows XP
pIfRow->dwIndex = pCurrAddresses->IfIndex;
dwRetVal = GetIfEntry(pIfRow);
#endif

if (dwRetVal != NO_ERROR) {
PyErr_SetString(PyExc_RuntimeError, "GetIfEntry() or GetIfEntry2() failed.");
PyErr_SetString(PyExc_RuntimeError,
"GetIfEntry() or GetIfEntry2() failed.");
goto error;
}

#if (_WIN32_WINNT >= 0x0600) // Windows Vista, 7, 8, 8.1, 10
#if (_WIN32_WINNT >= 0x0600) // Windows Vista and above
py_nic_info = Py_BuildValue("(KKKKKKKK)",
pIfRow->OutOctets,
pIfRow->InOctets,
Expand All @@ -2233,8 +2233,7 @@ psutil_net_io_counters(PyObject *self, PyObject *args) {
pIfRow->OutErrors,
pIfRow->InDiscards,
pIfRow->OutDiscards);
#endif
#if (_WIN32_WINNT < 0x0600) // Windows 2000 / XP
#else // Windows XP
py_nic_info = Py_BuildValue("(kkkkkkkk)",
pIfRow->dwOutOctets,
pIfRow->dwInOctets,
Expand Down
16 changes: 5 additions & 11 deletions psutil/tests/test_posix.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,25 +314,19 @@ def test_os_waitpid_bad_ret_status(self):

def test_disk_usage(self):
def df(device):
# Use 1 kB block sizes since OS X doesn't have -B flag
out = sh("df -k %s" % device).strip()
out = sh("df -B 1 %s" % device).strip()
line = out.split('\n')[1]
fields = line.split()
total = int(fields[1]) * 1024
used = int(fields[2]) * 1024
free = int(fields[3]) * 1024
total = int(fields[1])
used = int(fields[2])
free = int(fields[3])
percent = float(fields[4].replace('%', ''))
return (total, used, free, percent)

tolerance = 4 * 1024 * 1024 # 4MB
for part in psutil.disk_partitions(all=False):
usage = psutil.disk_usage(part.mountpoint)
try:
total, used, free, percent = df(part.device)
except RuntimeError:
# Issue with OS X not being able to read certain partitions
# Issue with Linux systems not able to read Docker mappings
continue
total, used, free, percent = df(part.device)
self.assertAlmostEqual(usage.total, total, delta=tolerance)
self.assertAlmostEqual(usage.used, used, delta=tolerance)
self.assertAlmostEqual(usage.free, free, delta=tolerance)
Expand Down

0 comments on commit 7769dbc

Please sign in to comment.