Skip to content

Commit 1c9ee6d

Browse files
Remove unneeded reference counts in Cpython/Python
1 parent 1379d50 commit 1c9ee6d

17 files changed

+15
-47
lines changed

Python/_warnings.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ create_filter(PyObject *category, PyObject *action_str, const char *modname)
7878
return NULL;
7979
}
8080
} else {
81-
modname_obj = Py_NewRef(Py_None);
81+
modname_obj = Py_None;
8282
}
8383

8484
/* This assumes the line number is zero for now. */
@@ -383,7 +383,6 @@ get_filter(PyInterpreterState *interp, PyObject *category,
383383

384384
action = get_default_action(interp);
385385
if (action != NULL) {
386-
Py_INCREF(Py_None);
387386
*item = Py_None;
388387
return action;
389388
}
@@ -753,7 +752,6 @@ warn_explicit(PyThreadState *tstate, PyObject *category, PyObject *message,
753752

754753
return_none:
755754
result = Py_None;
756-
Py_INCREF(result);
757755

758756
cleanup:
759757
Py_XDECREF(item);
@@ -1013,7 +1011,6 @@ get_source_line(PyInterpreterState *interp, PyObject *module_globals, int lineno
10131011
return NULL;
10141012
}
10151013
if (source == Py_None) {
1016-
Py_DECREF(source);
10171014
return NULL;
10181015
}
10191016

Python/bltinmodule.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2686,7 +2686,6 @@ zip_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
26862686
return NULL;
26872687
}
26882688
for (i=0 ; i < tuplesize ; i++) {
2689-
Py_INCREF(Py_None);
26902689
PyTuple_SET_ITEM(result, i, Py_None);
26912690
}
26922691

Python/ceval.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,7 +1904,6 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
19041904
DISPATCH();
19051905
}
19061906
else if (err > 0) {
1907-
Py_INCREF(Py_False);
19081907
SET_TOP(Py_False);
19091908
DISPATCH();
19101909
}
@@ -6423,8 +6422,8 @@ exception_group_match(PyObject* exc_value, PyObject *match_type,
64236422
PyObject **match, PyObject **rest)
64246423
{
64256424
if (Py_IsNone(exc_value)) {
6426-
*match = Py_NewRef(Py_None);
6427-
*rest = Py_NewRef(Py_None);
6425+
*match = Py_None;
6426+
*rest = Py_None;
64286427
return 0;
64296428
}
64306429
assert(PyExceptionInstance_Check(exc_value));
@@ -6448,7 +6447,7 @@ exception_group_match(PyObject* exc_value, PyObject *match_type,
64486447
}
64496448
*match = wrapped;
64506449
}
6451-
*rest = Py_NewRef(Py_None);
6450+
*rest = Py_None;
64526451
return 0;
64536452
}
64546453

@@ -6469,8 +6468,8 @@ exception_group_match(PyObject* exc_value, PyObject *match_type,
64696468
return 0;
64706469
}
64716470
/* no match */
6472-
*match = Py_NewRef(Py_None);
6473-
*rest = Py_NewRef(Py_None);
6471+
*match = Py_None;
6472+
*rest = Py_None;
64746473
return 0;
64756474
}
64766475

Python/codecs.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ PyObject *_PyCodec_Lookup(const char *encoding)
178178
if (result == NULL)
179179
goto onError;
180180
if (result == Py_None) {
181-
Py_DECREF(result);
182181
continue;
183182
}
184183
if (!PyTuple_Check(result) || PyTuple_GET_SIZE(result) != 4) {

Python/compile.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,7 +1247,6 @@ merge_consts_recursive(struct compiler *c, PyObject *o)
12471247
// None and Ellipsis are singleton, and key is the singleton.
12481248
// No need to merge object and key.
12491249
if (o == Py_None || o == Py_Ellipsis) {
1250-
Py_INCREF(o);
12511250
return o;
12521251
}
12531252

@@ -6000,7 +5999,6 @@ compiler_error(struct compiler *c, const char *format, ...)
60005999
}
60016000
PyObject *loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
60026001
if (loc == NULL) {
6003-
Py_INCREF(Py_None);
60046002
loc = Py_None;
60056003
}
60066004
PyObject *args = Py_BuildValue("O(OiiOii)", msg, c->c_filename,

Python/errors.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,6 @@ _PyErr_NormalizeException(PyThreadState *tstate, PyObject **exc,
327327
*/
328328
if (!value) {
329329
value = Py_None;
330-
Py_INCREF(value);
331330
}
332331

333332
/* Normalize the exception so that if the type is a class, the

Python/import.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1399,7 +1399,6 @@ PyImport_ImportFrozenModuleObject(PyObject *name)
13991399
}
14001400
}
14011401
else {
1402-
Py_INCREF(Py_None);
14031402
origname = Py_None;
14041403
}
14051404
err = PyDict_SetItemString(d, "__origname__", origname);

Python/initconfig.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ _Py_GetGlobalVariablesAsDict(void)
202202
#define FROM_STRING(STR) \
203203
((STR != NULL) ? \
204204
PyUnicode_FromString(STR) \
205-
: (Py_INCREF(Py_None), Py_None))
205+
: (Py_None))
206206
#define SET_ITEM_STR(VAR) \
207207
SET_ITEM(#VAR, FROM_STRING(VAR))
208208

@@ -992,7 +992,7 @@ _PyConfig_AsDict(const PyConfig *config)
992992
#define FROM_WSTRING(STR) \
993993
((STR != NULL) ? \
994994
PyUnicode_FromWideChar(STR, -1) \
995-
: (Py_INCREF(Py_None), Py_None))
995+
: (Py_None))
996996
#define SET_ITEM_WSTR(ATTR) \
997997
SET_ITEM(#ATTR, FROM_WSTRING(config->ATTR))
998998
#define SET_ITEM_WSTRLIST(LIST) \

Python/marshal.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,27 +1006,22 @@ r_object(RFILE *p)
10061006
break;
10071007

10081008
case TYPE_NONE:
1009-
Py_INCREF(Py_None);
10101009
retval = Py_None;
10111010
break;
10121011

10131012
case TYPE_STOPITER:
1014-
Py_INCREF(PyExc_StopIteration);
10151013
retval = PyExc_StopIteration;
10161014
break;
10171015

10181016
case TYPE_ELLIPSIS:
1019-
Py_INCREF(Py_Ellipsis);
10201017
retval = Py_Ellipsis;
10211018
break;
10221019

10231020
case TYPE_FALSE:
1024-
Py_INCREF(Py_False);
10251021
retval = Py_False;
10261022
break;
10271023

10281024
case TYPE_TRUE:
1029-
Py_INCREF(Py_True);
10301025
retval = Py_True;
10311026
break;
10321027

Python/modsupport.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,6 @@ do_mkvalue(const char **p_format, va_list *p_va, int flags)
360360
n = -1;
361361
if (u == NULL) {
362362
v = Py_None;
363-
Py_INCREF(v);
364363
}
365364
else {
366365
if (n < 0)
@@ -411,7 +410,6 @@ do_mkvalue(const char **p_format, va_list *p_va, int flags)
411410
n = -1;
412411
if (str == NULL) {
413412
v = Py_None;
414-
Py_INCREF(v);
415413
}
416414
else {
417415
if (n < 0) {
@@ -447,7 +445,6 @@ do_mkvalue(const char **p_format, va_list *p_va, int flags)
447445
n = -1;
448446
if (str == NULL) {
449447
v = Py_None;
450-
Py_INCREF(v);
451448
}
452449
else {
453450
if (n < 0) {

0 commit comments

Comments
 (0)