Skip to content

Commit 6e69441

Browse files
philg314encukou
authored andcommitted
Use built-in AssertionError
1 parent 0f6cd30 commit 6e69441

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

Modules/_testcapi/unicode.c

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
#include "parts.h"
22

3-
// Forward declarations
43
static struct PyModuleDef *_testcapimodule = NULL; // set at initialization
5-
static PyObject *TestError; /* set to exception object in init */
6-
7-
/* Raise TestError with test_name + ": " + msg, and return NULL. */
8-
static PyObject *
9-
raiseTestError(const char* test_name, const char* msg)
10-
{
11-
PyErr_Format(TestError, "%s: %s", test_name, msg);
12-
return NULL;
13-
}
144

155
static PyObject *
166
codec_incrementalencoder(PyObject *self, PyObject *args)
@@ -41,7 +31,7 @@ test_unicode_compare_with_ascii(PyObject *self, PyObject *Py_UNUSED(ignored)) {
4131
result = PyUnicode_CompareWithASCIIString(py_s, "str");
4232
Py_DECREF(py_s);
4333
if (!result) {
44-
PyErr_SetString(TestError, "Python string ending in NULL "
34+
PyErr_SetString(PyExc_AssertionError, "Python string ending in NULL "
4535
"should not compare equal to c string.");
4636
return NULL;
4737
}
@@ -74,18 +64,22 @@ test_widechar(PyObject *self, PyObject *Py_UNUSED(ignored))
7464
if (PyUnicode_GET_LENGTH(wide) != PyUnicode_GET_LENGTH(utf8)) {
7565
Py_DECREF(wide);
7666
Py_DECREF(utf8);
77-
return raiseTestError("test_widechar",
78-
"wide string and utf8 string "
79-
"have different length");
67+
PyErr_SetString(PyExc_AssertionError,
68+
"test_widechar: "
69+
"wide string and utf8 string "
70+
"have different length");
71+
return NULL;
8072
}
8173
if (PyUnicode_Compare(wide, utf8)) {
8274
Py_DECREF(wide);
8375
Py_DECREF(utf8);
8476
if (PyErr_Occurred())
8577
return NULL;
86-
return raiseTestError("test_widechar",
87-
"wide string and utf8 string "
88-
"are different");
78+
PyErr_SetString(PyExc_AssertionError,
79+
"test_widechar: "
80+
"wide string and utf8 string "
81+
"are different");
82+
return NULL;
8983
}
9084

9185
Py_DECREF(wide);
@@ -95,9 +89,12 @@ test_widechar(PyObject *self, PyObject *Py_UNUSED(ignored))
9589
wide = PyUnicode_FromWideChar(invalid, 1);
9690
if (wide == NULL)
9791
PyErr_Clear();
98-
else
99-
return raiseTestError("test_widechar",
100-
"PyUnicode_FromWideChar(L\"\\U00110000\", 1) didn't fail");
92+
else {
93+
PyErr_SetString(PyExc_AssertionError,
94+
"test_widechar: "
95+
"PyUnicode_FromWideChar(L\"\\U00110000\", 1) didn't fail");
96+
return NULL;
97+
}
10198
#endif
10299
Py_RETURN_NONE;
103100
}
@@ -287,7 +284,7 @@ test_string_from_format(PyObject *self, PyObject *Py_UNUSED(ignored))
287284
if (result == NULL) \
288285
return NULL; \
289286
if (!_PyUnicode_EqualToASCIIString(result, EXPECTED)) { \
290-
PyErr_Format(TestError, \
287+
PyErr_Format(PyExc_AssertionError, \
291288
"test_string_from_format: failed at \"%s\" " \
292289
"expected \"%s\" got \"%s\"", \
293290
FORMAT, EXPECTED, PyUnicode_AsUTF8(result)); \
@@ -684,10 +681,6 @@ int
684681
_PyTestCapi_Init_Unicode(PyObject *m) {
685682
_testcapimodule = PyModule_GetDef(m);
686683

687-
TestError = PyErr_NewException("_testcapi.unicode_error", NULL, NULL);
688-
Py_INCREF(TestError);
689-
PyModule_AddObject(m, "unicode_error", TestError);
690-
691684
if (PyModule_AddFunctions(m, TestMethods) < 0) {
692685
return -1;
693686
}

0 commit comments

Comments
 (0)