https://github.com/python/cpython/commit/046125e5b6a0e9bfa260c9e2c20f4d84fb832fbb
commit: 046125e5b6a0e9bfa260c9e2c20f4d84fb832fbb
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: ambv <[email protected]>
date: 2025-05-21T17:14:00+02:00
summary:

[3.13] gh-71253: Match _io exception in _pyio (gh-133985) (gh-134431)

Test was only testing _io, expanded to cover _pyio.

(cherry picked from commit 06eaf4055c1d7359e129efb65b94f34d2ec51a57)

Co-authored-by: Cody Maloney <[email protected]>
Co-authored-by: Peter Bierma <[email protected]>

files:
A Misc/NEWS.d/next/Library/2025-05-13-18-21-59.gh-issue-71253.-3Sf_K.rst
M Lib/_pyio.py
M Lib/test/test_io.py

diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index a3fede699218a1..5e7806b2bd40f4 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -1559,7 +1559,8 @@ def __init__(self, file, mode='r', closefd=True, 
opener=None):
                     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)
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index 4f06fd9db0322f..ce7166842237ea 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -894,7 +894,7 @@ def test_bad_opener_negative_1(self):
         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 @@ def test_bad_opener_other_negative(self):
         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
index 00000000000000..714d707f488709
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-05-13-18-21-59.gh-issue-71253.-3Sf_K.rst
@@ -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.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to