Skip to content

Commit d83c959

Browse files
committed
Fixes from review.
Signed-off-by: Chris Lalancette <[email protected]>
1 parent e05f926 commit d83c959

File tree

3 files changed

+81
-60
lines changed

3 files changed

+81
-60
lines changed

docs/index.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ Network
735735
- **speed**: the NIC speed expressed in mega bits (MB), if it can't be
736736
determined (e.g. 'localhost') it will be set to ``0``.
737737
- **mtu**: NIC's maximum transmission unit expressed in bytes.
738-
- **flags**: a string of comma-separated flags on the interface (may be the empty string).
738+
- **flags**: a string of comma-separated flags on the interface (may be an empty string).
739739
Possible flags are: ``up``, ``broadcast``, ``debug``, ``loopback``,
740740
``pointopoint``, ``notrailers``, ``running``, ``noarp``, ``promisc``,
741741
``allmulti``, ``master``, ``slave``, ``multicast``, ``portsel``,
@@ -755,7 +755,7 @@ Network
755755

756756
.. versionchanged:: 5.7.3 `isup` on UNIX also checks whether the NIC is running.
757757

758-
.. versionchanged:: 5.9.1 *flags* field was added on POSIX.
758+
.. versionchanged:: 5.9.2 *flags* field was added on POSIX.
759759

760760
Sensors
761761
-------

psutil/_psutil_posix.c

+78-58
Original file line numberDiff line numberDiff line change
@@ -430,23 +430,21 @@ psutil_net_if_mtu(PyObject *self, PyObject *args) {
430430
return PyErr_SetFromErrno(PyExc_OSError);
431431
}
432432

433-
static bool
434-
check_and_append_iff_flag(PyObject *py_retlist, short int flags, short int flag_to_check, const char * flag_name)
433+
static int
434+
append_flag(PyObject *py_retlist, const char * flag_name)
435435
{
436436
PyObject *py_str = NULL;
437437

438-
if (flags & flag_to_check) {
439-
py_str = PyUnicode_DecodeFSDefault(flag_name);
440-
if (! py_str)
441-
return false;
442-
if (PyList_Append(py_retlist, py_str)) {
443-
Py_DECREF(py_str);
444-
return false;
445-
}
446-
Py_CLEAR(py_str);
438+
py_str = PyUnicode_DecodeFSDefault(flag_name);
439+
if (! py_str)
440+
return 0;
441+
if (PyList_Append(py_retlist, py_str)) {
442+
Py_DECREF(py_str);
443+
return 0;
447444
}
445+
Py_CLEAR(py_str);
448446

449-
return true;
447+
return 1;
450448
}
451449

452450
/*
@@ -469,14 +467,14 @@ psutil_net_if_flags(PyObject *self, PyObject *args) {
469467

470468
sock = socket(AF_INET, SOCK_DGRAM, 0);
471469
if (sock == -1) {
472-
PyErr_SetFromErrno(PyExc_OSError);
470+
PyErr_SetFromOSErrnoWithSyscall("socket(SOCK_DGRAM)");
473471
goto error;
474472
}
475473

476474
PSUTIL_STRNCPY(ifr.ifr_name, nic_name, sizeof(ifr.ifr_name));
477475
ret = ioctl(sock, SIOCGIFFLAGS, &ifr);
478476
if (ret == -1) {
479-
PyErr_SetFromErrno(PyExc_OSError);
477+
PyErr_SetFromOSErrnoWithSyscall("ioctl(SIOCGIFFLAGS)");
480478
goto error;
481479
}
482480

@@ -492,113 +490,135 @@ psutil_net_if_flags(PyObject *self, PyObject *args) {
492490

493491
#ifdef IFF_UP
494492
// Available in (at least) Linux, macOS, AIX, BSD
495-
if (!check_and_append_iff_flag(py_retlist, flags, IFF_UP, "up"))
496-
goto error;
493+
if (flags & IFF_UP)
494+
if (!append_flag(py_retlist, "up"))
495+
goto error;
497496
#endif
498497
#ifdef IFF_BROADCAST
499498
// Available in (at least) Linux, macOS, AIX, BSD
500-
if (!check_and_append_iff_flag(py_retlist, flags, IFF_BROADCAST, "broadcast"))
501-
goto error;
499+
if (flags & IFF_BROADCAST)
500+
if (!append_flag(py_retlist, "broadcast"))
501+
goto error;
502502
#endif
503503
#ifdef IFF_DEBUG
504504
// Available in (at least) Linux, macOS, BSD
505-
if (!check_and_append_iff_flag(py_retlist, flags, IFF_DEBUG, "debug"))
506-
goto error;
505+
if (flags & IFF_DEBUG)
506+
if (!append_flag(py_retlist, "debug"))
507+
goto error;
507508
#endif
508509
#ifdef IFF_LOOPBACK
509510
// Available in (at least) Linux, macOS, BSD
510-
if (!check_and_append_iff_flag(py_retlist, flags, IFF_LOOPBACK, "loopback"))
511-
goto error;
511+
if (flags & IFF_LOOPBACK)
512+
if (!append_flag(py_retlist, "loopback"))
513+
goto error;
512514
#endif
513515
#ifdef IFF_POINTOPOINT
514516
// Available in (at least) Linux, macOS, BSD
515-
if (!check_and_append_iff_flag(py_retlist, flags, IFF_POINTOPOINT, "pointopoint"))
516-
goto error;
517+
if (flags & IFF_POINTOPOINT)
518+
if (!append_flag(py_retlist, "pointopoint"))
519+
goto error;
517520
#endif
518521
#ifdef IFF_NOTRAILERS
519522
// Available in (at least) Linux, macOS, AIX
520-
if (!check_and_append_iff_flag(py_retlist, flags, IFF_NOTRAILERS, "notrailers"))
521-
goto error;
523+
if (flags & IFF_NOTRAILERS)
524+
if (!append_flag(py_retlist, "notrailers"))
525+
goto error;
522526
#endif
523527
#ifdef IFF_RUNNING
524528
// Available in (at least) Linux, macOS, AIX, BSD
525-
if (!check_and_append_iff_flag(py_retlist, flags, IFF_RUNNING, "running"))
526-
goto error;
529+
if (flags & IFF_RUNNING)
530+
if (!append_flag(py_retlist, "running"))
531+
goto error;
527532
#endif
528533
#ifdef IFF_NOARP
529534
// Available in (at least) Linux, macOS, BSD
530-
if (!check_and_append_iff_flag(py_retlist, flags, IFF_NOARP, "noarp"))
531-
goto error;
535+
if (flags & IFF_NOARP)
536+
if (!append_flag(py_retlist, "noarp"))
537+
goto error;
532538
#endif
533539
#ifdef IFF_PROMISC
534540
// Available in (at least) Linux, macOS, BSD
535-
if (!check_and_append_iff_flag(py_retlist, flags, IFF_PROMISC, "promisc"))
536-
goto error;
541+
if (flags & IFF_PROMISC)
542+
if (!append_flag(py_retlist, "promisc"))
543+
goto error;
537544
#endif
538545
#ifdef IFF_ALLMULTI
539546
// Available in (at least) Linux, macOS, BSD
540-
if (!check_and_append_iff_flag(py_retlist, flags, IFF_ALLMULTI, "allmulti"))
541-
goto error;
547+
if (flags & IFF_ALLMULTI)
548+
if (!append_flag(py_retlist, "allmulti"))
549+
goto error;
542550
#endif
543551
#ifdef IFF_MASTER
544552
// Available in (at least) Linux
545-
if (!check_and_append_iff_flag(py_retlist, flags, IFF_MASTER, "master"))
546-
goto error;
553+
if (flags & IFF_MASTER)
554+
if (!append_flag(py_retlist, "master"))
555+
goto error;
547556
#endif
548557
#ifdef IFF_SLAVE
549558
// Available in (at least) Linux
550-
if (!check_and_append_iff_flag(py_retlist, flags, IFF_SLAVE, "slave"))
551-
goto error;
559+
if (flags & IFF_SLAVE)
560+
if (!append_flag(py_retlist, "slave"))
561+
goto error;
552562
#endif
553563
#ifdef IFF_MULTICAST
554564
// Available in (at least) Linux, macOS, BSD
555-
if (!check_and_append_iff_flag(py_retlist, flags, IFF_MULTICAST, "multicast"))
556-
goto error;
565+
if (flags & IFF_MULTICAST)
566+
if (!append_flag(py_retlist, "multicast"))
567+
goto error;
557568
#endif
558569
#ifdef IFF_PORTSEL
559570
// Available in (at least) Linux
560-
if (!check_and_append_iff_flag(py_retlist, flags, IFF_PORTSEL, "portsel"))
561-
goto error;
571+
if (flags & IFF_PORTSEL)
572+
if (!append_flag(py_retlist, "portsel"))
573+
goto error;
562574
#endif
563575
#ifdef IFF_AUTOMEDIA
564576
// Available in (at least) Linux
565-
if (!check_and_append_iff_flag(py_retlist, flags, IFF_AUTOMEDIA, "automedia"))
566-
goto error;
577+
if (flags & IFF_AUTOMEDIA)
578+
if (!append_flag(py_retlist, "automedia"))
579+
goto error;
567580
#endif
568581
#ifdef IFF_DYNAMIC
569582
// Available in (at least) Linux
570-
if (!check_and_append_iff_flag(py_retlist, flags, IFF_DYNAMIC, "dynamic"))
571-
goto error;
583+
if (flags & IFF_DYNAMIC)
584+
if (!append_flag(py_retlist, "dynamic"))
585+
goto error;
572586
#endif
573587
#ifdef IFF_OACTIVE
574588
// Available in (at least) macOS, BSD
575-
if (!check_and_append_iff_flag(py_retlist, flags, IFF_OACTIVE, "oactive"))
576-
goto error;
589+
if (flags & IFF_OACTIVE)
590+
if (!append_flag(py_retlist, "oactive"))
591+
goto error;
577592
#endif
578593
#ifdef IFF_SIMPLEX
579594
// Available in (at least) macOS, AIX, BSD
580-
if (!check_and_append_iff_flag(py_retlist, flags, IFF_SIMPLEX, "simplex"))
581-
goto error;
595+
if (flags & IFF_SIMPLEX)
596+
if (!append_flag(py_retlist, "simplex"))
597+
goto error;
582598
#endif
583599
#ifdef IFF_LINK0
584600
// Available in (at least) macOS, BSD
585-
if (!check_and_append_iff_flag(py_retlist, flags, IFF_LINK0, "link0"))
586-
goto error;
601+
if (flags & IFF_LINK0)
602+
if (!append_flag(py_retlist, "link0"))
603+
goto error;
587604
#endif
588605
#ifdef IFF_LINK1
589606
// Available in (at least) macOS, BSD
590-
if (!check_and_append_iff_flag(py_retlist, flags, IFF_LINK1, "link1"))
591-
goto error;
607+
if (flags & IFF_LINK1)
608+
if (!append_flag(py_retlist, "link1"))
609+
goto error;
592610
#endif
593611
#ifdef IFF_LINK2
594612
// Available in (at least) macOS, BSD
595-
if (!check_and_append_iff_flag(py_retlist, flags, IFF_LINK2, "link2"))
596-
goto error;
613+
if (flags & IFF_LINK2)
614+
if (!append_flag(py_retlist, "link2"))
615+
goto error;
597616
#endif
598617
#ifdef IFF_D2
599618
// Available in (at least) AIX
600-
if (!check_and_append_iff_flag(py_retlist, flags, IFF_D2, "d2"))
601-
goto error;
619+
if (flags & IFF_D2)
620+
if (!append_flag(py_retlist, "d2"))
621+
goto error;
602622
#endif
603623

604624
return py_retlist;

psutil/tests/test_system.py

+1
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,7 @@ def test_net_if_stats(self):
814814
self.assertIn(duplex, all_duplexes)
815815
self.assertGreaterEqual(speed, 0)
816816
self.assertGreaterEqual(mtu, 0)
817+
self.assertIsInstance(flags, str)
817818

818819
@unittest.skipIf(not (LINUX or BSD or MACOS),
819820
"LINUX or BSD or MACOS specific")

0 commit comments

Comments
 (0)