mirror of
https://github.com/python/cpython.git
synced 2025-08-03 08:34:29 +00:00
Issue #20440: Massive replacing unsafe attribute setting code with special
macro Py_SETREF.
This commit is contained in:
commit
f006940351
28 changed files with 92 additions and 120 deletions
|
@ -276,9 +276,8 @@ _set_str(const char *name, PyObject **target, PyObject *src, const char *dflt)
|
|||
else {
|
||||
if (PyUnicode_READY(src) == -1)
|
||||
return -1;
|
||||
Py_XDECREF(*target);
|
||||
Py_INCREF(src);
|
||||
*target = src;
|
||||
Py_SETREF(*target, src);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
@ -784,8 +783,7 @@ parse_process_char(ReaderObj *self, Py_UCS4 c)
|
|||
static int
|
||||
parse_reset(ReaderObj *self)
|
||||
{
|
||||
Py_XDECREF(self->fields);
|
||||
self->fields = PyList_New(0);
|
||||
Py_SETREF(self->fields, PyList_New(0));
|
||||
if (self->fields == NULL)
|
||||
return -1;
|
||||
self->field_len = 0;
|
||||
|
|
|
@ -391,8 +391,7 @@ StructUnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds, int isSt
|
|||
Py_DECREF((PyObject *)dict);
|
||||
return NULL;
|
||||
}
|
||||
Py_DECREF(result->tp_dict);
|
||||
result->tp_dict = (PyObject *)dict;
|
||||
Py_SETREF(result->tp_dict, (PyObject *)dict);
|
||||
dict->format = _ctypes_alloc_format_string(NULL, "B");
|
||||
if (dict->format == NULL) {
|
||||
Py_DECREF(result);
|
||||
|
@ -871,8 +870,7 @@ PyCPointerType_SetProto(StgDictObject *stgdict, PyObject *proto)
|
|||
return -1;
|
||||
}
|
||||
Py_INCREF(proto);
|
||||
Py_XDECREF(stgdict->proto);
|
||||
stgdict->proto = proto;
|
||||
Py_SETREF(stgdict->proto, proto);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -962,8 +960,7 @@ PyCPointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|||
Py_DECREF((PyObject *)stgdict);
|
||||
return NULL;
|
||||
}
|
||||
Py_DECREF(result->tp_dict);
|
||||
result->tp_dict = (PyObject *)stgdict;
|
||||
Py_SETREF(result->tp_dict, (PyObject *)stgdict);
|
||||
|
||||
return (PyObject *)result;
|
||||
}
|
||||
|
@ -1406,8 +1403,7 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|||
/* replace the class dict by our updated spam dict */
|
||||
if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict))
|
||||
goto error;
|
||||
Py_DECREF(result->tp_dict);
|
||||
result->tp_dict = (PyObject *)stgdict; /* steal the reference */
|
||||
Py_SETREF(result->tp_dict, (PyObject *)stgdict); /* steal the reference */
|
||||
stgdict = NULL;
|
||||
|
||||
/* Special case for character arrays.
|
||||
|
@ -1820,8 +1816,7 @@ static PyObject *CreateSwappedType(PyTypeObject *type, PyObject *args, PyObject
|
|||
Py_DECREF((PyObject *)stgdict);
|
||||
return NULL;
|
||||
}
|
||||
Py_DECREF(result->tp_dict);
|
||||
result->tp_dict = (PyObject *)stgdict;
|
||||
Py_SETREF(result->tp_dict, (PyObject *)stgdict);
|
||||
|
||||
return (PyObject *)result;
|
||||
}
|
||||
|
@ -1949,8 +1944,7 @@ PyCSimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|||
Py_DECREF((PyObject *)stgdict);
|
||||
return NULL;
|
||||
}
|
||||
Py_DECREF(result->tp_dict);
|
||||
result->tp_dict = (PyObject *)stgdict;
|
||||
Py_SETREF(result->tp_dict, (PyObject *)stgdict);
|
||||
|
||||
/* Install from_param class methods in ctypes base classes.
|
||||
Overrides the PyCSimpleType_from_param generic method.
|
||||
|
@ -2313,8 +2307,7 @@ PyCFuncPtrType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|||
Py_DECREF((PyObject *)stgdict);
|
||||
return NULL;
|
||||
}
|
||||
Py_DECREF(result->tp_dict);
|
||||
result->tp_dict = (PyObject *)stgdict;
|
||||
Py_SETREF(result->tp_dict, (PyObject *)stgdict);
|
||||
|
||||
if (-1 == make_funcptrtype_dict(stgdict)) {
|
||||
Py_DECREF(result);
|
||||
|
@ -2458,8 +2451,7 @@ KeepRef(CDataObject *target, Py_ssize_t index, PyObject *keep)
|
|||
return -1;
|
||||
}
|
||||
if (ob->b_objects == NULL || !PyDict_CheckExact(ob->b_objects)) {
|
||||
Py_XDECREF(ob->b_objects);
|
||||
ob->b_objects = keep; /* refcount consumed */
|
||||
Py_SETREF(ob->b_objects, keep); /* refcount consumed */
|
||||
return 0;
|
||||
}
|
||||
key = unique_key(target, index);
|
||||
|
@ -2962,9 +2954,8 @@ PyCFuncPtr_set_errcheck(PyCFuncPtrObject *self, PyObject *ob)
|
|||
"the errcheck attribute must be callable");
|
||||
return -1;
|
||||
}
|
||||
Py_XDECREF(self->errcheck);
|
||||
Py_XINCREF(ob);
|
||||
self->errcheck = ob;
|
||||
Py_SETREF(self->errcheck, ob);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -2993,9 +2984,8 @@ PyCFuncPtr_set_restype(PyCFuncPtrObject *self, PyObject *ob)
|
|||
return -1;
|
||||
}
|
||||
Py_XDECREF(self->checker);
|
||||
Py_XDECREF(self->restype);
|
||||
Py_INCREF(ob);
|
||||
self->restype = ob;
|
||||
Py_SETREF(self->restype, ob);
|
||||
self->checker = PyObject_GetAttrString(ob, "_check_retval_");
|
||||
if (self->checker == NULL)
|
||||
PyErr_Clear();
|
||||
|
@ -3033,11 +3023,9 @@ PyCFuncPtr_set_argtypes(PyCFuncPtrObject *self, PyObject *ob)
|
|||
converters = converters_from_argtypes(ob);
|
||||
if (!converters)
|
||||
return -1;
|
||||
Py_XDECREF(self->converters);
|
||||
self->converters = converters;
|
||||
Py_XDECREF(self->argtypes);
|
||||
Py_SETREF(self->converters, converters);
|
||||
Py_INCREF(ob);
|
||||
self->argtypes = ob;
|
||||
Py_SETREF(self->argtypes, ob);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@ -5164,9 +5152,8 @@ comerror_init(PyObject *self, PyObject *args, PyObject *kwds)
|
|||
return -1;
|
||||
|
||||
bself = (PyBaseExceptionObject *)self;
|
||||
Py_DECREF(bself->args);
|
||||
bself->args = args;
|
||||
Py_INCREF(bself->args);
|
||||
Py_INCREF(args);
|
||||
Py_SETREF(bself->args, args);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -312,9 +312,8 @@ PyCursesPanel_replace_panel(PyCursesPanelObject *self, PyObject *args)
|
|||
PyErr_SetString(_curses_panelstate_global->PyCursesError, "replace_panel() returned ERR");
|
||||
return NULL;
|
||||
}
|
||||
Py_DECREF(po->wo);
|
||||
po->wo = temp;
|
||||
Py_INCREF(po->wo);
|
||||
Py_INCREF(temp);
|
||||
Py_SETREF(po->wo, temp);
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
|
|
@ -969,8 +969,7 @@ _io_BytesIO___init___impl(bytesio *self, PyObject *initvalue)
|
|||
if (initvalue && initvalue != Py_None) {
|
||||
if (PyBytes_CheckExact(initvalue)) {
|
||||
Py_INCREF(initvalue);
|
||||
Py_XDECREF(self->buf);
|
||||
self->buf = initvalue;
|
||||
Py_SETREF(self->buf, initvalue);
|
||||
self->string_size = PyBytes_GET_SIZE(initvalue);
|
||||
}
|
||||
else {
|
||||
|
|
|
@ -204,8 +204,8 @@ void pysqlite_flush_statement_cache(pysqlite_Connection* self)
|
|||
node = node->next;
|
||||
}
|
||||
|
||||
Py_DECREF(self->statement_cache);
|
||||
self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "O", self);
|
||||
Py_SETREF(self->statement_cache,
|
||||
(pysqlite_Cache *)PyObject_CallFunction((PyObject *)&pysqlite_CacheType, "O", self));
|
||||
Py_DECREF(self);
|
||||
self->statement_cache->decref_factory = 0;
|
||||
}
|
||||
|
@ -318,9 +318,8 @@ PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args,
|
|||
_pysqlite_drop_unused_cursor_references(self);
|
||||
|
||||
if (cursor && self->row_factory != Py_None) {
|
||||
Py_XDECREF(((pysqlite_Cursor*)cursor)->row_factory);
|
||||
Py_INCREF(self->row_factory);
|
||||
((pysqlite_Cursor*)cursor)->row_factory = self->row_factory;
|
||||
Py_SETREF(((pysqlite_Cursor *)cursor)->row_factory, self->row_factory);
|
||||
}
|
||||
|
||||
return cursor;
|
||||
|
@ -795,8 +794,7 @@ static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self
|
|||
}
|
||||
}
|
||||
|
||||
Py_DECREF(self->statements);
|
||||
self->statements = new_list;
|
||||
Py_SETREF(self->statements, new_list);
|
||||
}
|
||||
|
||||
static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
|
||||
|
@ -827,8 +825,7 @@ static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
|
|||
}
|
||||
}
|
||||
|
||||
Py_DECREF(self->cursors);
|
||||
self->cursors = new_list;
|
||||
Py_SETREF(self->cursors, new_list);
|
||||
}
|
||||
|
||||
PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
|
||||
|
|
|
@ -170,8 +170,7 @@ int pysqlite_build_row_cast_map(pysqlite_Cursor* self)
|
|||
return 0;
|
||||
}
|
||||
|
||||
Py_XDECREF(self->row_cast_map);
|
||||
self->row_cast_map = PyList_New(0);
|
||||
Py_SETREF(self->row_cast_map, PyList_New(0));
|
||||
|
||||
for (i = 0; i < sqlite3_column_count(self->statement->st); i++) {
|
||||
converter = NULL;
|
||||
|
@ -510,9 +509,8 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
|
|||
goto error;
|
||||
|
||||
/* reset description and rowcount */
|
||||
Py_DECREF(self->description);
|
||||
Py_INCREF(Py_None);
|
||||
self->description = Py_None;
|
||||
Py_SETREF(self->description, Py_None);
|
||||
self->rowcount = -1L;
|
||||
|
||||
func_args = PyTuple_New(1);
|
||||
|
@ -537,8 +535,8 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
|
|||
}
|
||||
|
||||
if (self->statement->in_use) {
|
||||
Py_DECREF(self->statement);
|
||||
self->statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
|
||||
Py_SETREF(self->statement,
|
||||
PyObject_New(pysqlite_Statement, &pysqlite_StatementType));
|
||||
if (!self->statement) {
|
||||
goto error;
|
||||
}
|
||||
|
@ -654,8 +652,7 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
|
|||
numcols = sqlite3_column_count(self->statement->st);
|
||||
Py_END_ALLOW_THREADS
|
||||
|
||||
Py_DECREF(self->description);
|
||||
self->description = PyTuple_New(numcols);
|
||||
Py_SETREF(self->description, PyTuple_New(numcols));
|
||||
if (!self->description) {
|
||||
goto error;
|
||||
}
|
||||
|
|
|
@ -753,8 +753,7 @@ deepcopy(PyObject** object, PyObject* memo)
|
|||
if (!copy)
|
||||
return 0;
|
||||
|
||||
Py_DECREF(*object);
|
||||
*object = copy;
|
||||
Py_SETREF(*object, copy);
|
||||
|
||||
return 1; /* success */
|
||||
}
|
||||
|
|
|
@ -1589,8 +1589,7 @@ static int PySSL_set_context(PySSLSocket *self, PyObject *value,
|
|||
return -1;
|
||||
#else
|
||||
Py_INCREF(value);
|
||||
Py_DECREF(self->ctx);
|
||||
self->ctx = (PySSLContext *) value;
|
||||
Py_SETREF(self->ctx, (PySSLContext *)value);
|
||||
SSL_set_SSL_CTX(self->ssl, self->ctx->ctx);
|
||||
#endif
|
||||
} else {
|
||||
|
@ -1647,8 +1646,7 @@ PySSL_get_owner(PySSLSocket *self, void *c)
|
|||
static int
|
||||
PySSL_set_owner(PySSLSocket *self, PyObject *value, void *c)
|
||||
{
|
||||
Py_XDECREF(self->owner);
|
||||
self->owner = PyWeakref_NewRef(value, NULL);
|
||||
Py_SETREF(self->owner, PyWeakref_NewRef(value, NULL));
|
||||
if (self->owner == NULL)
|
||||
return -1;
|
||||
return 0;
|
||||
|
|
|
@ -380,9 +380,8 @@ faulthandler_enable(PyObject *self, PyObject *args, PyObject *kwargs)
|
|||
if (tstate == NULL)
|
||||
return NULL;
|
||||
|
||||
Py_XDECREF(fatal_error.file);
|
||||
Py_XINCREF(file);
|
||||
fatal_error.file = file;
|
||||
Py_SETREF(fatal_error.file, file);
|
||||
fatal_error.fd = fd;
|
||||
fatal_error.all_threads = all_threads;
|
||||
fatal_error.interp = tstate->interp;
|
||||
|
@ -599,9 +598,8 @@ faulthandler_dump_traceback_later(PyObject *self,
|
|||
/* Cancel previous thread, if running */
|
||||
cancel_dump_traceback_later();
|
||||
|
||||
Py_XDECREF(thread.file);
|
||||
Py_XINCREF(file);
|
||||
thread.file = file;
|
||||
Py_SETREF(thread.file, file);
|
||||
thread.fd = fd;
|
||||
thread.timeout_us = timeout_us;
|
||||
thread.repeat = repeat;
|
||||
|
@ -778,9 +776,8 @@ faulthandler_register_py(PyObject *self,
|
|||
user->previous = previous;
|
||||
}
|
||||
|
||||
Py_XDECREF(user->file);
|
||||
Py_XINCREF(file);
|
||||
user->file = file;
|
||||
Py_SETREF(user->file, file);
|
||||
user->fd = fd;
|
||||
user->all_threads = all_threads;
|
||||
user->chain = chain;
|
||||
|
|
|
@ -634,8 +634,7 @@ tee_next(teeobject *to)
|
|||
link = teedataobject_jumplink(to->dataobj);
|
||||
if (link == NULL)
|
||||
return NULL;
|
||||
Py_DECREF(to->dataobj);
|
||||
to->dataobj = (teedataobject *)link;
|
||||
Py_SETREF(to->dataobj, (teedataobject *)link);
|
||||
to->index = 0;
|
||||
}
|
||||
value = teedataobject_getitem(to->dataobj, to->index);
|
||||
|
|
|
@ -1266,8 +1266,7 @@ PyInit__signal(void)
|
|||
if (Handlers[SIGINT].func == DefaultHandler) {
|
||||
/* Install default int handler */
|
||||
Py_INCREF(IntHandler);
|
||||
Py_DECREF(Handlers[SIGINT].func);
|
||||
Handlers[SIGINT].func = IntHandler;
|
||||
Py_SETREF(Handlers[SIGINT].func, IntHandler);
|
||||
old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
|
||||
}
|
||||
|
||||
|
|
|
@ -155,8 +155,7 @@ zipimporter_init(ZipImporter *self, PyObject *args, PyObject *kwds)
|
|||
tmp = PyUnicode_FromFormat("%U%c", self->prefix, SEP);
|
||||
if (tmp == NULL)
|
||||
goto error;
|
||||
Py_DECREF(self->prefix);
|
||||
self->prefix = tmp;
|
||||
Py_SETREF(self->prefix, tmp);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
|
|
@ -667,8 +667,7 @@ save_unconsumed_input(compobject *self, int err)
|
|||
PyBytes_AS_STRING(self->unused_data), old_size);
|
||||
Py_MEMCPY(PyBytes_AS_STRING(new_data) + old_size,
|
||||
self->zst.next_in, self->zst.avail_in);
|
||||
Py_DECREF(self->unused_data);
|
||||
self->unused_data = new_data;
|
||||
Py_SETREF(self->unused_data, new_data);
|
||||
self->zst.avail_in = 0;
|
||||
}
|
||||
}
|
||||
|
@ -680,8 +679,7 @@ save_unconsumed_input(compobject *self, int err)
|
|||
(char *)self->zst.next_in, self->zst.avail_in);
|
||||
if (new_data == NULL)
|
||||
return -1;
|
||||
Py_DECREF(self->unconsumed_tail);
|
||||
self->unconsumed_tail = new_data;
|
||||
Py_SETREF(self->unconsumed_tail, new_data);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue