https://github.com/python/cpython/commit/74dde92903769b77843152a3603f0e50845ccc81
commit: 74dde92903769b77843152a3603f0e50845ccc81
branch: 3.14
author: Miss Islington (bot) <31488909+miss-isling...@users.noreply.github.com>
committer: ambv <luk...@langa.pl>
date: 2025-05-21T17:13:43+02:00
summary:

[3.14] gh-71253: Match _io exception in _pyio (gh-133985) (gh-134430)

Test was only testing _io, expanded to cover _pyio.

(cherry picked from commit 06eaf4055c1d7359e129efb65b94f34d2ec51a57)

Co-authored-by: Cody Maloney <cmalo...@users.noreply.github.com>
Co-authored-by: Peter Bierma <zintensity...@gmail.com>

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 a870de5b532542..f79674fb7a9f5e 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -1563,7 +1563,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 90680c6d47ab41..4625e3a01faa7b 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -918,7 +918,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):
@@ -926,7 +926,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 -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: arch...@mail-archive.com

Reply via email to