summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2023-09-02 21:43:14 (GMT)
committerGitHub <noreply@github.com>2023-09-02 21:43:14 (GMT)
commitba7e06bb631a85b0e74b6de4c5005d4b4447519f (patch)
tree634020786bcdfc205798936608f6f4f1f3a7d5e3 /Modules
parentf58617a3bc4ff64e4663ae1a719f03bf3f2475f1 (diff)
downloadcpython-ba7e06bb631a85b0e74b6de4c5005d4b4447519f.zip
cpython-ba7e06bb631a85b0e74b6de4c5005d4b4447519f.tar.gz
cpython-ba7e06bb631a85b0e74b6de4c5005d4b4447519f.tar.bz2
[3.12] Add Modules/_testcapi/util.h header (GH-108774) (#108780)
Add Modules/_testcapi/util.h header (GH-108774) It contains common macros used in C API tests. (cherry picked from commit 0e01fac315dfa705ac8a6954485546f28cf4c87d) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_testcapi/abstract.c19
-rw-r--r--Modules/_testcapi/dict.c19
-rw-r--r--Modules/_testcapi/exceptions.c2
-rw-r--r--Modules/_testcapi/unicode.c58
-rw-r--r--Modules/_testcapi/util.h25
-rw-r--r--Modules/_testcapimodule.c18
6 files changed, 48 insertions, 93 deletions
diff --git a/Modules/_testcapi/abstract.c b/Modules/_testcapi/abstract.c
index 9715efb..38236aa 100644
--- a/Modules/_testcapi/abstract.c
+++ b/Modules/_testcapi/abstract.c
@@ -2,24 +2,7 @@
#define PY_SSIZE_T_CLEAN
#include "parts.h"
-
-#define NULLABLE(x) do { if (x == Py_None) x = NULL; } while (0);
-
-#define RETURN_INT(value) do { \
- int _ret = (value); \
- if (_ret == -1) { \
- return NULL; \
- } \
- return PyLong_FromLong(_ret); \
- } while (0)
-
-#define RETURN_SIZE(value) do { \
- Py_ssize_t _ret = (value); \
- if (_ret == -1) { \
- return NULL; \
- } \
- return PyLong_FromSsize_t(_ret); \
- } while (0)
+#include "util.h"
static PyObject *
diff --git a/Modules/_testcapi/dict.c b/Modules/_testcapi/dict.c
index c0f26e7..bcc86c9 100644
--- a/Modules/_testcapi/dict.c
+++ b/Modules/_testcapi/dict.c
@@ -2,24 +2,7 @@
#define PY_SSIZE_T_CLEAN
#include "parts.h"
-
-#define NULLABLE(x) do { if (x == Py_None) x = NULL; } while (0);
-
-#define RETURN_INT(value) do { \
- int _ret = (value); \
- if (_ret == -1) { \
- return NULL; \
- } \
- return PyLong_FromLong(_ret); \
- } while (0)
-
-#define RETURN_SIZE(value) do { \
- Py_ssize_t _ret = (value); \
- if (_ret == -1) { \
- return NULL; \
- } \
- return PyLong_FromSsize_t(_ret); \
- } while (0)
+#include "util.h"
static PyObject *
diff --git a/Modules/_testcapi/exceptions.c b/Modules/_testcapi/exceptions.c
index 4ae9ba4..9756c37 100644
--- a/Modules/_testcapi/exceptions.c
+++ b/Modules/_testcapi/exceptions.c
@@ -1,8 +1,8 @@
#define PY_SSIZE_T_CLEAN
#include "parts.h"
+#include "util.h"
#include "clinic/exceptions.c.h"
-#define NULLABLE(x) do { if (x == Py_None) x = NULL; } while (0);
/*[clinic input]
module _testcapi
diff --git a/Modules/_testcapi/unicode.c b/Modules/_testcapi/unicode.c
index b4d7bf82..de899b6 100644
--- a/Modules/_testcapi/unicode.c
+++ b/Modules/_testcapi/unicode.c
@@ -2,6 +2,7 @@
#define PY_SSIZE_T_CLEAN
#include "parts.h"
+#include "util.h"
static struct PyModuleDef *_testcapimodule = NULL; // set at initialization
@@ -102,7 +103,6 @@ test_widechar(PyObject *self, PyObject *Py_UNUSED(ignored))
Py_RETURN_NONE;
}
-#define NULLABLE(x) do { if (x == Py_None) x = NULL; } while (0);
static PyObject *
unicode_copy(PyObject *unicode)
@@ -348,13 +348,8 @@ unicode_substring(PyObject *self, PyObject *args)
static PyObject *
unicode_getlength(PyObject *self, PyObject *arg)
{
- Py_ssize_t result;
-
NULLABLE(arg);
- result = PyUnicode_GetLength(arg);
- if (result == -1)
- return NULL;
- return PyLong_FromSsize_t(result);
+ RETURN_SIZE(PyUnicode_GetLength(arg));
}
/* Test PyUnicode_ReadChar() */
@@ -483,16 +478,12 @@ static PyObject *
unicode_aswidechar_null(PyObject *self, PyObject *args)
{
PyObject *unicode;
- Py_ssize_t buflen, size;
+ Py_ssize_t buflen;
if (!PyArg_ParseTuple(args, "On", &unicode, &buflen))
return NULL;
NULLABLE(unicode);
- size = PyUnicode_AsWideChar(unicode, NULL, buflen);
- if (size == -1) {
- return NULL;
- }
- return PyLong_FromSsize_t(size);
+ RETURN_SIZE(PyUnicode_AsWideChar(unicode, NULL, buflen));
}
/* Test PyUnicode_AsWideCharString() */
@@ -1302,17 +1293,13 @@ unicode_count(PyObject *self, PyObject *args)
PyObject *substr;
Py_ssize_t start;
Py_ssize_t end;
- Py_ssize_t result;
if (!PyArg_ParseTuple(args, "OOnn", &str, &substr, &start, &end))
return NULL;
NULLABLE(str);
NULLABLE(substr);
- result = PyUnicode_Count(str, substr, start, end);
- if (result == -1)
- return NULL;
- return PyLong_FromSsize_t(result);
+ RETURN_SIZE(PyUnicode_Count(str, substr, start, end));
}
/* Test PyUnicode_Find() */
@@ -1332,8 +1319,11 @@ unicode_find(PyObject *self, PyObject *args)
NULLABLE(str);
NULLABLE(substr);
result = PyUnicode_Find(str, substr, start, end, direction);
- if (result == -2)
+ if (result == -2) {
+ assert(PyErr_Occurred());
return NULL;
+ }
+ assert(!PyErr_Occurred());
return PyLong_FromSsize_t(result);
}
@@ -1346,17 +1336,13 @@ unicode_tailmatch(PyObject *self, PyObject *args)
Py_ssize_t start;
Py_ssize_t end;
int direction;
- Py_ssize_t result;
if (!PyArg_ParseTuple(args, "OOnni", &str, &substr, &start, &end, &direction))
return NULL;
NULLABLE(str);
NULLABLE(substr);
- result = PyUnicode_Tailmatch(str, substr, start, end, direction);
- if (result == -1)
- return NULL;
- return PyLong_FromSsize_t(result);
+ RETURN_SIZE(PyUnicode_Tailmatch(str, substr, start, end, direction));
}
/* Test PyUnicode_FindChar() */
@@ -1375,10 +1361,12 @@ unicode_findchar(PyObject *self, PyObject *args)
}
NULLABLE(str);
result = PyUnicode_FindChar(str, (Py_UCS4)ch, start, end, direction);
- if (result == -2)
+ if (result == -2) {
+ assert(PyErr_Occurred());
return NULL;
- else
- return PyLong_FromSsize_t(result);
+ }
+ assert(!PyErr_Occurred());
+ return PyLong_FromSsize_t(result);
}
/* Test PyUnicode_Replace() */
@@ -1416,6 +1404,7 @@ unicode_compare(PyObject *self, PyObject *args)
if (result == -1 && PyErr_Occurred()) {
return NULL;
}
+ assert(!PyErr_Occurred());
return PyLong_FromLong(result);
}
@@ -1476,32 +1465,21 @@ unicode_contains(PyObject *self, PyObject *args)
{
PyObject *container;
PyObject *element;
- int result;
if (!PyArg_ParseTuple(args, "OO", &container, &element))
return NULL;
NULLABLE(container);
NULLABLE(element);
- result = PyUnicode_Contains(container, element);
- if (result == -1 && PyErr_Occurred()) {
- return NULL;
- }
- return PyLong_FromLong(result);
+ RETURN_INT(PyUnicode_Contains(container, element));
}
/* Test PyUnicode_IsIdentifier() */
static PyObject *
unicode_isidentifier(PyObject *self, PyObject *arg)
{
- int result;
-
NULLABLE(arg);
- result = PyUnicode_IsIdentifier(arg);
- if (result == -1 && PyErr_Occurred()) {
- return NULL;
- }
- return PyLong_FromLong(result);
+ RETURN_INT(PyUnicode_IsIdentifier(arg));
}
/* Test PyUnicode_CopyCharacters() */
diff --git a/Modules/_testcapi/util.h b/Modules/_testcapi/util.h
new file mode 100644
index 0000000..05ccb12
--- /dev/null
+++ b/Modules/_testcapi/util.h
@@ -0,0 +1,25 @@
+#define NULLABLE(x) do { \
+ if (x == Py_None) { \
+ x = NULL; \
+ } \
+ } while (0);
+
+#define RETURN_INT(value) do { \
+ int _ret = (value); \
+ if (_ret == -1) { \
+ assert(PyErr_Occurred()); \
+ return NULL; \
+ } \
+ assert(!PyErr_Occurred()); \
+ return PyLong_FromLong(_ret); \
+ } while (0)
+
+#define RETURN_SIZE(value) do { \
+ Py_ssize_t _ret = (value); \
+ if (_ret == -1) { \
+ assert(PyErr_Occurred()); \
+ return NULL; \
+ } \
+ assert(!PyErr_Occurred()); \
+ return PyLong_FromSsize_t(_ret); \
+ } while (0)
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index ee6f02d..dec64af 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -44,16 +44,8 @@
// Several parts of this module are broken out into files in _testcapi/.
// Include definitions from there.
#include "_testcapi/parts.h"
+#include "_testcapi/util.h"
-#define NULLABLE(x) do { if (x == Py_None) x = NULL; } while (0);
-
-#define RETURN_INT(value) do { \
- int _ret = (value); \
- if (_ret == -1) { \
- return NULL; \
- } \
- return PyLong_FromLong(_ret); \
- } while (0)
// Forward declarations
static struct PyModuleDef _testcapimodule;
@@ -1348,19 +1340,13 @@ static PyObject *
test_PyBuffer_SizeFromFormat(PyObject *self, PyObject *args)
{
const char *format;
- Py_ssize_t result;
if (!PyArg_ParseTuple(args, "s:test_PyBuffer_SizeFromFormat",
&format)) {
return NULL;
}
- result = PyBuffer_SizeFromFormat(format);
- if (result == -1) {
- return NULL;
- }
-
- return PyLong_FromSsize_t(result);
+ RETURN_SIZE(PyBuffer_SizeFromFormat(format));
}
/* Test that the fatal error from not having a current thread doesn't