Skip to content

Commit 0e01fac

Browse files
Add Modules/_testcapi/util.h header (GH-108774)
It contains common macros used in C API tests.
1 parent 578ebc5 commit 0e01fac

File tree

7 files changed

+49
-94
lines changed

7 files changed

+49
-94
lines changed

Makefile.pre.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -2799,7 +2799,7 @@ MODULE__SHA2_DEPS=$(srcdir)/Modules/hashlib.h $(LIBHACL_SHA2_HEADERS) $(LIBHACL_
27992799
MODULE__SHA3_DEPS=$(srcdir)/Modules/hashlib.h $(LIBHACL_HEADERS) Modules/_hacl/Hacl_Hash_SHA3.h Modules/_hacl/Hacl_Hash_SHA3.c
28002800
MODULE__SOCKET_DEPS=$(srcdir)/Modules/socketmodule.h $(srcdir)/Modules/addrinfo.h $(srcdir)/Modules/getaddrinfo.c $(srcdir)/Modules/getnameinfo.c
28012801
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
2802-
MODULE__TESTCAPI_DEPS=$(srcdir)/Modules/_testcapi/testcapi_long.h $(srcdir)/Modules/_testcapi/parts.h
2802+
MODULE__TESTCAPI_DEPS=$(srcdir)/Modules/_testcapi/testcapi_long.h $(srcdir)/Modules/_testcapi/parts.h $(srcdir)/Modules/_testcapi/util.h
28032803
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
28042804

28052805
CODECS_COMMON_HEADERS=$(srcdir)/Modules/cjkcodecs/multibytecodec.h $(srcdir)/Modules/cjkcodecs/cjkcodecs.h

Modules/_testcapi/abstract.c

+1-18
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,7 @@
11
#include <stddef.h> // ptrdiff_t
22

33
#include "parts.h"
4-
5-
#define NULLABLE(x) do { if (x == Py_None) x = NULL; } while (0);
6-
7-
#define RETURN_INT(value) do { \
8-
int _ret = (value); \
9-
if (_ret == -1) { \
10-
return NULL; \
11-
} \
12-
return PyLong_FromLong(_ret); \
13-
} while (0)
14-
15-
#define RETURN_SIZE(value) do { \
16-
Py_ssize_t _ret = (value); \
17-
if (_ret == -1) { \
18-
return NULL; \
19-
} \
20-
return PyLong_FromSsize_t(_ret); \
21-
} while (0)
4+
#include "util.h"
225

236

247
static PyObject *

Modules/_testcapi/dict.c

+1-18
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,7 @@
11
#include <stddef.h> // ptrdiff_t
22

33
#include "parts.h"
4-
5-
#define NULLABLE(x) do { if (x == Py_None) x = NULL; } while (0);
6-
7-
#define RETURN_INT(value) do { \
8-
int _ret = (value); \
9-
if (_ret == -1) { \
10-
return NULL; \
11-
} \
12-
return PyLong_FromLong(_ret); \
13-
} while (0)
14-
15-
#define RETURN_SIZE(value) do { \
16-
Py_ssize_t _ret = (value); \
17-
if (_ret == -1) { \
18-
return NULL; \
19-
} \
20-
return PyLong_FromSsize_t(_ret); \
21-
} while (0)
4+
#include "util.h"
225

236

247
static PyObject *

Modules/_testcapi/exceptions.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "parts.h"
2+
#include "util.h"
23
#include "clinic/exceptions.c.h"
34

4-
#define NULLABLE(x) do { if (x == Py_None) x = NULL; } while (0);
55

66
/*[clinic input]
77
module _testcapi

Modules/_testcapi/unicode.c

+18-40
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stddef.h> // ptrdiff_t
22

33
#include "parts.h"
4+
#include "util.h"
45

56
static struct PyModuleDef *_testcapimodule = NULL; // set at initialization
67

@@ -101,7 +102,6 @@ test_widechar(PyObject *self, PyObject *Py_UNUSED(ignored))
101102
Py_RETURN_NONE;
102103
}
103104

104-
#define NULLABLE(x) do { if (x == Py_None) x = NULL; } while (0);
105105

106106
static PyObject *
107107
unicode_copy(PyObject *unicode)
@@ -347,13 +347,8 @@ unicode_substring(PyObject *self, PyObject *args)
347347
static PyObject *
348348
unicode_getlength(PyObject *self, PyObject *arg)
349349
{
350-
Py_ssize_t result;
351-
352350
NULLABLE(arg);
353-
result = PyUnicode_GetLength(arg);
354-
if (result == -1)
355-
return NULL;
356-
return PyLong_FromSsize_t(result);
351+
RETURN_SIZE(PyUnicode_GetLength(arg));
357352
}
358353

359354
/* Test PyUnicode_ReadChar() */
@@ -482,16 +477,12 @@ static PyObject *
482477
unicode_aswidechar_null(PyObject *self, PyObject *args)
483478
{
484479
PyObject *unicode;
485-
Py_ssize_t buflen, size;
480+
Py_ssize_t buflen;
486481

487482
if (!PyArg_ParseTuple(args, "On", &unicode, &buflen))
488483
return NULL;
489484
NULLABLE(unicode);
490-
size = PyUnicode_AsWideChar(unicode, NULL, buflen);
491-
if (size == -1) {
492-
return NULL;
493-
}
494-
return PyLong_FromSsize_t(size);
485+
RETURN_SIZE(PyUnicode_AsWideChar(unicode, NULL, buflen));
495486
}
496487

497488
/* Test PyUnicode_AsWideCharString() */
@@ -1293,17 +1284,13 @@ unicode_count(PyObject *self, PyObject *args)
12931284
PyObject *substr;
12941285
Py_ssize_t start;
12951286
Py_ssize_t end;
1296-
Py_ssize_t result;
12971287

12981288
if (!PyArg_ParseTuple(args, "OOnn", &str, &substr, &start, &end))
12991289
return NULL;
13001290

13011291
NULLABLE(str);
13021292
NULLABLE(substr);
1303-
result = PyUnicode_Count(str, substr, start, end);
1304-
if (result == -1)
1305-
return NULL;
1306-
return PyLong_FromSsize_t(result);
1293+
RETURN_SIZE(PyUnicode_Count(str, substr, start, end));
13071294
}
13081295

13091296
/* Test PyUnicode_Find() */
@@ -1323,8 +1310,11 @@ unicode_find(PyObject *self, PyObject *args)
13231310
NULLABLE(str);
13241311
NULLABLE(substr);
13251312
result = PyUnicode_Find(str, substr, start, end, direction);
1326-
if (result == -2)
1313+
if (result == -2) {
1314+
assert(PyErr_Occurred());
13271315
return NULL;
1316+
}
1317+
assert(!PyErr_Occurred());
13281318
return PyLong_FromSsize_t(result);
13291319
}
13301320

@@ -1337,17 +1327,13 @@ unicode_tailmatch(PyObject *self, PyObject *args)
13371327
Py_ssize_t start;
13381328
Py_ssize_t end;
13391329
int direction;
1340-
Py_ssize_t result;
13411330

13421331
if (!PyArg_ParseTuple(args, "OOnni", &str, &substr, &start, &end, &direction))
13431332
return NULL;
13441333

13451334
NULLABLE(str);
13461335
NULLABLE(substr);
1347-
result = PyUnicode_Tailmatch(str, substr, start, end, direction);
1348-
if (result == -1)
1349-
return NULL;
1350-
return PyLong_FromSsize_t(result);
1336+
RETURN_SIZE(PyUnicode_Tailmatch(str, substr, start, end, direction));
13511337
}
13521338

13531339
/* Test PyUnicode_FindChar() */
@@ -1366,10 +1352,12 @@ unicode_findchar(PyObject *self, PyObject *args)
13661352
}
13671353
NULLABLE(str);
13681354
result = PyUnicode_FindChar(str, (Py_UCS4)ch, start, end, direction);
1369-
if (result == -2)
1355+
if (result == -2) {
1356+
assert(PyErr_Occurred());
13701357
return NULL;
1371-
else
1372-
return PyLong_FromSsize_t(result);
1358+
}
1359+
assert(!PyErr_Occurred());
1360+
return PyLong_FromSsize_t(result);
13731361
}
13741362

13751363
/* Test PyUnicode_Replace() */
@@ -1407,6 +1395,7 @@ unicode_compare(PyObject *self, PyObject *args)
14071395
if (result == -1 && PyErr_Occurred()) {
14081396
return NULL;
14091397
}
1398+
assert(!PyErr_Occurred());
14101399
return PyLong_FromLong(result);
14111400
}
14121401

@@ -1467,32 +1456,21 @@ unicode_contains(PyObject *self, PyObject *args)
14671456
{
14681457
PyObject *container;
14691458
PyObject *element;
1470-
int result;
14711459

14721460
if (!PyArg_ParseTuple(args, "OO", &container, &element))
14731461
return NULL;
14741462

14751463
NULLABLE(container);
14761464
NULLABLE(element);
1477-
result = PyUnicode_Contains(container, element);
1478-
if (result == -1 && PyErr_Occurred()) {
1479-
return NULL;
1480-
}
1481-
return PyLong_FromLong(result);
1465+
RETURN_INT(PyUnicode_Contains(container, element));
14821466
}
14831467

14841468
/* Test PyUnicode_IsIdentifier() */
14851469
static PyObject *
14861470
unicode_isidentifier(PyObject *self, PyObject *arg)
14871471
{
1488-
int result;
1489-
14901472
NULLABLE(arg);
1491-
result = PyUnicode_IsIdentifier(arg);
1492-
if (result == -1 && PyErr_Occurred()) {
1493-
return NULL;
1494-
}
1495-
return PyLong_FromLong(result);
1473+
RETURN_INT(PyUnicode_IsIdentifier(arg));
14961474
}
14971475

14981476
/* Test PyUnicode_CopyCharacters() */

Modules/_testcapi/util.h

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#define NULLABLE(x) do { \
2+
if (x == Py_None) { \
3+
x = NULL; \
4+
} \
5+
} while (0);
6+
7+
#define RETURN_INT(value) do { \
8+
int _ret = (value); \
9+
if (_ret == -1) { \
10+
assert(PyErr_Occurred()); \
11+
return NULL; \
12+
} \
13+
assert(!PyErr_Occurred()); \
14+
return PyLong_FromLong(_ret); \
15+
} while (0)
16+
17+
#define RETURN_SIZE(value) do { \
18+
Py_ssize_t _ret = (value); \
19+
if (_ret == -1) { \
20+
assert(PyErr_Occurred()); \
21+
return NULL; \
22+
} \
23+
assert(!PyErr_Occurred()); \
24+
return PyLong_FromSsize_t(_ret); \
25+
} while (0)

Modules/_testcapimodule.c

+2-16
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,8 @@
4343
// Several parts of this module are broken out into files in _testcapi/.
4444
// Include definitions from there.
4545
#include "_testcapi/parts.h"
46+
#include "_testcapi/util.h"
4647

47-
#define NULLABLE(x) do { if (x == Py_None) x = NULL; } while (0);
48-
49-
#define RETURN_INT(value) do { \
50-
int _ret = (value); \
51-
if (_ret == -1) { \
52-
return NULL; \
53-
} \
54-
return PyLong_FromLong(_ret); \
55-
} while (0)
5648

5749
// Forward declarations
5850
static struct PyModuleDef _testcapimodule;
@@ -1333,19 +1325,13 @@ static PyObject *
13331325
test_PyBuffer_SizeFromFormat(PyObject *self, PyObject *args)
13341326
{
13351327
const char *format;
1336-
Py_ssize_t result;
13371328

13381329
if (!PyArg_ParseTuple(args, "s:test_PyBuffer_SizeFromFormat",
13391330
&format)) {
13401331
return NULL;
13411332
}
13421333

1343-
result = PyBuffer_SizeFromFormat(format);
1344-
if (result == -1) {
1345-
return NULL;
1346-
}
1347-
1348-
return PyLong_FromSsize_t(result);
1334+
RETURN_SIZE(PyBuffer_SizeFromFormat(format));
13491335
}
13501336

13511337
/* Test that the fatal error from not having a current thread doesn't

0 commit comments

Comments
 (0)