Skip to content

Commit 228b12e

Browse files
Issue #28999: Use Py_RETURN_NONE, Py_RETURN_TRUE and Py_RETURN_FALSE wherever
possible. Patch is writen with Coccinelle.
1 parent 60e6e96 commit 228b12e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+225
-445
lines changed

Modules/_blake2/blake2b_impl.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,7 @@ _blake2b_blake2b_update(BLAKE2bObject *self, PyObject *obj)
310310
#endif /* !WITH_THREAD */
311311
PyBuffer_Release(&buf);
312312

313-
Py_INCREF(Py_None);
314-
return Py_None;
313+
Py_RETURN_NONE;
315314
}
316315

317316
/*[clinic input]

Modules/_blake2/blake2s_impl.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,7 @@ _blake2s_blake2s_update(BLAKE2sObject *self, PyObject *obj)
310310
#endif /* !WITH_THREAD */
311311
PyBuffer_Release(&buf);
312312

313-
Py_INCREF(Py_None);
314-
return Py_None;
313+
Py_RETURN_NONE;
315314
}
316315

317316
/*[clinic input]

Modules/_csv.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,7 @@ static PyObject *
152152
get_nullchar_as_None(Py_UCS4 c)
153153
{
154154
if (c == '\0') {
155-
Py_INCREF(Py_None);
156-
return Py_None;
155+
Py_RETURN_NONE;
157156
}
158157
else
159158
return PyUnicode_FromOrdinal(c);
@@ -1294,8 +1293,7 @@ csv_writerows(WriterObj *self, PyObject *seqseq)
12941293
Py_DECREF(row_iter);
12951294
if (PyErr_Occurred())
12961295
return NULL;
1297-
Py_INCREF(Py_None);
1298-
return Py_None;
1296+
Py_RETURN_NONE;
12991297
}
13001298

13011299
static struct PyMethodDef Writer_methods[] = {
@@ -1450,17 +1448,15 @@ csv_register_dialect(PyObject *module, PyObject *args, PyObject *kwargs)
14501448
return NULL;
14511449
}
14521450
Py_DECREF(dialect);
1453-
Py_INCREF(Py_None);
1454-
return Py_None;
1451+
Py_RETURN_NONE;
14551452
}
14561453

14571454
static PyObject *
14581455
csv_unregister_dialect(PyObject *module, PyObject *name_obj)
14591456
{
14601457
if (PyDict_DelItem(_csvstate_global->dialects, name_obj) < 0)
14611458
return PyErr_Format(_csvstate_global->error_obj, "unknown dialect");
1462-
Py_INCREF(Py_None);
1463-
return Py_None;
1459+
Py_RETURN_NONE;
14641460
}
14651461

14661462
static PyObject *

Modules/_ctypes/_ctypes.c

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,7 @@ _DictRemover_call(PyObject *myself, PyObject *args, PyObject *kw)
156156
Py_CLEAR(self->key);
157157
Py_CLEAR(self->dict);
158158
}
159-
Py_INCREF(Py_None);
160-
return Py_None;
159+
Py_RETURN_NONE;
161160
}
162161

163162
static PyTypeObject DictRemover_Type = {
@@ -979,8 +978,7 @@ PyCPointerType_set_type(PyTypeObject *self, PyObject *type)
979978
if (-1 == PyDict_SetItemString((PyObject *)dict, "_type_", type))
980979
return NULL;
981980

982-
Py_INCREF(Py_None);
983-
return Py_None;
981+
Py_RETURN_NONE;
984982
}
985983

986984
static PyObject *_byref(PyObject *);
@@ -1496,8 +1494,7 @@ c_wchar_p_from_param(PyObject *type, PyObject *value)
14961494
PyObject *as_parameter;
14971495
int res;
14981496
if (value == Py_None) {
1499-
Py_INCREF(Py_None);
1500-
return Py_None;
1497+
Py_RETURN_NONE;
15011498
}
15021499
if (PyUnicode_Check(value)) {
15031500
PyCArgObject *parg;
@@ -1561,8 +1558,7 @@ c_char_p_from_param(PyObject *type, PyObject *value)
15611558
PyObject *as_parameter;
15621559
int res;
15631560
if (value == Py_None) {
1564-
Py_INCREF(Py_None);
1565-
return Py_None;
1561+
Py_RETURN_NONE;
15661562
}
15671563
if (PyBytes_Check(value)) {
15681564
PyCArgObject *parg;
@@ -1629,8 +1625,7 @@ c_void_p_from_param(PyObject *type, PyObject *value)
16291625

16301626
/* None */
16311627
if (value == Py_None) {
1632-
Py_INCREF(Py_None);
1633-
return Py_None;
1628+
Py_RETURN_NONE;
16341629
}
16351630
/* Should probably allow buffer interface as well */
16361631
/* int, long */
@@ -2602,8 +2597,7 @@ PyCData_setstate(PyObject *myself, PyObject *args)
26022597
Py_DECREF(mydict);
26032598
if (res == -1)
26042599
return NULL;
2605-
Py_INCREF(Py_None);
2606-
return Py_None;
2600+
Py_RETURN_NONE;
26072601
}
26082602

26092603
/*
@@ -2825,8 +2819,7 @@ _PyCData_set(CDataObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value,
28252819
return result;
28262820
} else if (value == Py_None && PyCPointerTypeObject_Check(type)) {
28272821
*(void **)ptr = NULL;
2828-
Py_INCREF(Py_None);
2829-
return Py_None;
2822+
Py_RETURN_NONE;
28302823
} else {
28312824
PyErr_Format(PyExc_TypeError,
28322825
"expected %s instance, got %s",
@@ -2980,8 +2973,7 @@ PyCFuncPtr_get_errcheck(PyCFuncPtrObject *self)
29802973
Py_INCREF(self->errcheck);
29812974
return self->errcheck;
29822975
}
2983-
Py_INCREF(Py_None);
2984-
return Py_None;
2976+
Py_RETURN_NONE;
29852977
}
29862978

29872979
static int
@@ -3019,8 +3011,7 @@ PyCFuncPtr_get_restype(PyCFuncPtrObject *self)
30193011
Py_INCREF(dict->restype);
30203012
return dict->restype;
30213013
} else {
3022-
Py_INCREF(Py_None);
3023-
return Py_None;
3014+
Py_RETURN_NONE;
30243015
}
30253016
}
30263017

@@ -3057,8 +3048,7 @@ PyCFuncPtr_get_argtypes(PyCFuncPtrObject *self)
30573048
Py_INCREF(dict->argtypes);
30583049
return dict->argtypes;
30593050
} else {
3060-
Py_INCREF(Py_None);
3061-
return Py_None;
3051+
Py_RETURN_NONE;
30623052
}
30633053
}
30643054

Modules/_ctypes/_ctypes_test.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,7 @@ PyObject *py_func_si(PyObject *self, PyObject *args)
363363
int i;
364364
if (!PyArg_ParseTuple(args, "si", &name, &i))
365365
return NULL;
366-
Py_INCREF(Py_None);
367-
return Py_None;
366+
Py_RETURN_NONE;
368367
}
369368

370369
EXPORT(void) _py_func_si(char *s, int i)
@@ -373,8 +372,7 @@ EXPORT(void) _py_func_si(char *s, int i)
373372

374373
PyObject *py_func(PyObject *self, PyObject *args)
375374
{
376-
Py_INCREF(Py_None);
377-
return Py_None;
375+
Py_RETURN_NONE;
378376
}
379377

380378
EXPORT(void) _py_func(void)

Modules/_ctypes/callproc.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -892,8 +892,7 @@ static PyObject *GetResult(PyObject *restype, void *result, PyObject *checker)
892892
return PyLong_FromLong(*(int *)result);
893893

894894
if (restype == Py_None) {
895-
Py_INCREF(Py_None);
896-
return Py_None;
895+
Py_RETURN_NONE;
897896
}
898897

899898
dict = PyType_stgdict(restype);
@@ -1263,8 +1262,7 @@ static PyObject *free_library(PyObject *self, PyObject *args)
12631262
return NULL;
12641263
if (!FreeLibrary((HMODULE)hMod))
12651264
return PyErr_SetFromWindowsErr(GetLastError());
1266-
Py_INCREF(Py_None);
1267-
return Py_None;
1265+
Py_RETURN_NONE;
12681266
}
12691267

12701268
static const char copy_com_pointer_doc[] =
@@ -1349,8 +1347,7 @@ static PyObject *py_dl_close(PyObject *self, PyObject *args)
13491347
ctypes_dlerror());
13501348
return NULL;
13511349
}
1352-
Py_INCREF(Py_None);
1353-
return Py_None;
1350+
Py_RETURN_NONE;
13541351
}
13551352

13561353
static PyObject *py_dl_sym(PyObject *self, PyObject *args)
@@ -1624,8 +1621,7 @@ resize(PyObject *self, PyObject *args)
16241621
obj->b_size = size;
16251622
}
16261623
done:
1627-
Py_INCREF(Py_None);
1628-
return Py_None;
1624+
Py_RETURN_NONE;
16291625
}
16301626

16311627
static PyObject *

Modules/_ctypes/cfield.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,8 +1337,7 @@ z_get(void *ptr, Py_ssize_t size)
13371337
return PyBytes_FromStringAndSize(*(char **)ptr,
13381338
strlen(*(char **)ptr));
13391339
} else {
1340-
Py_INCREF(Py_None);
1341-
return Py_None;
1340+
Py_RETURN_NONE;
13421341
}
13431342
}
13441343

@@ -1360,8 +1359,7 @@ Z_set(void *ptr, PyObject *value, Py_ssize_t size)
13601359
#else
13611360
*(wchar_t **)ptr = (wchar_t *)PyLong_AsUnsignedLongMask(value);
13621361
#endif
1363-
Py_INCREF(Py_None);
1364-
return Py_None;
1362+
Py_RETURN_NONE;
13651363
}
13661364
if (!PyUnicode_Check(value)) {
13671365
PyErr_Format(PyExc_TypeError,
@@ -1392,8 +1390,7 @@ Z_get(void *ptr, Py_ssize_t size)
13921390
if (p) {
13931391
return PyUnicode_FromWideChar(p, wcslen(p));
13941392
} else {
1395-
Py_INCREF(Py_None);
1396-
return Py_None;
1393+
Py_RETURN_NONE;
13971394
}
13981395
}
13991396
#endif
@@ -1452,8 +1449,7 @@ BSTR_get(void *ptr, Py_ssize_t size)
14521449
/* Hm, it seems NULL pointer and zero length string are the
14531450
same in BSTR, see Don Box, p 81
14541451
*/
1455-
Py_INCREF(Py_None);
1456-
return Py_None;
1452+
Py_RETURN_NONE;
14571453
}
14581454
}
14591455
#endif
@@ -1493,8 +1489,7 @@ static PyObject *
14931489
P_get(void *ptr, Py_ssize_t size)
14941490
{
14951491
if (*(void **)ptr == NULL) {
1496-
Py_INCREF(Py_None);
1497-
return Py_None;
1492+
Py_RETURN_NONE;
14981493
}
14991494
return PyLong_FromVoidPtr(*(void **)ptr);
15001495
}

Modules/_curses_panel.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ static PyObject *
5959
PyCursesCheckERR(int code, const char *fname)
6060
{
6161
if (code != ERR) {
62-
Py_INCREF(Py_None);
63-
return Py_None;
62+
Py_RETURN_NONE;
6463
} else {
6564
if (fname == NULL) {
6665
PyErr_SetString(_curses_panelstate_global->PyCursesError, catchall_ERR);
@@ -177,8 +176,8 @@ static PyObject *PyCursesPanel_##X(PyCursesPanelObject *self) \
177176
#define Panel_NoArgTrueFalseFunction(X) \
178177
static PyObject *PyCursesPanel_##X(PyCursesPanelObject *self) \
179178
{ \
180-
if (X (self->pan) == FALSE) { Py_INCREF(Py_False); return Py_False; } \
181-
else { Py_INCREF(Py_True); return Py_True; } }
179+
if (X (self->pan) == FALSE) { Py_RETURN_FALSE; } \
180+
else { Py_RETURN_TRUE; } }
182181

183182
#define Panel_TwoArgNoReturnFunction(X, TYPE, PARSESTR) \
184183
static PyObject *PyCursesPanel_##X(PyCursesPanelObject *self, PyObject *args) \
@@ -245,8 +244,7 @@ PyCursesPanel_above(PyCursesPanelObject *self)
245244

246245
if (pan == NULL) { /* valid output, it means the calling panel
247246
is on top of the stack */
248-
Py_INCREF(Py_None);
249-
return Py_None;
247+
Py_RETURN_NONE;
250248
}
251249
po = find_po(pan);
252250
if (po == NULL) {
@@ -270,8 +268,7 @@ PyCursesPanel_below(PyCursesPanelObject *self)
270268

271269
if (pan == NULL) { /* valid output, it means the calling panel
272270
is on the bottom of the stack */
273-
Py_INCREF(Py_None);
274-
return Py_None;
271+
Py_RETURN_NONE;
275272
}
276273
po = find_po(pan);
277274
if (po == NULL) {
@@ -319,8 +316,7 @@ PyCursesPanel_replace_panel(PyCursesPanelObject *self, PyObject *args)
319316
}
320317
Py_INCREF(temp);
321318
Py_SETREF(po->wo, temp);
322-
Py_INCREF(Py_None);
323-
return Py_None;
319+
Py_RETURN_NONE;
324320
}
325321

326322
static PyObject *
@@ -406,8 +402,7 @@ PyCurses_bottom_panel(PyObject *self)
406402

407403
if (pan == NULL) { /* valid output, it means
408404
there's no panel at all */
409-
Py_INCREF(Py_None);
410-
return Py_None;
405+
Py_RETURN_NONE;
411406
}
412407
po = find_po(pan);
413408
if (po == NULL) {
@@ -452,8 +447,7 @@ PyCurses_top_panel(PyObject *self)
452447

453448
if (pan == NULL) { /* valid output, it means
454449
there's no panel at all */
455-
Py_INCREF(Py_None);
456-
return Py_None;
450+
Py_RETURN_NONE;
457451
}
458452
po = find_po(pan);
459453
if (po == NULL) {
@@ -469,8 +463,7 @@ static PyObject *PyCurses_update_panels(PyObject *self)
469463
{
470464
PyCursesInitialised;
471465
update_panels();
472-
Py_INCREF(Py_None);
473-
return Py_None;
466+
Py_RETURN_NONE;
474467
}
475468

476469

0 commit comments

Comments
 (0)