Skip to content

Commit b02a715

Browse files
committed
add tests for _PyErr_SetLocaleString
1 parent 6f9ee0b commit b02a715

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

Lib/test/test_capi/test_exceptions.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,23 @@ def test_set_string(self):
256256
# CRASHES setstring(ZeroDivisionError, NULL)
257257
# CRASHES setstring(NULL, b'error')
258258

259+
def test_set_locale_string(self):
260+
# test _PyErr_SetLocaleString()
261+
setlocalestring = _testcapi.err_setlocalestring
262+
with self.assertRaises(ZeroDivisionError) as e:
263+
setlocalestring(ZeroDivisionError, b'error')
264+
self.assertEqual(e.exception.args, ('error',))
265+
with self.assertRaises(ZeroDivisionError) as e:
266+
setlocalestring(ZeroDivisionError, 'помилка'.encode())
267+
self.assertEqual(e.exception.args, ('помилка',))
268+
269+
with self.assertRaises(ZeroDivisionError):
270+
setlocalestring(ZeroDivisionError, b'\xff')
271+
self.assertRaises(SystemError, setlocalestring, list, b'error')
272+
273+
# CRASHES setlocalestring(ZeroDivisionError, NULL)
274+
# CRASHES setlocalestring(NULL, b'error')
275+
259276
def test_format(self):
260277
"""Test PyErr_Format()"""
261278
import_helper.import_module('ctypes')

Modules/_testcapi/clinic/exceptions.c.h

Lines changed: 32 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/_testcapi/exceptions.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
#include "parts.h"
55
#include "util.h"
6+
7+
#include "pycore_pyerrors.h"
8+
69
#include "clinic/exceptions.c.h"
710

811

@@ -154,6 +157,25 @@ _testcapi_err_setstring_impl(PyObject *module, PyObject *exc,
154157
return NULL;
155158
}
156159

160+
/*[clinic input]
161+
_testcapi.err_setlocalestring
162+
exc: object
163+
value: str(zeroes=True, accept={robuffer, str, NoneType})
164+
/
165+
[clinic start generated code]*/
166+
167+
static PyObject *
168+
_testcapi_err_setlocalestring_impl(PyObject *module, PyObject *exc,
169+
const char *value,
170+
Py_ssize_t value_length)
171+
/*[clinic end generated code: output=5c02a10d4eb573d7 input=628c26bdc2497a92]*/
172+
{
173+
NULLABLE(exc)
174+
_PyErr_SetLocaleString(exc, value);
175+
return NULL;
176+
}
177+
178+
157179
/*[clinic input]
158180
_testcapi.err_setfromerrnowithfilename
159181
error: int
@@ -396,6 +418,7 @@ static PyMethodDef test_methods[] = {
396418
_TESTCAPI_EXC_SET_OBJECT_METHODDEF
397419
_TESTCAPI_EXC_SET_OBJECT_FETCH_METHODDEF
398420
_TESTCAPI_ERR_SETSTRING_METHODDEF
421+
_TESTCAPI_ERR_SETLOCALESTRING_METHODDEF
399422
_TESTCAPI_ERR_SETFROMERRNOWITHFILENAME_METHODDEF
400423
_TESTCAPI_RAISE_EXCEPTION_METHODDEF
401424
_TESTCAPI_RAISE_MEMORYERROR_METHODDEF

0 commit comments

Comments
 (0)