8000 [3.12] C API tests: use special markers to test that output parameter… · python/cpython@e65401d · GitHub
[go: up one dir, main page]

Skip to content

Commit e65401d

Browse files
[3.12] C API tests: use special markers to test that output parameters were set (GH-109014) (#109023)
[3.12] C API tests: use special markers to test that output parameters were set (GH-109014). (cherry picked from commit bf414b7)
1 parent db55cfc commit e65401d

File tree

6 files changed

+50
-20
lines changed

6 files changed

+50
-20
lines changed

Modules/_testcapi/code.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "parts.h"
2+
#include "util.h"
23

34
static Py_ssize_t
45
get_code_extra_index(PyInterpreterState* interp) {
@@ -74,7 +75,7 @@ test_code_extra(PyObject* self, PyObject *Py_UNUSED(callable))
7475
}
7576

7677
// Check the value is initially NULL
77-
void *extra;
78+
void *extra = UNINITIALIZED_PTR;
7879< 8000 div class="diff-text-inner"> int res = PyUnstable_Code_GetExtra(test_func_code, code_extra_index, &extra);
7980
if (res < 0) {
8081
goto finally;
@@ -87,6 +88,7 @@ test_code_extra(PyObject* self, PyObject *Py_UNUSED(callable))
8788
goto finally;
8889
}
8990
// Assert it was set correctly
91+
extra = UNINITIALIZED_PTR;
9092
res = PyUnstable_Code_GetExtra(test_func_code, code_extra_index, &extra);
9193
if (res < 0) {
9294
goto finally;

Modules/_testcapi/dict.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ dict_items(PyObject *self, PyObject *obj)
212212
static PyObject *
213213
dict_next(PyObject *self, PyObject *args)
214214
{
215-
PyObject *mapping, *key, *value;
215+
PyObject *mapping, *key = UNINITIALIZED_PTR, *value = UNINITIALIZED_PTR;
216216
Py_ssize_t pos;
217217
if (!PyArg_ParseTuple(args, "On", &mapping, &pos)) {
218218
return NULL;
@@ -222,6 +222,8 @@ dict_next(PyObject *self, PyObject *args)
222222
if (rc != 0) {
223223
return Py_BuildValue("inOO", rc, pos, key, value);
224224
}
225+
assert(key == UNINITIALIZED_PTR);
226+
assert(value == UNINITIALIZED_PTR);
225227
if (PyErr_Occurred()) {
226228
return NULL;
227229
}

Modules/_testcapi/exceptions.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,15 @@ _testcapi_exc_set_object_fetch_impl(PyObject *module, PyObject *exc,
121121
PyObject *obj)
122122
/*[clinic end generated code: output=7a5ff5f6d3cf687f input=77ec686f1f95fa38]*/
123123
{
124-
PyObject *type;
125-
PyObject *value;
126-
PyObject *tb;
124+
PyObject *type = UNINITIALIZED_PTR;
125+
PyObject *value = UNINITIALIZED_PTR;
126+
PyObject *tb = UNINITIALIZED_PTR;
127127

128128
PyErr_SetObject(exc, obj);
129129
PyErr_Fetch(&type, &value, &tb);
130+
assert(type != UNINITIALIZED_PTR);
131+
assert(value != UNINITIALIZED_PTR);
132+
assert(tb != UNINITIALIZED_PTR);
130133
Py_XDECREF(type);
131134
Py_XDECREF(tb);
132135
return value;
@@ -245,7 +248,7 @@ _testcapi_set_exc_info_impl(PyObject *module, PyObject *new_type,
245248
PyObject *new_value, PyObject *new_tb)
246249
/*[clinic end generated code: output=b55fa35dec31300e input=ea9f19e0f55fe5b3]*/
247250
{
248-
PyObject *type, *value, *tb;
251+
PyObject *type = UNINITIALIZED_PTR, *value = UNINITIALIZED_PTR, *tb = UNINITIALIZED_PTR;
249252
PyErr_GetExcInfo(&type, &value, &tb);
250253

251254
Py_INCREF(new_type);

Modules/_testcapi/unicode.c

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -491,16 +491,18 @@ static PyObject *
491491
unicode_aswidecharstring(PyObject *self, PyObject *args)
492492
{
493493
PyObject *unicode, *result;
494-
Py_ssize_t size = 100;
494+
Py_ssize_t size = UNINITIALIZED_SIZE;
495495
wchar_t *buffer;
496496

497497
if (!PyArg_ParseTuple(args, "O", &unicode))
498498
return NULL;
499499

500500
NULLABLE(unicode);
501501
buffer = PyUnicode_AsWideCharString(unicode, &size);
502-
if (buffer == NULL)
502+
if (buffer == NULL) {
503+
assert(size == UNINITIALIZED_SIZE);
503504
return NULL;
505+
}
504506

505507
result = PyUnicode_FromWideChar(buffer, size + 1);
506508
PyMem_Free(buffer);
@@ -625,15 +627,17 @@ unicode_asutf8andsize(PyObject *self, PyObject *args)
625627
PyObject *unicode;
626628
Py_ssize_t buflen;
627629
const char *s;
628-
Py_ssize_t size = -100;
630+
Py_ssize_t size = UNINITIALIZED_SIZE;
629631

630632
if (!PyArg_ParseTuple(args, "On", &unicode, &buflen))
631633
return NULL;
632634

633635
NULLABLE(unicode);
634636
s = PyUnicode_AsUTF8AndSize(unicode, &size);
635-
if (s == NULL)
637+
if (s == NULL) {
638+
assert(size == UNINITIALIZED_SIZE);
636639
return NULL;
640+
}
637641

638642
return Py_BuildValue("(y#n)", s, buflen, size);
639643
}
@@ -735,14 +739,15 @@ unicode_decodeutf7stateful(PyObject *self, PyObject *args)
735739
const char *data;
736740
Py_ssize_t size;
737741
const char *errors = NULL;
738-
Py_ssize_t consumed;
742+
Py_ssize_t consumed = UNINITIALIZED_SIZE;
739743
PyObject *result;
740744

741745
if (!PyArg_ParseTuple(args, "y#|z", &data, &size, &errors))
742746
return NULL;
743747

744748
result = PyUnicode_DecodeUTF7Stateful(data, size, errors, &consumed);
745749
if (!result) {
750+
assert(consumed == UNINITIALIZED_SIZE);
746751
return NULL;
747752
}
748753
return Py_BuildValue("(Nn)", result, consumed);
@@ -769,14 +774,15 @@ unicode_decodeutf8stateful(PyObject *self, PyObject *args)
769774
const char *data;
770775
Py_ssize_t size;
771776
const char *errors = NULL;
772-
Py_ssize_t consumed = 123456789;
777+
Py_ssize_t consumed = UNINITIALIZED_SIZE;
773778
PyObject *result;
774779

775780
if (!PyArg_ParseTuple(args, "y#|z", &data, &size, &errors))
776781
return NULL;
777782

778783
result = PyUnicode_DecodeUTF8Stateful(data, size, errors, &consumed);
779784
if (!result) {
785+
assert(consumed == UNINITIALIZED_SIZE);
780786
return NULL;
781787
}
782788
return Py_BuildValue("(Nn)", result, consumed);
@@ -797,7 +803,7 @@ unicode_decodeutf32(PyObject *self, PyObject *args)
797803
const char *data;
798804
Py_ssize_t size;
799805
const char *errors = NULL;
800-
int byteorder;
806+
int byteorder = UNINITIALIZED_INT;
801807
PyObject *result;
802808

803809
if (!PyArg_ParseTuple(args, "iy#|z", &byteorder, &data, &size, &errors))
@@ -817,15 +823,16 @@ unicode_decodeutf32stateful(PyObject *self, PyObject *args)
817823
const char *data;
818824
Py_ssize_t size;
819825
const char *errors = NULL;
820-
int byteorder;
821-
Py_ssize_t consumed;
826+
int byteorder = UNINITIALIZED_INT;
827+
Py_ssize_t consumed = UNINITIALIZED_SIZE;
822828
PyObject *result;
823829

824830
if (!PyArg_ParseTuple(args, "iy#|z", &byteorder, &data, &size, &errors))
825831
return NULL;
826832

827833
result = PyUnicode_DecodeUTF32Stateful(data, size, errors, &byteorder, &consumed);
828834
if (!result) {
835+
assert(consumed == UNINITIALIZED_SIZE);
829836
return NULL;
830837
}
831838
return Py_BuildValue("(iNn)", byteorder, result, consumed);
@@ -846,7 +853,7 @@ unicode_decodeutf16(PyObject *self, PyObject *args)
846853
const char *data;
847854
Py_ssize_t size;
848855
const char *errors = NULL;
849-
int byteorder = 0;
856+
int byteorder = UNINITIALIZED_INT;
850857
PyObject *result;
851858

852859
if (!PyArg_ParseTuple(args, "iy#|z", &byteorder, &data, &size, &errors))
@@ -866,15 +873,16 @@ unicode_decodeutf16stateful(PyObject *self, PyObject *args)
866873
const char *data;
867874
Py_ssize_t size;
868875
const char *errors = NULL;
869-
int byteorder;
870-
Py_ssize_t consumed;
876+
int byteorder = UNINITIALIZED_INT;
877+
Py_ssize_t consumed = UNINITIALIZED_SIZE;
871878
PyObject *result;
872879

873880
if (!PyArg_ParseTuple(args, "iy#|z", &byteorder, &data, &size, &errors))
874881
return NULL;
875882

876883
result = PyUnicode_DecodeUTF16Stateful(data, size, errors, &byteorder, &consumed);
877884
if (!result) {
885+
assert(consumed == UNINITIALIZED_SIZE);
878886
return NULL;
879887
}
880888
return Py_BuildValue("(iNn)", byteorder, result, consumed);
@@ -1028,14 +1036,15 @@ unicode_decodembcsstateful(PyObject *self, PyObject *args)
10281036
const char *data;
10291037
Py_ssize_t size;
10301038
const char *errors = NULL;
1031-
Py_ssize_t consumed;
1039+
Py_ssize_t consumed = UNINITIALIZED_SIZE;
10321040
PyObject *result;
10331041

10341042
if (!PyArg_ParseTuple(args, "y#|z", &data, &size, &errors))
10351043
return NULL;
10361044

10371045
result = PyUnicode_DecodeMBCSStateful(data, size, errors, &consumed);
10381046
if (!result) {
1047+
assert(consumed == UNINITIALIZED_SIZE);
10391048
return NULL;
10401049
}
10411050
return Py_BuildValue("(Nn)", result, consumed);
@@ -1049,14 +1058,15 @@ unicode_decodecodepagestateful(PyObject *self, PyObject *args)
10491058
const char *data;
10501059
Py_ssize_t size;
10511060
const char *errors = NULL;
1052-
Py_ssize_t consumed;
1061+
Py_ssize_t consumed = UNINITIALIZED_SIZE;
10531062
PyObject *result;
10541063

10551064
if (!PyArg_ParseTuple(args, "iy#|z", &code_page, &data, &size, &errors))
10561065
return NULL;
10571066

10581067
result = PyUnicode_DecodeCodePageStateful(code_page, data, size, errors, &consumed);
10591068
if (!result) {
1069+
assert(consumed == UNINITIALIZED_SIZE);
10601070
return NULL;
10611071
}
10621072
return Py_BuildValue("(Nn)", result, consumed);

Modules/_testcapi/util.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,10 @@
2323
assert(!PyErr_Occurred()); \
2424
return PyLong_FromSsize_t(_ret); \
2525
} while (0)
26+
27+
/* Marker to check that pointer value was set. */
28+
#define UNINITIALIZED_PTR ((void *)"uninitialized")
29+
/* Marker to check that Py_ssize_t value was set. */
30+
#define UNINITIALIZED_SIZE ((Py_ssize_t)236892191)
31+
/* Marker to check that integer value was set. */
32+
#define UNINITIALIZED_INT (63256717)

Modules/_testcapimodule.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,13 @@ test_dict_inner(int count)
221221
Py_DECREF(v);
222222
}
223223

224+
k = v = UNINITIALIZED_PTR;
224225
while (PyDict_Next(dict, &pos, &k, &v)) {
225226
PyObject *o;
226227
iterations++;
227228

229+
assert(k != UNINITIALIZED_PTR);
230+
assert(v != UNINITIALIZED_PTR);
228231
i = PyLong_AS_LONG(v) + 1;
229232
o = PyLong_FromLong(i);
230233
if (o == NULL)
@@ -234,7 +237,10 @@ test_dict_inner(int count)
234237
return -1;
235238
}
236239
Py_DECREF(o);
240+
k = v = UNINITIALIZED_PTR;
237241
}
242+
assert(k == UNINITIALIZED_PTR);
243+
assert(v == UNINITIALIZED_PTR);
238244

239245
Py_DECREF(dict);
240246

0 commit comments

Comments
 (0)
0