-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-107915: Handle errors in C API functions PyErr_Set*() and PyErr_Format() #107918
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
serhiy-storchaka
merged 1 commit into
python:main
from
serhiy-storchaka:capi-handle-errors-PyErr_SetX
Aug 19, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
import errno | ||
import os | ||
import re | ||
import sys | ||
import unittest | ||
|
||
from test import support | ||
from test.support import import_helper | ||
from test.support.os_helper import TESTFN, TESTFN_UNDECODABLE | ||
from test.support.script_helper import assert_python_failure | ||
from test.support.testcase import ExceptionIsLikeMixin | ||
|
||
|
@@ -12,6 +15,8 @@ | |
# Skip this test if the _testcapi module isn't available. | ||
_testcapi = import_helper.import_module('_testcapi') | ||
|
||
NULL = None | ||
|
||
class Test_Exceptions(unittest.TestCase): | ||
|
||
def test_exception(self): | ||
|
@@ -189,6 +194,82 @@ def __repr__(self): | |
self.assertEqual(exc.__notes__[0], | ||
'Normalization failed: type=Broken args=<unknown>') | ||
|
||
def test_set_string(self): | ||
"""Test PyErr_SetString()""" | ||
setstring = _testcapi.err_setstring | ||
with self.assertRaises(ZeroDivisionError) as e: | ||
setstring(ZeroDivisionError, b'error') | ||
self.assertEqual(e.exception.args, ('error',)) | ||
with self.assertRaises(ZeroDivisionError) as e: | ||
setstring(ZeroDivisionError, 'помилка'.encode()) | ||
self.assertEqual(e.exception.args, ('помилка',)) | ||
|
||
with self.assertRaises(UnicodeDecodeError): | ||
setstring(ZeroDivisionError, b'\xff') | ||
self.assertRaises(SystemError, setstring, list, b'error') | ||
# CRASHES setstring(ZeroDivisionError, NULL) | ||
# CRASHES setstring(NULL, b'error') | ||
|
||
def test_format(self): | ||
"""Test PyErr_Format()""" | ||
import_helper.import_module('ctypes') | ||
from ctypes import pythonapi, py_object, c_char_p, c_int | ||
name = "PyErr_Format" | ||
PyErr_Format = getattr(pythonapi, name) | ||
PyErr_Format.argtypes = (py_object, c_char_p,) | ||
PyErr_Format.restype = py_object | ||
with self.assertRaises(ZeroDivisionError) as e: | ||
PyErr_Format(ZeroDivisionError, b'%s %d', b'error', c_int(42)) | ||
self.assertEqual(e.exception.args, ('error 42',)) | ||
with self.assertRaises(ZeroDivisionError) as e: | ||
PyErr_Format(ZeroDivisionError, b'%s', 'помилка'.encode()) | ||
self.assertEqual(e.exception.args, ('помилка',)) | ||
|
||
with self.assertRaisesRegex(OverflowError, 'not in range'): | ||
PyErr_Format(ZeroDivisionError, b'%c', c_int(-1)) | ||
with self.assertRaisesRegex(ValueError, 'format string'): | ||
PyErr_Format(ZeroDivisionError, b'\xff') | ||
Comment on lines
+228
to
+231
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fails without the fix. |
||
self.assertRaises(SystemError, PyErr_Format, list, b'error') | ||
# CRASHES PyErr_Format(ZeroDivisionError, NULL) | ||
# CRASHES PyErr_Format(py_object(), b'error') | ||
|
||
def test_setfromerrnowithfilename(self): | ||
"""Test PyErr_SetFromErrnoWithFilename()""" | ||
setfromerrnowithfilename = _testcapi.err_setfromerrnowithfilename | ||
ENOENT = errno.ENOENT | ||
with self.assertRaises(FileNotFoundError) as e: | ||
setfromerrnowithfilename(ENOENT, OSError, b'file') | ||
self.assertEqual(e.exception.args, | ||
(ENOENT, 'No such file or directory')) | ||
self.assertEqual(e.exception.errno, ENOENT) | ||
self.assertEqual(e.exception.filename, 'file') | ||
|
||
with self.assertRaises(FileNotFoundError) as e: | ||
setfromerrnowithfilename(ENOENT, OSError, os.fsencode(TESTFN)) | ||
self.assertEqual(e.exception.filename, TESTFN) | ||
|
||
if TESTFN_UNDECODABLE: | ||
with self.assertRaises(FileNotFoundError) as e: | ||
setfromerrnowithfilename(ENOENT, OSError, TESTFN_UNDECODABLE) | ||
self.assertEqual(e.exception.filename, | ||
os.fsdecode(TESTFN_UNDECODABLE)) | ||
|
||
with self.assertRaises(FileNotFoundError) as e: | ||
setfromerrnowithfilename(ENOENT, OSError, NULL) | ||
self.assertIsNone(e.exception.filename) | ||
|
||
with self.assertRaises(OSError) as e: | ||
setfromerrnowithfilename(0, OSError, b'file') | ||
self.assertEqual(e.exception.args, (0, 'Error')) | ||
self.assertEqual(e.exception.errno, 0) | ||
self.assertEqual(e.exception.filename, 'file') | ||
|
||
with self.assertRaises(ZeroDivisionError) as e: | ||
setfromerrnowithfilename(ENOENT, ZeroDivisionError, b'file') | ||
self.assertEqual(e.exception.args, | ||
(ENOENT, 'No such file or directory', 'file')) | ||
# CRASHES setfromerrnowithfilename(ENOENT, NULL, b'error') | ||
|
||
|
||
class Test_PyUnstable_Exc_PrepReraiseStar(ExceptionIsLikeMixin, unittest.TestCase): | ||
|
||
|
4 changes: 4 additions & 0 deletions
4
Misc/NEWS.d/next/C API/2023-08-13-12-33-00.gh-issue-107915.jQ0wOi.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Such C API functions as ``PyErr_SetString()``, ``PyErr_Format()``, | ||
``PyErr_SetFromErrnoWithFilename()`` and many others no longer crash or | ||
ignore errors if it failed to format the error message or decode the | ||
filename. Instead, they keep a corresponding error. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fails without the fix.