8000 gh-99300: Use Py_NewRef() in Python/ directory (#99317) · python/cpython@231d83b · GitHub
[go: up one dir, main page]

Skip to content

Commit 231d83b

Browse files
authored
gh-99300: Use Py_NewRef() in Python/ directory (#99317)
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in C files of the Python/ directory. Update Parser/asdl_c.py to regenerate Python/Python-ast.c.
1 parent d8f239d commit 231d83b

File tree

10 files changed

+169
-327
lines changed

10 files changed

+169
-327
lines changed

Parser/asdl_c.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -995,10 +995,11 @@ def visitModule(self, mod):
995995
996996
static PyObject* ast2obj_object(struct ast_state *Py_UNUSED(state), void *o)
997997
{
998-
if (!o)
999-
o = Py_None;
1000-
Py_INCREF((PyObject*)o);
1001-
return (PyObject*)o;
998+
PyObject *op = (PyObject*)o;
999+
if (!op) {
1000+
op = Py_None;
1001+
}
1002+
return Py_NewRef(op);
10021003
}
10031004
#define ast2obj_constant ast2obj_object
10041005
#define ast2obj_identifier ast2obj_object
@@ -1032,8 +1033,7 @@ def visitModule(self, mod):
10321033
*out = NULL;
10331034
return -1;
10341035
}
1035-
Py_INCREF(obj);
1036-
*out = obj;
1036+
*out = Py_NewRef(obj);
10371037
return 0;
10381038
}
10391039
@@ -1301,8 +1301,7 @@ def simpleSum(self, sum, name):
13011301
self.emit("switch(o) {", 1)
13021302
for t in sum.types:
13031303
self.emit("case %s:" % t.name, 2)
1304-
self.emit("Py_INCREF(state->%s_singleton);" % t.name, 3)
1305-
self.emit("return state->%s_singleton;" % t.name, 3)
1304+
self.emit("return Py_NewRef(state->%s_singleton);" % t.name, 3)
13061305
self.emit("}", 1)
13071306
self.emit("Py_UNREACHABLE();", 1);
13081307
self.emit("}", 0)

Python/Python-ast.c

Lines changed: 38 additions & 70 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/bltinmodule.c

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ update_bases(PyObject *bases, PyObject *const *args, Py_ssize_t nargs)
6363
}
6464
for (j = 0; j < i; j++) {
6565
base = args[j];
66-
PyList_SET_ITEM(new_bases, j, base);
67-
Py_INCREF(base);
66+
PyList_SET_ITEM(new_bases, j, Py_NewRef(base));
6867
}
6968
}
7069
j = PyList_GET_SIZE(new_bases);
@@ -170,8 +169,7 @@ builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs,
170169
}
171170
if (winner != meta) {
172171
Py_DECREF(meta);
173-
meta = winner;
174-
Py_INCREF(meta);
172+
meta = Py_NewRef(winner);
175173
}
176174
}
177175
/* else: meta is not a class, so we cannot do the metaclass
@@ -804,8 +802,7 @@ builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
804802
goto error;
805803
if (is_ast) {
806804
if (flags & PyCF_ONLY_AST) {
807-
Py_INCREF(source);
808-
result = source;
805+
result = Py_NewRef(source);
809806
}
810807
else {
811808
PyArena *arena;
@@ -1128,8 +1125,7 @@ builtin_getattr(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
11281125
if (nargs > 2) {
11291126
if (_PyObject_LookupAttr(v, name, &result) == 0) {
11301127
PyObject *dflt = args[2];
1131-
Py_INCREF(dflt);
1132-
return dflt;
1128+
return Py_NewRef(dflt);
11331129
}
11341130
}
11351131
else {
@@ -1162,8 +1158,7 @@ builtin_globals_impl(PyObject *module)
11621158
PyObject *d;
11631159

11641160
d = PyEval_GetGlobals();
1165-
Py_XINCREF(d);
1166-
return d;
1161+
return Py_XNewRef(d);
11671162
}
11681163

11691164

@@ -1390,12 +1385,10 @@ map_reduce(mapobject *lz, PyObject *Py_UNUSED(ignored))
13901385
Py_ssize_t i;
13911386
if (args == NULL)
13921387
return NULL;
1393-
Py_INCREF(lz->func);
1394-
PyTuple_SET_ITEM(args, 0, lz->func);
1388+
PyTuple_SET_ITEM(args, 0, Py_NewRef(lz->func));
13951389
for (i = 0; i<numargs; i++){
13961390
PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
1397-
Py_INCREF(it);
1398-
PyTuple_SET_ITEM(args, i+1, it);
1391+
PyTuple_SET_ITEM(args, i+1, Py_NewRef(it));
13991392
}
14001393

14011394
return Py_BuildValue("ON", Py_TYPE(lz), args);
@@ -1486,8 +1479,7 @@ builtin_next(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
14861479
return NULL;
14871480
PyErr_Clear();
14881481
}
1489-
Py_INCREF(def);
1490-
return def;
1482+
return Py_NewRef(def);
14911483
} else if (PyErr_Occurred()) {
14921484
return NULL;
14931485
} else {
@@ -1723,8 +1715,7 @@ builtin_locals_impl(PyObject *module)
17231715
PyObject *d;
17241716

17251717
d = PyEval_GetLocals();
1726-
Py_XINCREF(d);
1727-
return d;
1718+
return Py_XNewRef(d);
17281719
}
17291720

17301721

@@ -1785,8 +1776,7 @@ min_max(PyObject *args, PyObject *kwds, int op)
17851776
}
17861777
/* no key function; the value is the item */
17871778
else {
1788-
val = item;
1789-
Py_INCREF(val);
1779+
val = Py_NewRef(item);
17901780
}
17911781
17921782
/* maximum value and item are unset; set them */
@@ -1816,8 +1806,7 @@ min_max(PyObject *args, PyObject *kwds, int op)
18161806
if (maxval == NULL) {
18171807
assert(maxitem == NULL);
18181808
if (defaultval != NULL) {
1819-
Py_INCREF(defaultval);
1820-
maxitem = defaultval;
1809+
maxitem = Py_NewRef(defaultval);
18211810
} else {
18221811
PyErr_Format(PyExc_ValueError,
18231812
"%s() arg is an empty sequence", name);
@@ -2737,8 +2726,7 @@ zip_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
27372726
return NULL;
27382727
}
27392728
for (i=0 ; i < tuplesize ; i++) {
2740-
Py_INCREF(Py_None);
2741-
PyTuple_SET_ITEM(result, i, Py_None);
2729+
PyTuple_SET_ITEM(result, i, Py_NewRef(Py_None));
27422730
}
27432731

27442732
/* create zipobject structure */

0 commit comments

Comments
 (0)
0