]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Add Modules/_testcapi/util.h header (GH-108774)
authorSerhiy Storchaka <storchaka@gmail.com>
Fri, 1 Sep 2023 18:42:42 +0000 (21:42 +0300)
committerGitHub <noreply@github.com>
Fri, 1 Sep 2023 18:42:42 +0000 (21:42 +0300)
It contains common macros used in C API tests.

Makefile.pre.in
Modules/_testcapi/abstract.c
Modules/_testcapi/dict.c
Modules/_testcapi/exceptions.c
Modules/_testcapi/unicode.c
Modules/_testcapi/util.h [new file with mode: 0644]
Modules/_testcapimodule.c

index 0870cb3d899d4510f0deba7f8b0b21eb10ed9b20..5826b9a82736b90fdd1bda49cc58b1f8dbb076be 100644 (file)
@@ -2799,7 +2799,7 @@ MODULE__SHA2_DEPS=$(srcdir)/Modules/hashlib.h $(LIBHACL_SHA2_HEADERS) $(LIBHACL_
 MODULE__SHA3_DEPS=$(srcdir)/Modules/hashlib.h $(LIBHACL_HEADERS) Modules/_hacl/Hacl_Hash_SHA3.h Modules/_hacl/Hacl_Hash_SHA3.c
 MODULE__SOCKET_DEPS=$(srcdir)/Modules/socketmodule.h $(srcdir)/Modules/addrinfo.h $(srcdir)/Modules/getaddrinfo.c $(srcdir)/Modules/getnameinfo.c
 MODULE__SSL_DEPS=$(srcdir)/Modules/_ssl.h $(srcdir)/Modules/_ssl/cert.c $(srcdir)/Modules/_ssl/debughelpers.c $(srcdir)/Modules/_ssl/misc.c $(srcdir)/Modules/_ssl_data.h $(srcdir)/Modules/_ssl_data_111.h $(srcdir)/Modules/_ssl_data_300.h $(srcdir)/Modules/socketmodule.h
-MODULE__TESTCAPI_DEPS=$(srcdir)/Modules/_testcapi/testcapi_long.h $(srcdir)/Modules/_testcapi/parts.h
+MODULE__TESTCAPI_DEPS=$(srcdir)/Modules/_testcapi/testcapi_long.h $(srcdir)/Modules/_testcapi/parts.h $(srcdir)/Modules/_testcapi/util.h
 MODULE__SQLITE3_DEPS=$(srcdir)/Modules/_sqlite/connection.h $(srcdir)/Modules/_sqlite/cursor.h $(srcdir)/Modules/_sqlite/microprotocols.h $(srcdir)/Modules/_sqlite/module.h $(srcdir)/Modules/_sqlite/prepare_protocol.h $(srcdir)/Modules/_sqlite/row.h $(srcdir)/Modules/_sqlite/util.h
 
 CODECS_COMMON_HEADERS=$(srcdir)/Modules/cjkcodecs/multibytecodec.h $(srcdir)/Modules/cjkcodecs/cjkcodecs.h
index 10d7ff8d4a7bc834d04b0a1f56fde71eb404e5e2..91a1ee3aaafc02667497730e5709a7b907c0b2d2 100644 (file)
@@ -1,24 +1,7 @@
 #include <stddef.h>               // ptrdiff_t
 
 #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 *
index 6c3f9cd22faa5aee00f88b7bb010f38dac26b37b..6720f0437401efc4f32f35a43cbec250a2a7013f 100644 (file)
@@ -1,24 +1,7 @@
 #include <stddef.h>               // ptrdiff_t
 
 #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 *
index d7edf8c7ce6744ae33b844e54fb2adfd5718e215..025b42db247e81526c6a303921480901b1e65350 100644 (file)
@@ -1,7 +1,7 @@
 #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
index b4c6c4b5e3ce1af00a6414cc34c4446c0ca52209..f2cf9a744007d9c11fd0060bf625348c48c6d205 100644 (file)
@@ -1,6 +1,7 @@
 #include <stddef.h>               // ptrdiff_t
 
 #include "parts.h"
+#include "util.h"
 
 static struct PyModuleDef *_testcapimodule = NULL;  // set at initialization
 
@@ -101,7 +102,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)
@@ -347,13 +347,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() */
@@ -482,16 +477,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() */
@@ -1293,17 +1284,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() */
@@ -1323,8 +1310,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);
 }
 
@@ -1337,17 +1327,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() */
@@ -1366,10 +1352,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() */
@@ -1407,6 +1395,7 @@ unicode_compare(PyObject *self, PyObject *args)
     if (result == -1 && PyErr_Occurred()) {
         return NULL;
     }
+    assert(!PyErr_Occurred());
     return PyLong_FromLong(result);
 }
 
@@ -1467,32 +1456,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 (file)
index 0000000..05ccb12
--- /dev/null
@@ -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)
index f8b36ff9ce835a22fec0dab308645aa777ad8f70..4fc354ae79bfed4bfcc94e327a84b9f2681eb8df 100644 (file)
 // 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;
@@ -1333,19 +1325,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