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

+1-4
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

-1
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

+5-6
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

-1
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

-2
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

-1
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

-1
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

+2-2
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

-5
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

-3
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) {

Python/pylifecycle.c

-1
Original file line numberDiff line numberDiff line change
@@ -2520,7 +2520,6 @@ _Py_FatalError_PrintExc(PyThreadState *tstate)
25202520
_PyErr_NormalizeException(tstate, &exception, &v, &tb);
25212521
if (tb == NULL) {
25222522
tb = Py_None;
2523-
Py_INCREF(tb);
25242523
}
25252524
PyException_SetTraceback(v, tb);
25262525
if (exception == NULL) {

Python/pystate.c

-3
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,6 @@ PyState_RemoveModule(PyModuleDef* def)
958958
Py_FatalError("Module index out of bounds.");
959959
}
960960

961-
Py_INCREF(Py_None);
962961
return PyList_SetItem(interp->modules_by_index, index, Py_None);
963962
}
964963

@@ -2078,8 +2077,6 @@ _long_shared(PyObject *obj, _PyCrossInterpreterData *data)
20782077
static PyObject *
20792078
_new_none_object(_PyCrossInterpreterData *data)
20802079
{
2081-
// XXX Singleton refcounts are problematic across interpreters...
2082-
Py_INCREF(Py_None);
20832080
return Py_None;
20842081
}
20852082

Python/pythonrun.c

-8
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,6 @@ parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename,
514514
if (!v)
515515
goto finally;
516516
if (v == Py_None) {
517-
Py_DECREF(v);
518517
_Py_DECLARE_STR(anon_string, "<string>");
519518
*filename = &_Py_STR(anon_string);
520519
Py_INCREF(*filename);
@@ -537,7 +536,6 @@ parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename,
537536
goto finally;
538537
if (v == Py_None) {
539538
*offset = -1;
540-
Py_DECREF(v);
541539
} else {
542540
hold = PyLong_AsSsize_t(v);
543541
Py_DECREF(v);
@@ -554,7 +552,6 @@ parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename,
554552
}
555553
else if (v == Py_None) {
556554
*end_lineno = *lineno;
557-
Py_DECREF(v);
558555
} else {
559556
hold = PyLong_AsSsize_t(v);
560557
Py_DECREF(v);
@@ -570,7 +567,6 @@ parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename,
570567
}
571568
else if (v == Py_None) {
572569
*end_offset = -1;
573-
Py_DECREF(v);
574570
} else {
575571
hold = PyLong_AsSsize_t(v);
576572
Py_DECREF(v);
@@ -588,7 +584,6 @@ parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename,
588584
if (!v)
589585
goto finally;
590586
if (v == Py_None) {
591-
Py_DECREF(v);
592587
*text = NULL;
593588
}
594589
else {
@@ -788,7 +783,6 @@ _PyErr_PrintEx(PyThreadState *tstate, int set_sys_last_vars)
788783
_PyErr_NormalizeException(tstate, &exception, &v, &tb);
789784
if (tb == NULL) {
790785
tb = Py_None;
791-
Py_INCREF(tb);
792786
}
793787
PyException_SetTraceback(v, tb);
794788
if (exception == NULL) {
@@ -835,11 +829,9 @@ _PyErr_PrintEx(PyThreadState *tstate, int set_sys_last_vars)
835829
tolerate NULLs, so just be safe. */
836830
if (exception2 == NULL) {
837831
exception2 = Py_None;
838-
Py_INCREF(exception2);
839832
}
840833
if (v2 == NULL) {
841834
v2 = Py_None;
842-
Py_INCREF(v2);
843835
}
844836
fflush(stdout);
845837
PySys_WriteStderr("Error in sys.excepthook:\n");

Python/structmember.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ PyMember_GetOne(const char *obj_addr, PyMemberDef *l)
4949
break;
5050
case T_STRING:
5151
if (*(char**)addr == NULL) {
52-
Py_INCREF(Py_None);
5352
v = Py_None;
5453
}
5554
else
@@ -63,9 +62,11 @@ PyMember_GetOne(const char *obj_addr, PyMemberDef *l)
6362
break;
6463
case T_OBJECT:
6564
v = *(PyObject **)addr;
66-
if (v == NULL)
65+
if (v) {
66+
Py_INCREF(v);
67+
} else {
6768
v = Py_None;
68-
Py_INCREF(v);
69+
}
6970
break;
7071
case T_OBJECT_EX:
7172
v = *(PyObject **)addr;
@@ -86,7 +87,6 @@ PyMember_GetOne(const char *obj_addr, PyMemberDef *l)
8687
break;
8788
case T_NONE:
8889
v = Py_None;
89-
Py_INCREF(v);
9090
break;
9191
default:
9292
PyErr_SetString(PyExc_SystemError, "bad memberdescr type");

Python/sysmodule.c

-1
Original file line numberDiff line numberDiff line change
@@ -2340,7 +2340,6 @@ _PySys_AddXOptionWithError(const wchar_t *s)
23402340
if (!name_end) {
23412341
name = PyUnicode_FromWideChar(s, -1);
23422342
value = Py_True;
2343-
Py_INCREF(value);
23442343
}
23452344
else {
23462345
name = PyUnicode_FromWideChar(s, name_end - s);

Python/thread.c

-2
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ PyThread_GetInfo(void)
219219
return NULL;
220220
}
221221
#else
222-
Py_INCREF(Py_None);
223222
value = Py_None;
224223
#endif
225224
PyStructSequence_SET_ITEM(threadinfo, pos++, value);
@@ -236,7 +235,6 @@ PyThread_GetInfo(void)
236235
if (value == NULL)
237236
#endif
238237
{
239-
Py_INCREF(Py_None);
240238
value = Py_None;
241239
}
242240
PyStructSequence_SET_ITEM(threadinfo, pos++, value);

Python/traceback.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,11 @@ static PyObject *
103103
tb_next_get(PyTracebackObject *self, void *Py_UNUSED(_))
104104
{
105105
PyObject* ret = (PyObject*)self->tb_next;
106-
if (!ret) {
106+
if (ret) {
107+
Py_INCREF(ret);
108+
} else {
107109
ret = Py_None;
108110
}
109-
Py_INCREF(ret);
110111
return ret;
111112
}
112113

0 commit comments

Comments
 (0)