@@ -430,23 +430,21 @@ psutil_net_if_mtu(PyObject *self, PyObject *args) {
430
430
return PyErr_SetFromErrno (PyExc_OSError );
431
431
}
432
432
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 )
435
435
{
436
436
PyObject * py_str = NULL ;
437
437
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 ;
447
444
}
445
+ Py_CLEAR (py_str );
448
446
449
- return true ;
447
+ return 1 ;
450
448
}
451
449
452
450
/*
@@ -469,14 +467,14 @@ psutil_net_if_flags(PyObject *self, PyObject *args) {
469
467
470
468
sock = socket (AF_INET , SOCK_DGRAM , 0 );
471
469
if (sock == -1 ) {
472
- PyErr_SetFromErrno ( PyExc_OSError );
470
+ PyErr_SetFromOSErrnoWithSyscall ( "socket(SOCK_DGRAM)" );
473
471
goto error ;
474
472
}
475
473
476
474
PSUTIL_STRNCPY (ifr .ifr_name , nic_name , sizeof (ifr .ifr_name ));
477
475
ret = ioctl (sock , SIOCGIFFLAGS , & ifr );
478
476
if (ret == -1 ) {
479
- PyErr_SetFromErrno ( PyExc_OSError );
477
+ PyErr_SetFromOSErrnoWithSyscall ( "ioctl(SIOCGIFFLAGS)" );
480
478
goto error ;
481
479
}
482
480
@@ -492,113 +490,135 @@ psutil_net_if_flags(PyObject *self, PyObject *args) {
492
490
493
491
#ifdef IFF_UP
494
492
// 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 ;
497
496
#endif
498
497
#ifdef IFF_BROADCAST
499
498
// 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 ;
502
502
#endif
503
503
#ifdef IFF_DEBUG
504
504
// 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 ;
507
508
#endif
508
509
#ifdef IFF_LOOPBACK
509
510
// 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 ;
512
514
#endif
513
515
#ifdef IFF_POINTOPOINT
514
516
// 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 ;
517
520
#endif
518
521
#ifdef IFF_NOTRAILERS
519
522
// 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 ;
522
526
#endif
523
527
#ifdef IFF_RUNNING
524
528
// 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 ;
527
532
#endif
528
533
#ifdef IFF_NOARP
529
534
// 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 ;
532
538
#endif
533
539
#ifdef IFF_PROMISC
534
540
// 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 ;
537
544
#endif
538
545
#ifdef IFF_ALLMULTI
539
546
// 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 ;
542
550
#endif
543
551
#ifdef IFF_MASTER
544
552
// 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 ;
547
556
#endif
548
557
#ifdef IFF_SLAVE
549
558
// 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 ;
552
562
#endif
553
563
#ifdef IFF_MULTICAST
554
564
// 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 ;
557
568
#endif
558
569
#ifdef IFF_PORTSEL
559
570
// 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 ;
562
574
#endif
563
575
#ifdef IFF_AUTOMEDIA
564
576
// 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 ;
567
580
#endif
568
581
#ifdef IFF_DYNAMIC
569
582
// 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 ;
572
586
#endif
573
587
#ifdef IFF_OACTIVE
574
588
// 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 ;
577
592
#endif
578
593
#ifdef IFF_SIMPLEX
579
594
// 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 ;
582
598
#endif
583
599
#ifdef IFF_LINK0
584
600
// 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 ;
587
604
#endif
588
605
#ifdef IFF_LINK1
589
606
// 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 ;
592
610
#endif
593
611
#ifdef IFF_LINK2
594
612
// 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 ;
597
616
#endif
598
617
#ifdef IFF_D2
599
618
// 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 ;
602
622
#endif
603
623
604
624
return py_retlist ;
0 commit comments