Skip to content

Commit

Permalink
gh-99300: Use Py_NewRef() in Python/ directory (#99317)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vstinner authored Nov 10, 2022
1 parent d8f239d commit 231d83b
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 327 deletions.
15 changes: 7 additions & 8 deletions Parser/asdl_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -995,10 +995,11 @@ def visitModule(self, mod):
static PyObject* ast2obj_object(struct ast_state *Py_UNUSED(state), void *o)
{
if (!o)
o = Py_None;
Py_INCREF((PyObject*)o);
return (PyObject*)o;
PyObject *op = (PyObject*)o;
if (!op) {
op = Py_None;
}
return Py_NewRef(op);
}
#define ast2obj_constant ast2obj_object
#define ast2obj_identifier ast2obj_object
Expand Down Expand Up @@ -1032,8 +1033,7 @@ def visitModule(self, mod):
*out = NULL;
return -1;
}
Py_INCREF(obj);
*out = obj;
*out = Py_NewRef(obj);
return 0;
}
Expand Down Expand Up @@ -1301,8 +1301,7 @@ def simpleSum(self, sum, name):
self.emit("switch(o) {", 1)
for t in sum.types:
self.emit("case %s:" % t.name, 2)
self.emit("Py_INCREF(state->%s_singleton);" % t.name, 3)
self.emit("return state->%s_singleton;" % t.name, 3)
self.emit("return Py_NewRef(state->%s_singleton);" % t.name, 3)
self.emit("}", 1)
self.emit("Py_UNREACHABLE();", 1);
self.emit("}", 0)
Expand Down
108 changes: 38 additions & 70 deletions Python/Python-ast.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 12 additions & 24 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ update_bases(PyObject *bases, PyObject *const *args, Py_ssize_t nargs)
}
for (j = 0; j < i; j++) {
base = args[j];
PyList_SET_ITEM(new_bases, j, base);
Py_INCREF(base);
PyList_SET_ITEM(new_bases, j, Py_NewRef(base));
}
}
j = PyList_GET_SIZE(new_bases);
Expand Down Expand Up @@ -170,8 +169,7 @@ builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs,
}
if (winner != meta) {
Py_DECREF(meta);
meta = winner;
Py_INCREF(meta);
meta = Py_NewRef(winner);
}
}
/* else: meta is not a class, so we cannot do the metaclass
Expand Down Expand Up @@ -804,8 +802,7 @@ builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
goto error;
if (is_ast) {
if (flags & PyCF_ONLY_AST) {
Py_INCREF(source);
result = source;
result = Py_NewRef(source);
}
else {
PyArena *arena;
Expand Down Expand Up @@ -1128,8 +1125,7 @@ builtin_getattr(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
if (nargs > 2) {
if (_PyObject_LookupAttr(v, name, &result) == 0) {
PyObject *dflt = args[2];
Py_INCREF(dflt);
return dflt;
return Py_NewRef(dflt);
}
}
else {
Expand Down Expand Up @@ -1162,8 +1158,7 @@ builtin_globals_impl(PyObject *module)
PyObject *d;

d = PyEval_GetGlobals();
Py_XINCREF(d);
return d;
return Py_XNewRef(d);
}


Expand Down Expand Up @@ -1390,12 +1385,10 @@ map_reduce(mapobject *lz, PyObject *Py_UNUSED(ignored))
Py_ssize_t i;
if (args == NULL)
return NULL;
Py_INCREF(lz->func);
PyTuple_SET_ITEM(args, 0, lz->func);
PyTuple_SET_ITEM(args, 0, Py_NewRef(lz->func));
for (i = 0; i<numargs; i++){
PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
Py_INCREF(it);
PyTuple_SET_ITEM(args, i+1, it);
PyTuple_SET_ITEM(args, i+1, Py_NewRef(it));
}

return Py_BuildValue("ON", Py_TYPE(lz), args);
Expand Down Expand Up @@ -1486,8 +1479,7 @@ builtin_next(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
return NULL;
PyErr_Clear();
}
Py_INCREF(def);
return def;
return Py_NewRef(def);
} else if (PyErr_Occurred()) {
return NULL;
} else {
Expand Down Expand Up @@ -1723,8 +1715,7 @@ builtin_locals_impl(PyObject *module)
PyObject *d;

d = PyEval_GetLocals();
Py_XINCREF(d);
return d;
return Py_XNewRef(d);
}


Expand Down Expand Up @@ -1785,8 +1776,7 @@ min_max(PyObject *args, PyObject *kwds, int op)
}
/* no key function; the value is the item */
else {
val = item;
Py_INCREF(val);
val = Py_NewRef(item);
}

/* maximum value and item are unset; set them */
Expand Down Expand Up @@ -1816,8 +1806,7 @@ min_max(PyObject *args, PyObject *kwds, int op)
if (maxval == NULL) {
assert(maxitem == NULL);
if (defaultval != NULL) {
Py_INCREF(defaultval);
maxitem = defaultval;
maxitem = Py_NewRef(defaultval);
} else {
PyErr_Format(PyExc_ValueError,
"%s() arg is an empty sequence", name);
Expand Down Expand Up @@ -2737,8 +2726,7 @@ zip_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL;
}
for (i=0 ; i < tuplesize ; i++) {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(result, i, Py_None);
PyTuple_SET_ITEM(result, i, Py_NewRef(Py_None));
}

/* create zipobject structure */
Expand Down
Loading

0 comments on commit 231d83b

Please sign in to comment.