From: Serhiy Storchaka Date: Mon, 15 Sep 2025 14:36:32 +0000 (+0300) Subject: gh-138712: Add os.NODEV (GH-138728) X-Git-Tag: v3.15.0a1~361 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a003112821a445128f9b94f9a46528e5449dfc86;p=thirdparty%2FPython%2Fcpython.git gh-138712: Add os.NODEV (GH-138728) --- diff --git a/Doc/library/os.rst b/Doc/library/os.rst index 0333fe9f9967..2e04fbb6f63f 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 0bb65fe717d3..ab3d128d08ab 100644 --- a/Lib/test/test_posix.py +++ b/Lib/test/test_posix.py @@ -757,12 +757,30 @@ class PosixTester(unittest.TestCase): 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 000000000000..3917726bd048 --- /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 bba73c659dd1..62b0c3560232 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;