@@ -12515,6 +12515,28 @@ os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
12515
12515
#endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */
12516
12516
12517
12517
12518
+ static PyObject *
12519
+ major_minor_conv (unsigned int value )
12520
+ {
12521
+ #ifdef NODEV
12522
+ if (value == (unsigned int )NODEV ) {
12523
+ return PyLong_FromLong ((int )NODEV );
12524
+ }
12525
+ #endif
12526
+ return PyLong_FromUnsignedLong (value );
12527
+ }
12528
+
12529
+ static int
12530
+ major_minor_check (dev_t value )
12531
+ {
12532
+ #ifdef NODEV
12533
+ if (value == NODEV ) {
12534
+ return 1 ;
12535
+ }
12536
+ #endif
12537
+ return (dev_t )(unsigned int )value == value ;
12538
+ }
12539
+
12518
12540
#ifdef HAVE_DEVICE_MACROS
12519
12541
/*[clinic input]
12520
12542
os.major
@@ -12529,13 +12551,7 @@ static PyObject *
12529
12551
os_major_impl (PyObject * module , dev_t device )
12530
12552
/*[clinic end generated code: output=4071ffee17647891 input=b1a0a14ec9448229]*/
12531
12553
{
12532
- unsigned int result = major (device );
12533
- #ifdef NODEV
12534
- if (result == (unsigned int )NODEV ) {
12535
- return PyLong_FromLong ((int )NODEV );
12536
- }
12537
- #endif
12538
- return PyLong_FromUnsignedLong (result );
12554
+ return major_minor_conv (major (device ));
12539
12555
}
12540
12556
12541
12557
@@ -12552,13 +12568,7 @@ static PyObject *
12552
12568
os_minor_impl (PyObject * module , dev_t device )
12553
12569
/*[clinic end generated code: output=306cb78e3bc5004f input=2f686e463682a9da]*/
12554
12570
{
12555
- unsigned int result = minor (device );
12556
- #ifdef NODEV
12557
- if (result == (unsigned int )NODEV ) {
12558
- return PyLong_FromLong ((int )NODEV );
12559
- }
12560
- #endif
12561
- return PyLong_FromUnsignedLong (result );
12571
+ return major_minor_conv (minor (device ));
12562
12572
}
12563
12573
12564
12574
@@ -12576,14 +12586,7 @@ static dev_t
12576
12586
os_makedev_impl (PyObject * module , dev_t major , dev_t minor )
12577
12587
/*[clinic end generated code: output=cad6125c51f5af80 input=2146126ec02e55c1]*/
12578
12588
{
12579
- #ifdef NODEV
12580
- if ((major != NODEV && (dev_t )(unsigned int )major != major ) ||
12581
- (minor != NODEV && (dev_t )(unsigned int )minor != minor ))
12582
- #else
12583
- if ((dev_t )(unsigned int )major != major ) ||
12584
- (dev_t )(unsigned int )minor != minor ))
12585
- #endif
12586
- {
12589
+ if (!major_minor_check (major ) || !major_minor_check (minor )) {
12587
12590
PyErr_SetString (PyExc_OverflowError ,
12588
12591
"Python int too large to convert to C unsigned int" );
12589
12592
return (dev_t )- 1 ;
0 commit comments