Skip to content

Commit 2ba5b75

Browse files
gh-138712: Add os.NODEV
1 parent af58a6f commit 2ba5b75

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

Doc/library/os.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2630,6 +2630,11 @@ features:
26302630
Compose a raw device number from the major and minor device numbers.
26312631

26322632

2633+
.. data:: NODEV
2634+
2635+
Non-existent device.
2636+
2637+
26332638
.. function:: pathconf(path, name)
26342639

26352640
Return system configuration information relevant to a named file. *name*

Lib/test/test_posix.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,12 +757,23 @@ def test_makedev(self):
757757
self.assertRaises((ValueError, OverflowError), posix.makedev, x, minor)
758758
self.assertRaises((ValueError, OverflowError), posix.makedev, major, x)
759759

760-
if sys.platform == 'linux' and not support.linked_to_musl():
761-
NODEV = -1
760+
if (hasattr(posix, 'NODEV') and
761+
sys.platform.startswith(('linux', 'macos', 'freebsd', 'dragonfly',
762+
'sunos'))):
763+
NODEV = posix.NODEV
762764
self.assertEqual(posix.major(NODEV), NODEV)
763765
self.assertEqual(posix.minor(NODEV), NODEV)
764766
self.assertEqual(posix.makedev(NODEV, NODEV), NODEV)
765767

768+
def test_nodev(self):
769+
if (not hasattr(posix, 'NODEV')
770+
and (not sys.platform.startswith(('linux', 'macos', 'freebsd',
771+
'dragonfly', 'netbsd', 'openbsd',
772+
'sunos'))
773+
or support.linked_to_musl())):
774+
self.skipTest('not defined on this platform')
775+
self.assertHasAttr(posix, 'NODEV')
776+
766777
def _test_all_chown_common(self, chown_func, first_param, stat_func):
767778
"""Common code for chown, fchown and lchown tests."""
768779
def check_stat(uid, gid):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add :const:`os.NODEV`.

Modules/posixmodule.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2759,7 +2759,7 @@ _pystat_fromstructstat(PyObject *module, STRUCT_STAT *st)
27592759
SET_ITEM(ST_BLOCKS_IDX, PyLong_FromLong((long)st->st_blocks));
27602760
#endif
27612761
#ifdef HAVE_STRUCT_STAT_ST_RDEV
2762-
SET_ITEM(ST_RDEV_IDX, PyLong_FromLong((long)st->st_rdev));
2762+
SET_ITEM(ST_RDEV_IDX, _PyLong_FromDev(st->st_rdev));
27632763
#endif
27642764
#ifdef HAVE_STRUCT_STAT_ST_GEN
27652765
SET_ITEM(ST_GEN_IDX, PyLong_FromLong((long)st->st_gen));
@@ -17878,6 +17878,10 @@ all_ins(PyObject *m)
1787817878
#endif
1787917879
#endif /* HAVE_EVENTFD && EFD_CLOEXEC */
1788017880

17881+
#ifdef NODEV
17882+
if (PyModule_Add(m, "NODEV", _PyLong_FromDev(NODEV))) return -1;
17883+
#endif
17884+
1788117885
#if defined(__APPLE__)
1788217886
if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1;
1788317887
if (PyModule_AddIntConstant(m, "_COPYFILE_STAT", COPYFILE_STAT)) return -1;

0 commit comments

Comments
 (0)