Skip to content

Commit c0abdbb

Browse files
Address review comments and small refactoring.
1 parent 395860a commit c0abdbb

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

Lib/test/test_posix.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,7 @@ def test_makedev(self):
729729
NODEV = -1
730730
self.assertEqual(posix.major(NODEV), NODEV)
731731
self.assertEqual(posix.minor(NODEV), NODEV)
732+
self.assertEqual(posix.makedev(NODEV, NODEV), NODEV)
732733

733734
def _test_all_chown_common(self, chown_func, first_param, stat_func):
734735
"""Common code for chown, fchown and lchown tests."""
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Fix integer conversion in :func:`os.major`, :func:`os.minor`, and
22
:func:`os.makedev`. Support device numbers larger than ``2**63-1``. Support
3-
non-existent device number.
3+
non-existent device number (``NODEV``).

Modules/posixmodule.c

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12515,6 +12515,28 @@ os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
1251512515
#endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */
1251612516

1251712517

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+
1251812540
#ifdef HAVE_DEVICE_MACROS
1251912541
/*[clinic input]
1252012542
os.major
@@ -12529,13 +12551,7 @@ static PyObject *
1252912551
os_major_impl(PyObject *module, dev_t device)
1253012552
/*[clinic end generated code: output=4071ffee17647891 input=b1a0a14ec9448229]*/
1253112553
{
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));
1253912555
}
1254012556

1254112557

@@ -12552,13 +12568,7 @@ static PyObject *
1255212568
os_minor_impl(PyObject *module, dev_t device)
1255312569
/*[clinic end generated code: output=306cb78e3bc5004f input=2f686e463682a9da]*/
1255412570
{
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));
1256212572
}
1256312573

1256412574

@@ -12576,14 +12586,7 @@ static dev_t
1257612586
os_makedev_impl(PyObject *module, dev_t major, dev_t minor)
1257712587
/*[clinic end generated code: output=cad6125c51f5af80 input=2146126ec02e55c1]*/
1257812588
{
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)) {
1258712590
PyErr_SetString(PyExc_OverflowError,
1258812591
"Python int too large to convert to C unsigned int");
1258912592
return (dev_t)-1;

0 commit comments

Comments
 (0)