Skip to content

Commit bbba3f3

Browse files
authored
gh-99300: Use Py_NewRef() in Modules/ directory (#99440)
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in test C files of the Modules/ directory.
1 parent 0bedc28 commit bbba3f3

File tree

6 files changed

+41
-82
lines changed

6 files changed

+41
-82
lines changed

Modules/_testbuffer.c

+5-10
Original file line numberDiff line numberDiff line change
@@ -1524,8 +1524,7 @@ ndarray_getbuf(NDArrayObject *self, Py_buffer *view, int flags)
15241524
return -1;
15251525
}
15261526

1527-
view->obj = (PyObject *)self;
1528-
Py_INCREF(view->obj);
1527+
view->obj = Py_NewRef(self);
15291528
self->head->exports++;
15301529

15311530
return 0;
@@ -2021,8 +2020,7 @@ ndarray_get_obj(NDArrayObject *self, void *closure)
20212020
if (base->obj == NULL) {
20222021
Py_RETURN_NONE;
20232022
}
2024-
Py_INCREF(base->obj);
2025-
return base->obj;
2023+
return Py_NewRef(base->obj);
20262024
}
20272025

20282026
static PyObject *
@@ -2559,8 +2557,7 @@ cmp_contig(PyObject *self, PyObject *args)
25592557
PyBuffer_Release(&v2);
25602558

25612559
ret = equal ? Py_True : Py_False;
2562-
Py_INCREF(ret);
2563-
return ret;
2560+
return Py_NewRef(ret);
25642561
}
25652562

25662563
static PyObject *
@@ -2597,8 +2594,7 @@ is_contiguous(PyObject *self, PyObject *args)
25972594
PyBuffer_Release(&view);
25982595
}
25992596

2600-
Py_INCREF(ret);
2601-
return ret;
2597+
return Py_NewRef(ret);
26022598
}
26032599

26042600
static Py_hash_t
@@ -2748,8 +2744,7 @@ staticarray_getbuf(StaticArrayObject *self, Py_buffer *view, int flags)
27482744
view->obj = NULL; /* Don't use this in new code. */
27492745
}
27502746
else {
2751-
view->obj = (PyObject *)self;
2752-
Py_INCREF(view->obj);
2747+
view->obj = Py_NewRef(self);
27532748
}
27542749

27552750
return 0;

Modules/_testcapi/vectorcall.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -297,24 +297,21 @@ static PyObject *
297297
func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
298298
{
299299
if (obj == Py_None || obj == NULL) {
300-
Py_INCREF(func);
301-
return func;
300+
return Py_NewRef(func);
302301
}
303302
return PyMethod_New(func, obj);
304303
}
305304

306305
static PyObject *
307306
nop_descr_get(PyObject *func, PyObject *obj, PyObject *type)
308307
{
309-
Py_INCREF(func);
310-
return func;
308+
return Py_NewRef(func);
311309
}
312310

313311
static PyObject *
314312
call_return_args(PyObject *self, PyObject *args, PyObject *kwargs)
315313
{
316-
Py_INCREF(args);
317-
return args;
314+
return Py_NewRef(args);
318315
}
319316

320317
static PyTypeObject MethodDescriptorBase_Type = {

Modules/_testcapimodule.c

+28-56
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,7 @@ dict_getitem_knownhash(PyObject *self, PyObject *args)
342342
return NULL;
343343
}
344344

345-
Py_XINCREF(result);
346-
return result;
345+
return Py_XNewRef(result);
347346
}
348347

349348
/* Issue #4701: Check that PyObject_Hash implicitly calls
@@ -1242,8 +1241,7 @@ get_args(PyObject *self, PyObject *args)
12421241
if (args == NULL) {
12431242
args = Py_None;
12441243
}
1245-
Py_INCREF(args);
1246-
return args;
1244+
return Py_NewRef(args);
12471245
}
12481246

12491247
static PyObject *
@@ -1252,8 +1250,7 @@ get_kwargs(PyObject *self, PyObject *args, PyObject *kwargs)
12521250
if (kwargs == NULL) {
12531251
kwargs = Py_None;
12541252
}
1255-
Py_INCREF(kwargs);
1256-
return kwargs;
1253+
return Py_NewRef(kwargs);
12571254
}
12581255

12591256
/* Test tuple argument processing */
@@ -1514,8 +1511,7 @@ getargs_S(PyObject *self, PyObject *args)
15141511
PyObject *obj;
15151512
if (!PyArg_ParseTuple(args, "S", &obj))
15161513
return NULL;
1517-
Py_INCREF(obj);
1518-
return obj;
1514+
return Py_NewRef(obj);
15191515
}
15201516

15211517
static PyObject *
@@ -1524,8 +1520,7 @@ getargs_Y(PyObject *self, PyObject *args)
15241520
PyObject *obj;
15251521
if (!PyArg_ParseTuple(args, "Y", &obj))
15261522
return NULL;
1527-
Py_INCREF(obj);
1528-
return obj;
1523+
return Py_NewRef(obj);
15291524
}
15301525

15311526
static PyObject *
@@ -1534,8 +1529,7 @@ getargs_U(PyObject *self, PyObject *args)
15341529
PyObject *obj;
15351530
if (!PyArg_ParseTuple(args, "U", &obj))
15361531
return NULL;
1537-
Py_INCREF(obj);
1538-
return obj;
1532+
return Py_NewRef(obj);
15391533
}
15401534

15411535
static PyObject *
@@ -1609,8 +1603,7 @@ getargs_z_star(PyObject *self, PyObject *args)
16091603
if (buffer.buf != NULL)
16101604
bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
16111605
else {
1612-
Py_INCREF(Py_None);
1613-
bytes = Py_None;
1606+
bytes = Py_NewRef(Py_None);
16141607
}
16151608
PyBuffer_Release(&buffer);
16161609
return bytes;
@@ -1873,8 +1866,7 @@ parse_tuple_and_keywords(PyObject *self, PyObject *args)
18731866
buffers + 4, buffers + 5, buffers + 6, buffers + 7);
18741867

18751868
if (result) {
1876-
return_value = Py_None;
1877-
Py_INCREF(Py_None);
1869+
return_value = Py_NewRef(Py_None);
18781870
}
18791871

18801872
exit:
@@ -2193,11 +2185,9 @@ get_timezone_utc_capi(PyObject* self, PyObject *args) {
21932185
return NULL;
21942186
}
21952187
if (macro) {
2196-
Py_INCREF(PyDateTime_TimeZone_UTC);
2197-
return PyDateTime_TimeZone_UTC;
2188+
return Py_NewRef(PyDateTime_TimeZone_UTC);
21982189
} else {
2199-
Py_INCREF(PyDateTimeAPI->TimeZone_UTC);
2200-
return PyDateTimeAPI->TimeZone_UTC;
2190+
return Py_NewRef(PyDateTimeAPI->TimeZone_UTC);
22012191
}
22022192
}
22032193

@@ -2984,8 +2974,7 @@ failing_converter(PyObject *obj, void *arg)
29842974
{
29852975
/* Clone str1, then let the conversion fail. */
29862976
assert(str1);
2987-
str2 = str1;
2988-
Py_INCREF(str2);
2977+
str2 = Py_NewRef(str1);
29892978
return 0;
29902979
}
29912980
static PyObject*
@@ -3428,8 +3417,7 @@ with_tp_del(PyObject *self, PyObject *args)
34283417
return NULL;
34293418
}
34303419
tp->tp_del = slot_tp_del;
3431-
Py_INCREF(obj);
3432-
return obj;
3420+
return Py_NewRef(obj);
34333421
}
34343422

34353423
static PyObject *
@@ -3447,8 +3435,7 @@ without_gc(PyObject *Py_UNUSED(self), PyObject *obj)
34473435
tp->tp_clear = NULL;
34483436
}
34493437
assert(!PyType_IS_GC(tp));
3450-
Py_INCREF(obj);
3451-
return obj;
3438+
return Py_NewRef(obj);
34523439
}
34533440

34543441
static PyMethodDef ml;
@@ -3469,8 +3456,7 @@ static PyMethodDef ml = {
34693456
static PyObject *
34703457
_test_incref(PyObject *ob)
34713458
{
3472-
Py_INCREF(ob);
3473-
return ob;
3459+
return Py_NewRef(ob);
34743460
}
34753461

34763462
static PyObject *
@@ -3817,8 +3803,7 @@ test_setallocators(PyMemAllocatorDomain domain)
38173803
goto fail;
38183804
}
38193805

3820-
Py_INCREF(Py_None);
3821-
res = Py_None;
3806+
res = Py_NewRef(Py_None);
38223807
goto finally;
38233808

38243809
fail:
@@ -4078,8 +4063,7 @@ call_in_temporary_c_thread(PyObject *self, PyObject *callback)
40784063
goto exit;
40794064
}
40804065

4081-
Py_INCREF(callback);
4082-
test_c_thread.callback = callback;
4066+
test_c_thread.callback = Py_NewRef(callback);
40834067

40844068
PyThread_acquire_lock(test_c_thread.start_event, 1);
40854069
PyThread_acquire_lock(test_c_thread.exit_event, 1);
@@ -4100,8 +4084,7 @@ call_in_temporary_c_thread(PyObject *self, PyObject *callback)
41004084
PyThread_release_lock(test_c_thread.exit_event);
41014085
Py_END_ALLOW_THREADS
41024086

4103-
Py_INCREF(Py_None);
4104-
res = Py_None;
4087+
res = Py_NewRef(Py_None);
41054088

41064089
exit:
41074090
Py_CLEAR(test_c_thread.callback);
@@ -5037,8 +5020,7 @@ _null_to_none(PyObject* obj)
50375020
if (obj == NULL) {
50385021
Py_RETURN_NONE;
50395022
}
5040-
Py_INCREF(obj);
5041-
return obj;
5023+
return Py_NewRef(obj);
50425024
}
50435025

50445026
static PyObject*
@@ -5554,8 +5536,7 @@ get_dict_watcher_events(PyObject *self, PyObject *Py_UNUSED(args))
55545536
PyErr_SetString(PyExc_RuntimeError, "no watchers active");
55555537
return NULL;
55565538
}
5557-
Py_INCREF(g_dict_watch_events);
5558-
return g_dict_watch_events;
5539+
return Py_NewRef(g_dict_watch_events);
55595540
}
55605541

55615542

@@ -5924,8 +5905,7 @@ function_get_code(PyObject *self, PyObject *func)
59245905
{
59255906
PyObject *code = PyFunction_GetCode(func);
59265907
if (code != NULL) {
5927-
Py_INCREF(code);
5928-
return code;
5908+
return Py_NewRef(code);
59295909
} else {
59305910
return NULL;
59315911
}
@@ -5936,8 +5916,7 @@ function_get_globals(PyObject *self, PyObject *func)
59365916
{
59375917
PyObject *globals = PyFunction_GetGlobals(func);
59385918
if (globals != NULL) {
5939-
Py_INCREF(globals);
5940-
return globals;
5919+
return Py_NewRef(globals);
59415920
} else {
59425921
return NULL;
59435922
}
@@ -5948,8 +5927,7 @@ function_get_module(PyObject *self, PyObject *func)
59485927
{
59495928
PyObject *module = PyFunction_GetModule(func);
59505929
if (module != NULL) {
5951-
Py_INCREF(module);
5952-
return module;
5930+
return Py_NewRef(module);
59535931
} else {
59545932
return NULL;
59555933
}
@@ -5960,8 +5938,7 @@ function_get_defaults(PyObject *self, PyObject *func)
59605938
{
59615939
PyObject *defaults = PyFunction_GetDefaults(func);
59625940
if (defaults != NULL) {
5963-
Py_INCREF(defaults);
5964-
return defaults;
5941+
return Py_NewRef(defaults);
59655942
} else if (PyErr_Occurred()) {
59665943
return NULL;
59675944
} else {
@@ -5987,8 +5964,7 @@ function_get_kw_defaults(PyObject *self, PyObject *func)
59875964
{
59885965
PyObject *defaults = PyFunction_GetKwDefaults(func);
59895966
if (defaults != NULL) {
5990-
Py_INCREF(defaults);
5991-
return defaults;
5967+
return Py_NewRef(defaults);
59925968
} else if (PyErr_Occurred()) {
59935969
return NULL;
59945970
} else {
@@ -6099,8 +6075,7 @@ get_type_modified_events(PyObject *self, PyObject *Py_UNUSED(args))
60996075
PyErr_SetString(PyExc_RuntimeError, "no watchers active");
61006076
return NULL;
61016077
}
6102-
Py_INCREF(g_type_modified_events);
6103-
return g_type_modified_events;
6078+
return Py_NewRef(g_type_modified_events);
61046079
}
61056080

61066081
static PyObject *
@@ -6729,8 +6704,7 @@ awaitObject_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
67296704
return NULL;
67306705
}
67316706

6732-
Py_INCREF(v);
6733-
ao->ao_iterator = v;
6707+
ao->ao_iterator = Py_NewRef(v);
67346708

67356709
return (PyObject *)ao;
67366710
}
@@ -6747,8 +6721,7 @@ awaitObject_dealloc(awaitObject *ao)
67476721
static PyObject *
67486722
awaitObject_await(awaitObject *ao)
67496723
{
6750-
Py_INCREF(ao->ao_iterator);
6751-
return ao->ao_iterator;
6724+
return Py_NewRef(ao->ao_iterator);
67526725
}
67536726

67546727
static PyAsyncMethods awaitType_as_async = {
@@ -6969,8 +6942,7 @@ generic_alias_new(PyObject *item)
69696942
if (o == NULL) {
69706943
return NULL;
69716944
}
6972-
Py_INCREF(item);
6973-
o->item = item;
6945+
o->item = Py_NewRef(item);
69746946
return (PyObject*) o;
69756947
}
69766948

Modules/_testinternalcapi.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,7 @@ set_eval_frame_record(PyObject *self, PyObject *list)
524524
return NULL;
525525
}
526526
Py_CLEAR(record_list);
527-
Py_INCREF(list);
528-
record_list = list;
527+
record_list = Py_NewRef(list);
529528
_PyInterpreterState_SetEvalFrameFunc(PyInterpreterState_Get(), record_eval);
530529
Py_RETURN_NONE;
531530
}

Modules/_testmultiphase.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ Example_demo(ExampleObject *self, PyObject *args)
5757
if (!PyArg_ParseTuple(args, "|O:demo", &o))
5858
return NULL;
5959
if (o != NULL && PyUnicode_Check(o)) {
60-
Py_INCREF(o);
61-
return o;
60+
return Py_NewRef(o);
6261
}
6362
Py_RETURN_NONE;
6463
}
@@ -77,8 +76,7 @@ Example_getattro(ExampleObject *self, PyObject *name)
7776
if (self->x_attr != NULL) {
7877
PyObject *v = PyDict_GetItemWithError(self->x_attr, name);
7978
if (v != NULL) {
80-
Py_INCREF(v);
81-
return v;
79+
return Py_NewRef(v);
8280
}
8381
else if (PyErr_Occurred()) {
8482
return NULL;
@@ -151,8 +149,7 @@ _testmultiphase_StateAccessType_get_defining_module_impl(StateAccessTypeObject *
151149
return NULL;
152150
}
153151
assert(PyType_GetModuleByDef(Py_TYPE(self), &def_meth_state_access) == retval);
154-
Py_INCREF(retval);
155-
return retval;
152+
return Py_NewRef(retval);
156153
}
157154

158155
/*[clinic input]

Modules/testcapi_long.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,5 @@ TESTNAME(PyObject *error(const char*))
202202
Py_DECREF(Py_None);
203203
}
204204

205-
Py_INCREF(Py_None);
206-
return Py_None;
205+
return Py_NewRef(Py_None);
207206
}

0 commit comments

Comments
 (0)