gh-99300: Use Py_NewRef() in Modules/ directory (#99467)

Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in test C files of the Modules/ directory.
This commit is contained in:
Victor Stinner 2022-11-14 13:08:43 +01:00 committed by GitHub
parent 3817607b8a
commit 7e4dec02ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 75 additions and 144 deletions

View file

@ -386,10 +386,9 @@ init_method_ref(PyObject *self, PyObject *name,
if (PyMethod_Check(func) && PyMethod_GET_SELF(func) == self) {
/* Deconstruct a bound Python method */
func2 = PyMethod_GET_FUNCTION(func);
Py_INCREF(func2);
*method_self = self; /* borrowed */
Py_XSETREF(*method_func, func2);
func2 = PyMethod_GET_FUNCTION(func);
Py_XSETREF(*method_func, Py_NewRef(func2));
Py_DECREF(func);
return 0;
}
@ -408,8 +407,7 @@ reconstruct_method(PyObject *func, PyObject *self)
return PyMethod_New(func, self);
}
else {
Py_INCREF(func);
return func;
return Py_NewRef(func);
}
}
@ -907,8 +905,7 @@ PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
entry->me_value = value;
return 0;
}
Py_INCREF(key);
entry->me_key = key;
entry->me_key = Py_NewRef(key);
entry->me_value = value;
self->mt_used++;
@ -1196,8 +1193,7 @@ _Pickler_SetBufferCallback(PicklerObject *self, PyObject *buffer_callback)
return -1;
}
Py_XINCREF(buffer_callback);
self->buffer_callback = buffer_callback;
self->buffer_callback = Py_XNewRef(buffer_callback);
return 0;
}
@ -1543,9 +1539,8 @@ _Unpickler_MemoPut(UnpicklerObject *self, size_t idx, PyObject *value)
return -1;
assert(idx < self->memo_size);
}
Py_INCREF(value);
old_item = self->memo[idx];
self->memo[idx] = value;
self->memo[idx] = Py_NewRef(value);
if (old_item != NULL) {
Py_DECREF(old_item);
}
@ -1928,8 +1923,7 @@ whichmodule(PyObject *global, PyObject *dotted_path)
i = 0;
while (PyDict_Next(modules, &i, &module_name, &module)) {
if (_checkmodule(module_name, module, global, dotted_path) == 0) {
Py_INCREF(module_name);
return module_name;
return Py_NewRef(module_name);
}
if (PyErr_Occurred()) {
return NULL;
@ -1965,8 +1959,7 @@ whichmodule(PyObject *global, PyObject *dotted_path)
/* If no module is found, use __main__. */
module_name = &_Py_ID(__main__);
Py_INCREF(module_name);
return module_name;
return Py_NewRef(module_name);
}
/* fast_save_enter() and fast_save_leave() are guards against recursive
@ -3557,10 +3550,8 @@ fix_imports(PyObject **module_name, PyObject **global_name)
Py_CLEAR(*module_name);
Py_CLEAR(*global_name);
Py_INCREF(fixed_module_name);
Py_INCREF(fixed_global_name);
*module_name = fixed_module_name;
*global_name = fixed_global_name;
*module_name = Py_NewRef(fixed_module_name);
*global_name = Py_NewRef(fixed_global_name);
return 0;
}
else if (PyErr_Occurred()) {
@ -3576,8 +3567,7 @@ fix_imports(PyObject **module_name, PyObject **global_name)
Py_TYPE(item)->tp_name);
return -1;
}
Py_INCREF(item);
Py_XSETREF(*module_name, item);
Py_XSETREF(*module_name, Py_NewRef(item));
}
else if (PyErr_Occurred()) {
return -1;
@ -3602,8 +3592,7 @@ save_global(PicklerObject *self, PyObject *obj, PyObject *name)
const char global_op = GLOBAL;
if (name) {
Py_INCREF(name);
global_name = name;
global_name = Py_NewRef(name);
}
else {
if (_PyObject_LookupAttr(obj, &_Py_ID(__qualname__), &global_name) < 0)
@ -3637,8 +3626,8 @@ save_global(PicklerObject *self, PyObject *obj, PyObject *name)
obj, module_name);
goto error;
}
lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
Py_INCREF(lastname);
lastname = Py_NewRef(PyList_GET_ITEM(dotted_path,
PyList_GET_SIZE(dotted_path) - 1));
cls = get_deep_attribute(module, dotted_path, &parent);
Py_CLEAR(dotted_path);
if (cls == NULL) {
@ -3932,8 +3921,7 @@ get_class(PyObject *obj)
PyObject *cls;
if (_PyObject_LookupAttr(obj, &_Py_ID(__class__), &cls) == 0) {
cls = (PyObject *) Py_TYPE(obj);
Py_INCREF(cls);
cls = Py_NewRef(Py_TYPE(obj));
}
return cls;
}
@ -4084,12 +4072,10 @@ save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
return -1;
}
PyTuple_SET_ITEM(newargs, 0, cls_new);
Py_INCREF(cls);
PyTuple_SET_ITEM(newargs, 1, cls);
PyTuple_SET_ITEM(newargs, 1, Py_NewRef(cls));
for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
PyObject *item = PyTuple_GET_ITEM(args, i);
Py_INCREF(item);
PyTuple_SET_ITEM(newargs, i + 2, item);
PyTuple_SET_ITEM(newargs, i + 2, Py_NewRef(item));
}
callable = PyObject_Call(st->partial, newargs, kwargs);
@ -4405,8 +4391,7 @@ save(PicklerObject *self, PyObject *obj, int pers_save)
}
}
if (reduce_func != NULL) {
Py_INCREF(obj);
reduce_value = _Pickle_FastCall(reduce_func, obj);
reduce_value = _Pickle_FastCall(reduce_func, Py_NewRef(obj));
}
else if (PyType_IsSubtype(type, &PyType_Type)) {
status = save_global(self, obj, NULL);
@ -4869,8 +4854,7 @@ _pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
return NULL;
}
PyTuple_SET_ITEM(dict_args, 0, contents);
Py_INCREF((PyObject *)&PyDict_Type);
PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
PyTuple_SET_ITEM(reduce_value, 0, Py_NewRef(&PyDict_Type));
PyTuple_SET_ITEM(reduce_value, 1, dict_args);
return reduce_value;
}
@ -4944,8 +4928,7 @@ PicklerMemoProxy_New(PicklerObject *pickler)
self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
if (self == NULL)
return NULL;
Py_INCREF(pickler);
self->pickler = pickler;
self->pickler = (PicklerObject*)Py_NewRef(pickler);
PyObject_GC_Track(self);
return (PyObject *)self;
}
@ -5045,8 +5028,7 @@ Pickler_set_persid(PicklerObject *self, PyObject *value, void *Py_UNUSED(ignored
}
self->pers_func_self = NULL;
Py_INCREF(value);
Py_XSETREF(self->pers_func, value);
Py_XSETREF(self->pers_func, Py_NewRef(value));
return 0;
}
@ -7370,8 +7352,7 @@ _pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
return NULL;
}
PyTuple_SET_ITEM(constructor_args, 0, contents);
Py_INCREF((PyObject *)&PyDict_Type);
PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
PyTuple_SET_ITEM(reduce_value, 0, Py_NewRef(&PyDict_Type));
PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
return reduce_value;
}
@ -7446,8 +7427,7 @@ UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
&UnpicklerMemoProxyType);
if (self == NULL)
return NULL;
Py_INCREF(unpickler);
self->unpickler = unpickler;
self->unpickler = (UnpicklerObject*)Py_NewRef(unpickler);
PyObject_GC_Track(self);
return (PyObject *)self;
}
@ -7483,8 +7463,7 @@ Unpickler_set_memo(UnpicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored
return -1;
for (size_t i = 0; i < new_memo_size; i++) {
Py_XINCREF(unpickler->memo[i]);
new_memo[i] = unpickler->memo[i];
new_memo[i] = Py_XNewRef(unpickler->memo[i]);
}
}
else if (PyDict_Check(obj)) {
@ -7564,8 +7543,7 @@ Unpickler_set_persload(UnpicklerObject *self, PyObject *value, void *Py_UNUSED(i
}
self->pers_func_self = NULL;
Py_INCREF(value);
Py_XSETREF(self->pers_func, value);
Py_XSETREF(self->pers_func, Py_NewRef(value));
return 0;
}
@ -7944,8 +7922,7 @@ PyInit__pickle(void)
m = PyState_FindModule(&_picklemodule);
if (m) {
Py_INCREF(m);
return m;
return Py_NewRef(m);
}
if (PyType_Ready(&Pdata_Type) < 0)