diff --git a/Doc/library/os.rst b/Doc/library/os.rst index 0333fe9f9967f8..2e04fbb6f63fd3 100644 --- a/Doc/library/os.rst +++ b/Doc/library/os.rst @@ -2630,6 +2630,13 @@ features: Compose a raw device number from the major and minor device numbers. +.. data:: NODEV + + Non-existent device. + + .. versionadded:: next + + .. function:: pathconf(path, name) Return system configuration information relevant to a named file. *name* diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py index 0bb65fe717d359..ab3d128d08ab47 100644 --- a/Lib/test/test_posix.py +++ b/Lib/test/test_posix.py @@ -757,12 +757,30 @@ def test_makedev(self): self.assertRaises((ValueError, OverflowError), posix.makedev, x, minor) self.assertRaises((ValueError, OverflowError), posix.makedev, major, x) - if sys.platform == 'linux' and not support.linked_to_musl(): - NODEV = -1 + # The following tests are needed to test functions accepting or + # returning the special value NODEV (if it is defined). major(), minor() + # and makefile() are the only easily reproducible examples, but that + # behavior is platform specific -- on some platforms their code has + # a special case for NODEV, on others this is just an implementation + # artifact. + if (hasattr(posix, 'NODEV') and + sys.platform.startswith(('linux', 'macos', 'freebsd', 'dragonfly', + 'sunos'))): + NODEV = posix.NODEV self.assertEqual(posix.major(NODEV), NODEV) self.assertEqual(posix.minor(NODEV), NODEV) self.assertEqual(posix.makedev(NODEV, NODEV), NODEV) + def test_nodev(self): + # NODEV is not a part of Posix, but is defined on many systems. + if (not hasattr(posix, 'NODEV') + and (not sys.platform.startswith(('linux', 'macos', 'freebsd', + 'dragonfly', 'netbsd', 'openbsd', + 'sunos')) + or support.linked_to_musl())): + self.skipTest('not defined on this platform') + self.assertHasAttr(posix, 'NODEV') + def _test_all_chown_common(self, chown_func, first_param, stat_func): """Common code for chown, fchown and lchown tests.""" def check_stat(uid, gid): diff --git a/Misc/NEWS.d/next/Library/2025-09-10-10-11-59.gh-issue-138712.avrPG5.rst b/Misc/NEWS.d/next/Library/2025-09-10-10-11-59.gh-issue-138712.avrPG5.rst new file mode 100644 index 00000000000000..3917726bd048dc --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-09-10-10-11-59.gh-issue-138712.avrPG5.rst @@ -0,0 +1 @@ +Add :const:`os.NODEV`. diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index bba73c659dd168..62b0c35602323f 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -17861,6 +17861,10 @@ all_ins(PyObject *m) #endif #endif /* HAVE_EVENTFD && EFD_CLOEXEC */ +#ifdef NODEV + if (PyModule_Add(m, "NODEV", _PyLong_FromDev(NODEV))) return -1; +#endif + #if defined(__APPLE__) if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1; if (PyModule_AddIntConstant(m, "_COPYFILE_STAT", COPYFILE_STAT)) return -1;