Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ def test_WindowsError(self):
w = OSError(9, 'foo', 'bar')
self.assertEqual(w.errno, 9)
self.assertEqual(w.winerror, None)
self.assertEqual(str(w), "[Errno 9] foo: 'bar'")
self.assertEqual(str(w), "[Errno 9 (EBADF)] foo: 'bar'")
# ERROR_PATH_NOT_FOUND (win error 3) becomes ENOENT (2)
w = OSError(0, 'foo', 'bar', 3)
self.assertEqual(w.errno, 2)
Expand Down Expand Up @@ -1837,6 +1837,16 @@ def g():
next(i)
next(i)

def test_OSError_errno_error_message(self):
# From PR 14988.
with self.assertRaises(OSError) as cm:
open('__non-existent__')
self.assertEqual(
str(cm.exception),
f"[Errno {errno.ENOENT} (ENOENT)] "
"No such file or directory: '__non-existent__'",
)

@unittest.skipUnless(__debug__, "Won't work if __debug__ is False")
def test_assert_shadowing(self):
# Shadowing AssertionError would cause the assert statement to
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,8 @@ def handler(signum, frame):
if ('Exception ignored while trying to write to the signal wakeup fd'
not in err):
raise AssertionError(err)
if ('OSError: [Errno %d]' % errno.EBADF) not in err:
if ('OSError: [Errno %d (%s)]' %
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This exists in the CPython codebase; that makes me worry this same sort of string matching may exist in many other codebases and will be broken by this change...

Is there some way we could measure/check for that? Keeping the symbolic name outside the [] would limit breakage some.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. It's a valid concern indeed. I ran rg -n '\[Errno [^]]*\]|OSError: \[Errno' Lib Tools Objects Modules Python and found cases treating the int after Error as a format.
The two other alternatives are as follows, let me know which one looks better:

  1. [Errno 2] (ENOENT) No such file or directory
  2. [Errno 2] No such file or directory (ENOENT)

I like 1 bettter, to avoid confusion.

(errno.EBADF, errno.errorcode[errno.EBADF])) not in err:
raise AssertionError(err)
else:
raise AssertionError("ZeroDivisionError not raised")
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_tabnanny.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def test_when_nannynag_error(self):
def test_when_no_file(self):
"""A python file which does not exist actually in system."""
path = 'no_file.py'
err = (f"{path!r}: I/O Error: [Errno {errno.ENOENT}] "
err = (f"{path!r}: I/O Error: [Errno {errno.ENOENT} (ENOENT)] "
f"{os.strerror(errno.ENOENT)}: {path!r}\n")
with self.assertRaises(SystemExit):
self.verify_tabnanny_check(path, err=err)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Include the symbolic error name in :exc:`OSError` exception string representations.
Loading
Loading