]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-138712: Add os.NODEV (GH-138728)
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 15 Sep 2025 14:36:32 +0000 (17:36 +0300)
committerGitHub <noreply@github.com>
Mon, 15 Sep 2025 14:36:32 +0000 (14:36 +0000)
Doc/library/os.rst
Lib/test/test_posix.py
Misc/NEWS.d/next/Library/2025-09-10-10-11-59.gh-issue-138712.avrPG5.rst [new file with mode: 0644]
Modules/posixmodule.c

index 0333fe9f9967f8b4f8cc442822facdb571e064b8..2e04fbb6f63fd35a583d3d97ea011d03ff0876b3 100644 (file)
@@ -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*
index 0bb65fe717d359f5f822afa2edc34c5bcebb5d22..ab3d128d08ab472fa86eb4bd84bf51fb18a4336e 100644 (file)
@@ -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 (file)
index 0000000..3917726
--- /dev/null
@@ -0,0 +1 @@
+Add :const:`os.NODEV`.
index bba73c659dd168cb7db52aee4dfb29a3f69dd91d..62b0c35602323f0d0aad1daecb4940629666c3ec 100644 (file)
@@ -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;