]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-71253: Match _io exception in _pyio (gh-133985) (gh-134431)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 21 May 2025 15:14:00 +0000 (17:14 +0200)
committerGitHub <noreply@github.com>
Wed, 21 May 2025 15:14:00 +0000 (17:14 +0200)
Test was only testing _io, expanded to cover _pyio.

(cherry picked from commit 06eaf4055c1d7359e129efb65b94f34d2ec51a57)

Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>
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 a3fede699218a150fd6709ccb38c6e4e3c91cd9e..5e7806b2bd40f45e2af8bba53c4994bf97f36a02 100644 (file)
@@ -1559,7 +1559,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 4f06fd9db0322fd6560bfc4b2d0b8456629afe11..ce7166842237ea2333af5ad58f8eff81069ffb55 100644 (file)
@@ -894,7 +894,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):
@@ -902,7 +902,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.