Skip to content

Commit ba7e06b

Browse files
[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 0e01fac) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent f58617a commit ba7e06b

File tree

7 files changed

+49
-94
lines changed

7 files changed

+49
-94
lines changed

Makefile.pre.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2792,7 +2792,7 @@ MODULE__SHA2_DEPS=$(srcdir)/Modules/hashlib.h $(LIBHACL_SHA2_HEADERS) $(LIBHACL_
27922792
MODULE__SHA3_DEPS=$(srcdir)/Modules/hashlib.h $(LIBHACL_HEADERS) Modules/_hacl/Hacl_Hash_SHA3.h Modules/_hacl/Hacl_Hash_SHA3.c
27932793
MODULE__SOCKET_DEPS=$(srcdir)/Modules/socketmodule.h $(srcdir)/Modules/addrinfo.h $(srcdir)/Modules/getaddrinfo.c $(srcdir)/Modules/getnameinfo.c
27942794
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
2795-
MODULE__TESTCAPI_DEPS=$(srcdir)/Modules/_testcapi/testcapi_long.h $(srcdir)/Modules/_testcapi/parts.h
2795+
MODULE__TESTCAPI_DEPS=$(srcdir)/Modules/_testcapi/testcapi_long.h $(srcdir)/Modules/_testcapi/parts.h $(srcdir)/Modules/_testcapi/util.h
27962796
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
27972797

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

Modules/_testcapi/abstract.c

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,7 @@
22

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

247

258
static PyObject *

Modules/_testcapi/dict.c

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,7 @@
22

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

247

258
static PyObject *

Modules/_testcapi/exceptions.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#define PY_SSIZE_T_CLEAN
22
#include "parts.h"
3+
#include "util.h"
34
#include "clinic/exceptions.c.h"
45

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

77
/*[clinic input]
88
module _testcapi

Modules/_testcapi/unicode.c

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#define PY_SSIZE_T_CLEAN
44
#include "parts.h"
5+
#include "util.h"
56

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

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

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

107107
static PyObject *
108108
unicode_copy(PyObject *unicode)
@@ -348,13 +348,8 @@ unicode_substring(PyObject *self, PyObject *args)
348348
static PyObject *
349349
unicode_getlength(PyObject *self, PyObject *arg)
350350
{
351-
Py_ssize_t result;
352-
353351
NULLABLE(arg);
354-
result = PyUnicode_GetLength(arg);
355-
if (result == -1)
356-
return NULL;
357-
return PyLong_FromSsize_t(result);
352+
RETURN_SIZE(PyUnicode_GetLength(arg));
358353
}
359354

360355
/* Test PyUnicode_ReadChar() */
@@ -483,16 +478,12 @@ static PyObject *
483478
unicode_aswidechar_null(PyObject *self, PyObject *args)
484479
{
485480
PyObject *unicode;
486-
Py_ssize_t buflen, size;
481+
Py_ssize_t buflen;
487482

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

498489
/* Test PyUnicode_AsWideCharString() */
@@ -1302,17 +1293,13 @@ unicode_count(PyObject *self, PyObject *args)
13021293
PyObject *substr;
13031294
Py_ssize_t start;
13041295
Py_ssize_t end;
1305-
Py_ssize_t result;
13061296

13071297
if (!PyArg_ParseTuple(args, "OOnn", &str, &substr, &start, &end))
13081298
return NULL;
13091299

13101300
NULLABLE(str);
13111301
NULLABLE(substr);
1312-
result = PyUnicode_Count(str, substr, start, end);
1313-
if (result == -1)
1314-
return NULL;
1315-
return PyLong_FromSsize_t(result);
1302+
RETURN_SIZE(PyUnicode_Count(str, substr, start, end));
13161303
}
13171304

13181305
/* Test PyUnicode_Find() */
@@ -1332,8 +1319,11 @@ unicode_find(PyObject *self, PyObject *args)
13321319
NULLABLE(str);
13331320
NULLABLE(substr);
13341321
result = PyUnicode_Find(str, substr, start, end, direction);
1335-
if (result == -2)
1322+
if (result == -2) {
1323+
assert(PyErr_Occurred());
13361324
return NULL;
1325+
}
1326+
assert(!PyErr_Occurred());
13371327
return PyLong_FromSsize_t(result);
13381328
}
13391329

@@ -1346,17 +1336,13 @@ unicode_tailmatch(PyObject *self, PyObject *args)
13461336
Py_ssize_t start;
13471337
Py_ssize_t end;
13481338
int direction;
1349-
Py_ssize_t result;
13501339

13511340
if (!PyArg_ParseTuple(args, "OOnni", &str, &substr, &start, &end, &direction))
13521341
return NULL;
13531342

13541343
NULLABLE(str);
13551344
NULLABLE(substr);
1356-
result = PyUnicode_Tailmatch(str, substr, start, end, direction);
1357-
if (result == -1)
1358-
return NULL;
1359-
return PyLong_FromSsize_t(result);
1345+
RETURN_SIZE(PyUnicode_Tailmatch(str, substr, start, end, direction));
13601346
}
13611347

13621348
/* Test PyUnicode_FindChar() */
@@ -1375,10 +1361,12 @@ unicode_findchar(PyObject *self, PyObject *args)
13751361
}
13761362
NULLABLE(str);
13771363
result = PyUnicode_FindChar(str, (Py_UCS4)ch, start, end, direction);
1378-
if (result == -2)
1364+
if (result == -2) {
1365+
assert(PyErr_Occurred());
13791366
return NULL;
1380-
else
1381-
return PyLong_FromSsize_t(result);
1367+
}
1368+
assert(!PyErr_Occurred());
1369+
return PyLong_FromSsize_t(result);
13821370
}
13831371

13841372
/* Test PyUnicode_Replace() */
@@ -1416,6 +1404,7 @@ unicode_compare(PyObject *self, PyObject *args)
14161404
if (result == -1 && PyErr_Occurred()) {
14171405
return NULL;
14181406
}
1407+
assert(!PyErr_Occurred());
14191408
return PyLong_FromLong(result);
14201409
}
14211410

@@ -1476,32 +1465,21 @@ unicode_contains(PyObject *self, PyObject *args)
14761465
{
14771466
PyObject *container;
14781467
PyObject *element;
1479-
int result;
14801468

14811469
if (!PyArg_ParseTuple(args, "OO", &container, &element))
14821470
return NULL;
14831471

14841472
NULLABLE(container);
14851473
NULLABLE(element);
1486-
result = PyUnicode_Contains(container, element);
1487-
if (result == -1 && PyErr_Occurred()) {
1488-
return NULL;
1489-
}
1490-
return PyLong_FromLong(result);
1474+
RETURN_INT(PyUnicode_Contains(container, element));
14911475
}
14921476

14931477
/* Test PyUnicode_IsIdentifier() */
14941478
static PyObject *
14951479
unicode_isidentifier(PyObject *self, PyObject *arg)
14961480
{
1497-
int result;
1498-
14991481
NULLABLE(arg);
1500-
result = PyUnicode_IsIdentifier(arg);
1501-
if (result == -1 && PyErr_Occurred()) {
1502-
return NULL;
1503-
}
1504-
return PyLong_FromLong(result);
1482+
RETURN_INT(PyUnicode_IsIdentifier(arg));
15051483
}
15061484

15071485
/* Test PyUnicode_CopyCharacters() */

Modules/_testcapi/util.h

Lines changed: 25 additions & 0 deletions
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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,8 @@
4444
// Several parts of this module are broken out into files in _testcapi/.
4545
// Include definitions from there.
4646
#include "_testcapi/parts.h"
47+
#include "_testcapi/util.h"
4748

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

5850
// Forward declarations
5951
static struct PyModuleDef _testcapimodule;
@@ -1348,19 +1340,13 @@ static PyObject *
13481340
test_PyBuffer_SizeFromFormat(PyObject *self, PyObject *args)
13491341
{
13501342
const char *format;
1351-
Py_ssize_t result;
13521343

13531344
if (!PyArg_ParseTuple(args, "s:test_PyBuffer_SizeFromFormat",
13541345
&format)) {
13551346
return NULL;
13561347
}
13571348

1358-
result = PyBuffer_SizeFromFormat(format);
1359-
if (result == -1) {
1360-
return NULL;
1361-
}
1362-
1363-
return PyLong_FromSsize_t(result);
1349+
RETURN_SIZE(PyBuffer_SizeFromFormat(format));
13641350
}
13651351

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

0 commit comments

Comments
 (0)