]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-71253: Match _io exception in _pyio (gh-133985)
authorCody Maloney <cmaloney@users.noreply.github.com>
Wed, 21 May 2025 14:45:00 +0000 (10:45 -0400)
committerGitHub <noreply@github.com>
Wed, 21 May 2025 14:45:00 +0000 (16:45 +0200)
Test was only testing _io, expanded to cover _pyio.

Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Lib/_pyio.py
Lib/test/test_io.py
Misc/NEWS.d/next/Library/2025-05-13-18-21-59.gh-issue-71253.-3Sf_K.rst [new file with mode: 0644]

index a870de5b5325429e8a534349b233ed65aa5b0825..f79674fb7a9f5e0cfab0a511aab47f4f744f6213 100644 (file)
@@ -1563,7 +1563,8 @@ class FileIO(RawIOBase):
                     if not isinstance(fd, int):
                         raise TypeError('expected integer from opener')
                     if fd < 0:
-                        raise OSError('Negative file descriptor')
+                        # bpo-27066: Raise a ValueError for bad value.
+                        raise ValueError(f'opener returned {fd}')
                 owned_fd = fd
                 if not noinherit_flag:
                     os.set_inheritable(fd, False)
index 90680c6d47ab4183518347b7f9692acacd98c5f8..4625e3a01faa7b894434852d72b46f4624beb001 100644 (file)
@@ -918,7 +918,7 @@ class IOTest(unittest.TestCase):
         def badopener(fname, flags):
             return -1
         with self.assertRaises(ValueError) as cm:
-            open('non-existent', 'r', opener=badopener)
+            self.open('non-existent', 'r', opener=badopener)
         self.assertEqual(str(cm.exception), 'opener returned -1')
 
     def test_bad_opener_other_negative(self):
@@ -926,7 +926,7 @@ class IOTest(unittest.TestCase):
         def badopener(fname, flags):
             return -2
         with self.assertRaises(ValueError) as cm:
-            open('non-existent', 'r', opener=badopener)
+            self.open('non-existent', 'r', opener=badopener)
         self.assertEqual(str(cm.exception), 'opener returned -2')
 
     def test_opener_invalid_fd(self):
diff --git a/Misc/NEWS.d/next/Library/2025-05-13-18-21-59.gh-issue-71253.-3Sf_K.rst b/Misc/NEWS.d/next/Library/2025-05-13-18-21-59.gh-issue-71253.-3Sf_K.rst
new file mode 100644 (file)
index 0000000..714d707
--- /dev/null
@@ -0,0 +1,3 @@
+Raise :exc:`ValueError` in :func:`open` if *opener* returns a negative
+file-descriptor in the Python implementation of :mod:`io` to match the
+C implementation.