Skip to content

Commit a003112

Browse files
gh-138712: Add os.NODEV (GH-138728)
1 parent a5b9d0b commit a003112

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

Doc/library/os.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2630,6 +2630,13 @@ 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+
.. versionadded:: next
2638+
2639+
26332640
.. function:: pathconf(path, name)
26342641

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

Lib/test/test_posix.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,12 +757,30 @@ 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+
# The following tests are needed to test functions accepting or
761+
# returning the special value NODEV (if it is defined). major(), minor()
762+
# and makefile() are the only easily reproducible examples, but that
763+
# behavior is platform specific -- on some platforms their code has
764+
# a special case for NODEV, on others this is just an implementation
765+
# artifact.
766+
if (hasattr(posix, 'NODEV') and
767+
sys.platform.startswith(('linux', 'macos', 'freebsd', 'dragonfly',
768+
'sunos'))):
769+
NODEV = posix.NODEV
762770
self.assertEqual(posix.major(NODEV), NODEV)
763771
self.assertEqual(posix.minor(NODEV), NODEV)
764772
self.assertEqual(posix.makedev(NODEV, NODEV), NODEV)
765773

774+
def test_nodev(self):
775+
# NODEV is not a part of Posix, but is defined on many systems.
776+
if (not hasattr(posix, 'NODEV')
777+
and (not sys.platform.startswith(('linux', 'macos', 'freebsd',
778+
'dragonfly', 'netbsd', 'openbsd',
779+
'sunos'))
780+
or support.linked_to_musl())):
781+
self.skipTest('not defined on this platform')
782+
self.assertHasAttr(posix, 'NODEV')
783+
766784
def _test_all_chown_common(self, chown_func, first_param, stat_func):
767785
"""Common code for chown, fchown and lchown tests."""
768786
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17861,6 +17861,10 @@ all_ins(PyObject *m)
1786117861
#endif
1786217862
#endif /* HAVE_EVENTFD && EFD_CLOEXEC */
1786317863

17864+
#ifdef NODEV
17865+
if (PyModule_Add(m, "NODEV", _PyLong_FromDev(NODEV))) return -1;
17866+
#endif
17867+
1786417868
#if defined(__APPLE__)
1786517869
if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1;
1786617870
if (PyModule_AddIntConstant(m, "_COPYFILE_STAT", COPYFILE_STAT)) return -1;

0 commit comments

Comments
 (0)