bpo-1635741: sqlite3 uses Py_NewRef/Py_XNewRef (GH-23170)

This commit is contained in:
Erlend Egeberg Aasland 2020-12-27 12:05:33 +01:00 committed by GitHub
parent 3ccef1ca47
commit bf64d9064a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 45 additions and 82 deletions

View File

@ -34,11 +34,8 @@ pysqlite_Node* pysqlite_new_node(PyObject* key, PyObject* data)
return NULL; return NULL;
} }
Py_INCREF(key); node->key = Py_NewRef(key);
node->key = key; node->data = Py_NewRef(data);
Py_INCREF(data);
node->data = data;
node->prev = NULL; node->prev = NULL;
node->next = NULL; node->next = NULL;
@ -81,8 +78,7 @@ int pysqlite_cache_init(pysqlite_Cache* self, PyObject* args, PyObject* kwargs)
return -1; return -1;
} }
Py_INCREF(factory); self->factory = Py_NewRef(factory);
self->factory = factory;
self->decref_factory = 1; self->decref_factory = 1;
@ -218,8 +214,7 @@ PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* key)
self->last = node; self->last = node;
} }
Py_INCREF(node->data); return Py_NewRef(node->data);
return node->data;
} }
PyObject* pysqlite_cache_display(pysqlite_Cache* self, PyObject* args) PyObject* pysqlite_cache_display(pysqlite_Cache* self, PyObject* args)

View File

@ -575,8 +575,7 @@ PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_
/* TODO: have a way to show errors here */ /* TODO: have a way to show errors here */
if (!cur_py_value) { if (!cur_py_value) {
PyErr_Clear(); PyErr_Clear();
Py_INCREF(Py_None); cur_py_value = Py_NewRef(Py_None);
cur_py_value = Py_None;
} }
break; break;
case SQLITE_BLOB: case SQLITE_BLOB:
@ -586,8 +585,7 @@ PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_
break; break;
case SQLITE_NULL: case SQLITE_NULL:
default: default:
Py_INCREF(Py_None); cur_py_value = Py_NewRef(Py_None);
cur_py_value = Py_None;
} }
if (!cur_py_value) { if (!cur_py_value) {
@ -853,12 +851,11 @@ pysqlite_connection_create_function_impl(pysqlite_Connection *self,
flags |= SQLITE_DETERMINISTIC; flags |= SQLITE_DETERMINISTIC;
#endif #endif
} }
Py_INCREF(func);
rc = sqlite3_create_function_v2(self->db, rc = sqlite3_create_function_v2(self->db,
name, name,
narg, narg,
flags, flags,
(void*)func, (void*)Py_NewRef(func),
_pysqlite_func_callback, _pysqlite_func_callback,
NULL, NULL,
NULL, NULL,
@ -899,7 +896,7 @@ pysqlite_connection_create_aggregate_impl(pysqlite_Connection *self,
name, name,
n_arg, n_arg,
SQLITE_UTF8, SQLITE_UTF8,
(void*)aggregate_class, (void*)Py_NewRef(aggregate_class),
0, 0,
&_pysqlite_step_callback, &_pysqlite_step_callback,
&_pysqlite_final_callback, &_pysqlite_final_callback,
@ -1212,8 +1209,7 @@ int pysqlite_check_thread(pysqlite_Connection* self)
static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused) static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
{ {
Py_INCREF(self->isolation_level); return Py_NewRef(self->isolation_level);
return self->isolation_level;
} }
static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused) static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
@ -1526,8 +1522,7 @@ pysqlite_connection_interrupt_impl(pysqlite_Connection *self)
sqlite3_interrupt(self->db); sqlite3_interrupt(self->db);
Py_INCREF(Py_None); retval = Py_NewRef(Py_None);
retval = Py_None;
finally: finally:
return retval; return retval;
@ -1746,7 +1741,6 @@ pysqlite_connection_create_collation_impl(pysqlite_Connection *self,
/*[clinic end generated code: output=0f63b8995565ae22 input=5c3898813a776cf2]*/ /*[clinic end generated code: output=0f63b8995565ae22 input=5c3898813a776cf2]*/
{ {
PyObject* uppercase_name = 0; PyObject* uppercase_name = 0;
PyObject* retval;
Py_ssize_t i, len; Py_ssize_t i, len;
_Py_IDENTIFIER(upper); _Py_IDENTIFIER(upper);
const char *uppercase_name_str; const char *uppercase_name_str;
@ -1814,13 +1808,9 @@ finally:
Py_XDECREF(uppercase_name); Py_XDECREF(uppercase_name);
if (PyErr_Occurred()) { if (PyErr_Occurred()) {
retval = NULL; return NULL;
} else {
Py_INCREF(Py_None);
retval = Py_None;
} }
return Py_NewRef(Py_None);
return retval;
} }
/*[clinic input] /*[clinic input]
@ -1835,8 +1825,7 @@ static PyObject *
pysqlite_connection_enter_impl(pysqlite_Connection *self) pysqlite_connection_enter_impl(pysqlite_Connection *self)
/*[clinic end generated code: output=457b09726d3e9dcd input=127d7a4f17e86d8f]*/ /*[clinic end generated code: output=457b09726d3e9dcd input=127d7a4f17e86d8f]*/
{ {
Py_INCREF(self); return Py_NewRef((PyObject *)self);
return (PyObject*)self;
} }
/*[clinic input] /*[clinic input]

View File

@ -271,8 +271,7 @@ _pysqlite_fetch_one_row(pysqlite_Cursor* self)
nbytes = sqlite3_column_bytes(self->statement->st, i); nbytes = sqlite3_column_bytes(self->statement->st, i);
val_str = (const char*)sqlite3_column_blob(self->statement->st, i); val_str = (const char*)sqlite3_column_blob(self->statement->st, i);
if (!val_str) { if (!val_str) {
Py_INCREF(Py_None); converted = Py_NewRef(Py_None);
converted = Py_None;
} else { } else {
item = PyBytes_FromStringAndSize(val_str, nbytes); item = PyBytes_FromStringAndSize(val_str, nbytes);
if (!item) if (!item)
@ -285,8 +284,7 @@ _pysqlite_fetch_one_row(pysqlite_Cursor* self)
coltype = sqlite3_column_type(self->statement->st, i); coltype = sqlite3_column_type(self->statement->st, i);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
if (coltype == SQLITE_NULL) { if (coltype == SQLITE_NULL) {
Py_INCREF(Py_None); converted = Py_NewRef(Py_None);
converted = Py_None;
} else if (coltype == SQLITE_INTEGER) { } else if (coltype == SQLITE_INTEGER) {
converted = PyLong_FromLongLong(sqlite3_column_int64(self->statement->st, i)); converted = PyLong_FromLongLong(sqlite3_column_int64(self->statement->st, i));
} else if (coltype == SQLITE_FLOAT) { } else if (coltype == SQLITE_FLOAT) {
@ -402,8 +400,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
if (PyIter_Check(second_argument)) { if (PyIter_Check(second_argument)) {
/* iterator */ /* iterator */
Py_INCREF(second_argument); parameters_iter = Py_NewRef(second_argument);
parameters_iter = second_argument;
} else { } else {
/* sequence */ /* sequence */
parameters_iter = PyObject_GetIter(second_argument); parameters_iter = PyObject_GetIter(second_argument);
@ -456,8 +453,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
if (!func_args) { if (!func_args) {
goto error; goto error;
} }
Py_INCREF(operation); if (PyTuple_SetItem(func_args, 0, Py_NewRef(operation)) != 0) {
if (PyTuple_SetItem(func_args, 0, operation) != 0) {
goto error; goto error;
} }
@ -555,12 +551,12 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
goto error; goto error;
} }
PyTuple_SetItem(descriptor, 0, column_name); PyTuple_SetItem(descriptor, 0, column_name);
Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 1, Py_None); PyTuple_SetItem(descriptor, 1, Py_NewRef(Py_None));
Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 2, Py_None); PyTuple_SetItem(descriptor, 2, Py_NewRef(Py_None));
Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 3, Py_None); PyTuple_SetItem(descriptor, 3, Py_NewRef(Py_None));
Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 4, Py_None); PyTuple_SetItem(descriptor, 4, Py_NewRef(Py_None));
Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 5, Py_None); PyTuple_SetItem(descriptor, 5, Py_NewRef(Py_None));
Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 6, Py_None); PyTuple_SetItem(descriptor, 6, Py_NewRef(Py_None));
PyTuple_SetItem(self->description, i, descriptor); PyTuple_SetItem(self->description, i, descriptor);
} }
} }
@ -610,8 +606,7 @@ error:
self->rowcount = -1L; self->rowcount = -1L;
return NULL; return NULL;
} else { } else {
Py_INCREF(self); return Py_NewRef((PyObject *)self);
return (PyObject*)self;
} }
} }
@ -706,8 +701,7 @@ error:
if (PyErr_Occurred()) { if (PyErr_Occurred()) {
return NULL; return NULL;
} else { } else {
Py_INCREF(self); return Py_NewRef((PyObject *)self);
return (PyObject*)self;
} }
} }

View File

@ -140,8 +140,7 @@ pysqlite_microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt)
} }
if (alt) { if (alt) {
Py_INCREF(alt); return Py_NewRef(alt);
return alt;
} }
/* else set the right exception and return NULL */ /* else set the right exception and return NULL */
PyErr_SetString(pysqlite_ProgrammingError, "can't adapt"); PyErr_SetString(pysqlite_ProgrammingError, "can't adapt");

View File

@ -120,17 +120,11 @@ static PyObject *
pysqlite_complete_statement_impl(PyObject *module, const char *statement) pysqlite_complete_statement_impl(PyObject *module, const char *statement)
/*[clinic end generated code: output=e55f1ff1952df558 input=f6b24996b31c5c33]*/ /*[clinic end generated code: output=e55f1ff1952df558 input=f6b24996b31c5c33]*/
{ {
PyObject* result;
if (sqlite3_complete(statement)) { if (sqlite3_complete(statement)) {
result = Py_True; return Py_NewRef(Py_True);
} else { } else {
result = Py_False; return Py_NewRef(Py_False);
} }
Py_INCREF(result);
return result;
} }
/*[clinic input] /*[clinic input]
@ -219,8 +213,7 @@ pysqlite_register_converter_impl(PyObject *module, PyObject *orig_name,
goto error; goto error;
} }
Py_INCREF(Py_None); retval = Py_NewRef(Py_None);
retval = Py_None;
error: error:
Py_XDECREF(name); Py_XDECREF(name);
return retval; return retval;

View File

@ -63,11 +63,8 @@ pysqlite_row_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
if (self == NULL) if (self == NULL)
return NULL; return NULL;
Py_INCREF(data); self->data = Py_NewRef(data);
self->data = data; self->description = Py_NewRef(cursor->description);
Py_INCREF(cursor->description);
self->description = cursor->description;
return (PyObject *) self; return (PyObject *) self;
} }
@ -75,8 +72,7 @@ pysqlite_row_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
PyObject* pysqlite_row_item(pysqlite_Row* self, Py_ssize_t idx) PyObject* pysqlite_row_item(pysqlite_Row* self, Py_ssize_t idx)
{ {
PyObject *item = PyTuple_GetItem(self->data, idx); PyObject *item = PyTuple_GetItem(self->data, idx);
Py_XINCREF(item); return Py_XNewRef(item);
return item;
} }
static int static int
@ -111,7 +107,6 @@ PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx)
{ {
Py_ssize_t _idx; Py_ssize_t _idx;
Py_ssize_t nitems, i; Py_ssize_t nitems, i;
PyObject* item;
if (PyLong_Check(idx)) { if (PyLong_Check(idx)) {
_idx = PyNumber_AsSsize_t(idx, PyExc_IndexError); _idx = PyNumber_AsSsize_t(idx, PyExc_IndexError);
@ -119,9 +114,9 @@ PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx)
return NULL; return NULL;
if (_idx < 0) if (_idx < 0)
_idx += PyTuple_GET_SIZE(self->data); _idx += PyTuple_GET_SIZE(self->data);
item = PyTuple_GetItem(self->data, _idx);
Py_XINCREF(item); PyObject *item = PyTuple_GetItem(self->data, _idx);
return item; return Py_XNewRef(item);
} else if (PyUnicode_Check(idx)) { } else if (PyUnicode_Check(idx)) {
nitems = PyTuple_Size(self->description); nitems = PyTuple_Size(self->description);
@ -135,9 +130,8 @@ PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx)
} }
if (eq) { if (eq) {
/* found item */ /* found item */
item = PyTuple_GetItem(self->data, i); PyObject *item = PyTuple_GetItem(self->data, i);
Py_INCREF(item); return Py_XNewRef(item);
return item;
} }
} }

View File

@ -72,8 +72,7 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con
} }
self->in_weakreflist = NULL; self->in_weakreflist = NULL;
Py_INCREF(sql); self->sql = Py_NewRef(sql);
self->sql = sql;
/* Determine if the statement is a DML statement. /* Determine if the statement is a DML statement.
SELECT is the only exception. See #9924. */ SELECT is the only exception. See #9924. */
@ -240,11 +239,11 @@ void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* para
} }
for (i = 0; i < num_params; i++) { for (i = 0; i < num_params; i++) {
if (PyTuple_CheckExact(parameters)) { if (PyTuple_CheckExact(parameters)) {
current_param = PyTuple_GET_ITEM(parameters, i); PyObject *item = PyTuple_GET_ITEM(parameters, i);
Py_INCREF(current_param); current_param = Py_NewRef(item);
} else if (PyList_CheckExact(parameters)) { } else if (PyList_CheckExact(parameters)) {
current_param = PyList_GetItem(parameters, i); PyObject *item = PyList_GetItem(parameters, i);
Py_XINCREF(current_param); current_param = Py_XNewRef(item);
} else { } else {
current_param = PySequence_GetItem(parameters, i); current_param = PySequence_GetItem(parameters, i);
} }
@ -290,8 +289,8 @@ void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* para
return; return;
} }
if (PyDict_CheckExact(parameters)) { if (PyDict_CheckExact(parameters)) {
current_param = PyDict_GetItemWithError(parameters, binding_name_obj); PyObject *item = PyDict_GetItemWithError(parameters, binding_name_obj);
Py_XINCREF(current_param); current_param = Py_XNewRef(item);
} else { } else {
current_param = PyObject_GetItem(parameters, binding_name_obj); current_param = PyObject_GetItem(parameters, binding_name_obj);
} }