Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-1635741: sqlite3 now uses Py_NewRef and Py_XNewRef #23170

Merged
merged 6 commits into from
Dec 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions Modules/_sqlite/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,8 @@ pysqlite_Node* pysqlite_new_node(PyObject* key, PyObject* data)
return NULL;
}

Py_INCREF(key);
node->key = key;

Py_INCREF(data);
node->data = data;
node->key = Py_NewRef(key);
node->data = Py_NewRef(data);

node->prev = NULL;
node->next = NULL;
Expand Down Expand Up @@ -81,8 +78,7 @@ int pysqlite_cache_init(pysqlite_Cache* self, PyObject* args, PyObject* kwargs)
return -1;
}

Py_INCREF(factory);
self->factory = factory;
self->factory = Py_NewRef(factory);

self->decref_factory = 1;

Expand Down Expand Up @@ -218,8 +214,7 @@ PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* key)
self->last = node;
}

Py_INCREF(node->data);
return node->data;
return Py_NewRef(node->data);
}

PyObject* pysqlite_cache_display(pysqlite_Cache* self, PyObject* args)
Expand Down
36 changes: 11 additions & 25 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,8 @@ int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject
Py_CLEAR(self->statements);
Py_CLEAR(self->cursors);

Py_INCREF(Py_None);
Py_XSETREF(self->row_factory, Py_None);

Py_INCREF(&PyUnicode_Type);
Py_XSETREF(self->text_factory, (PyObject*)&PyUnicode_Type);
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved
self->row_factory = Py_NewRef(Py_None);
self->text_factory = Py_NewRef((PyObject*)&PyUnicode_Type);

#ifdef SQLITE_OPEN_URI
Py_BEGIN_ALLOW_THREADS
Expand Down Expand Up @@ -541,8 +538,7 @@ PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_
/* TODO: have a way to show errors here */
if (!cur_py_value) {
PyErr_Clear();
Py_INCREF(Py_None);
cur_py_value = Py_None;
cur_py_value = Py_NewRef(Py_None);
}
break;
case SQLITE_BLOB:
Expand All @@ -552,8 +548,7 @@ PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_
break;
case SQLITE_NULL:
default:
Py_INCREF(Py_None);
cur_py_value = Py_None;
cur_py_value = Py_NewRef(Py_None);
}

if (!cur_py_value) {
Expand Down Expand Up @@ -815,12 +810,11 @@ PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObjec
flags |= SQLITE_DETERMINISTIC;
#endif
}
Py_INCREF(func);
rc = sqlite3_create_function_v2(self->db,
name,
narg,
flags,
(void*)func,
(void*)Py_NewRef(func),
_pysqlite_func_callback,
NULL,
NULL,
Expand Down Expand Up @@ -851,12 +845,11 @@ PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObje
kwlist, &name, &n_arg, &aggregate_class)) {
return NULL;
}
Py_INCREF(aggregate_class);
rc = sqlite3_create_function_v2(self->db,
name,
n_arg,
SQLITE_UTF8,
(void*)aggregate_class,
(void*)Py_NewRef(aggregate_class),
0,
&_pysqlite_step_callback,
&_pysqlite_final_callback,
Expand Down Expand Up @@ -1144,8 +1137,7 @@ int pysqlite_check_thread(pysqlite_Connection* self)

static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
{
Py_INCREF(self->isolation_level);
return self->isolation_level;
return Py_NewRef(self->isolation_level);
}

static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Expand Down Expand Up @@ -1432,8 +1424,7 @@ pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)

sqlite3_interrupt(self->db);

Py_INCREF(Py_None);
retval = Py_None;
retval = Py_NewRef(Py_None);

finally:
return retval;
Expand Down Expand Up @@ -1643,7 +1634,6 @@ pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
PyObject* callable;
PyObject* uppercase_name = 0;
PyObject* name;
PyObject* retval;
Py_ssize_t i, len;
_Py_IDENTIFIER(upper);
const char *uppercase_name_str;
Expand Down Expand Up @@ -1716,22 +1706,18 @@ pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Py_XDECREF(uppercase_name);

if (PyErr_Occurred()) {
retval = NULL;
return NULL;
} else {
Py_INCREF(Py_None);
retval = Py_None;
return Py_NewRef(Py_None);
}
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved

return retval;
}

/* Called when the connection is used as a context manager. Returns itself as a
* convenience to the caller. */
static PyObject *
pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
{
Py_INCREF(self);
return (PyObject*)self;
return Py_NewRef((PyObject *)self);
}

/** Called when the connection is used as a context manager. If there was any
Expand Down
30 changes: 12 additions & 18 deletions Modules/_sqlite/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,7 @@ _pysqlite_fetch_one_row(pysqlite_Cursor* self)
nbytes = sqlite3_column_bytes(self->statement->st, i);
val_str = (const char*)sqlite3_column_blob(self->statement->st, i);
if (!val_str) {
Py_INCREF(Py_None);
converted = Py_None;
converted = Py_NewRef(Py_None);
} else {
item = PyBytes_FromStringAndSize(val_str, nbytes);
if (!item)
Expand All @@ -285,8 +284,7 @@ _pysqlite_fetch_one_row(pysqlite_Cursor* self)
coltype = sqlite3_column_type(self->statement->st, i);
Py_END_ALLOW_THREADS
if (coltype == SQLITE_NULL) {
Py_INCREF(Py_None);
converted = Py_None;
converted = Py_NewRef(Py_None);
} else if (coltype == SQLITE_INTEGER) {
converted = PyLong_FromLongLong(sqlite3_column_int64(self->statement->st, i));
} else if (coltype == SQLITE_FLOAT) {
Expand Down Expand Up @@ -402,8 +400,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)

if (PyIter_Check(second_argument)) {
/* iterator */
Py_INCREF(second_argument);
parameters_iter = second_argument;
parameters_iter = Py_NewRef(second_argument);
} else {
/* sequence */
parameters_iter = PyObject_GetIter(second_argument);
Expand Down Expand Up @@ -456,8 +453,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
if (!func_args) {
goto error;
}
Py_INCREF(operation);
if (PyTuple_SetItem(func_args, 0, operation) != 0) {
if (PyTuple_SetItem(func_args, 0, Py_NewRef(operation)) != 0) {
goto error;
}

Expand Down Expand Up @@ -555,12 +551,12 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
goto error;
}
PyTuple_SetItem(descriptor, 0, column_name);
Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 1, Py_None);
Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 2, Py_None);
Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 3, Py_None);
Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 4, Py_None);
Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 5, Py_None);
Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 6, Py_None);
PyTuple_SetItem(descriptor, 1, Py_NewRef(Py_None));
PyTuple_SetItem(descriptor, 2, Py_NewRef(Py_None));
PyTuple_SetItem(descriptor, 3, Py_NewRef(Py_None));
PyTuple_SetItem(descriptor, 4, Py_NewRef(Py_None));
PyTuple_SetItem(descriptor, 5, Py_NewRef(Py_None));
PyTuple_SetItem(descriptor, 6, Py_NewRef(Py_None));
PyTuple_SetItem(self->description, i, descriptor);
}
}
Expand Down Expand Up @@ -610,8 +606,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
self->rowcount = -1L;
return NULL;
} else {
Py_INCREF(self);
return (PyObject*)self;
return Py_NewRef((PyObject *)self);
}
}

Expand Down Expand Up @@ -705,8 +700,7 @@ pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args)
if (PyErr_Occurred()) {
return NULL;
} else {
Py_INCREF(self);
return (PyObject*)self;
return Py_NewRef((PyObject *)self);
}
}

Expand Down
3 changes: 1 addition & 2 deletions Modules/_sqlite/microprotocols.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ pysqlite_microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt)
}

if (alt) {
Py_INCREF(alt);
return alt;
return Py_NewRef(alt);
}
/* else set the right exception and return NULL */
PyErr_SetString(pysqlite_ProgrammingError, "can't adapt");
Expand Down
13 changes: 3 additions & 10 deletions Modules/_sqlite/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,11 @@ static PyObject *
pysqlite_complete_statement_impl(PyObject *module, const char *statement)
/*[clinic end generated code: output=e55f1ff1952df558 input=f6b24996b31c5c33]*/
{
PyObject* result;

if (sqlite3_complete(statement)) {
result = Py_True;
return Py_NewRef(Py_True);
} else {
result = Py_False;
return Py_NewRef(Py_False);
}

Py_INCREF(result);

return result;
}

/*[clinic input]
Expand Down Expand Up @@ -219,8 +213,7 @@ pysqlite_register_converter_impl(PyObject *module, PyObject *orig_name,
goto error;
}

Py_INCREF(Py_None);
retval = Py_None;
retval = Py_NewRef(Py_None);
error:
Py_XDECREF(name);
return retval;
Expand Down
20 changes: 5 additions & 15 deletions Modules/_sqlite/row.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,15 @@ pysqlite_row_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
if (self == NULL)
return NULL;

Py_INCREF(data);
self->data = data;

Py_INCREF(cursor->description);
self->description = cursor->description;
self->data = Py_NewRef(data);
self->description = Py_NewRef(cursor->description);

return (PyObject *) self;
}

PyObject* pysqlite_row_item(pysqlite_Row* self, Py_ssize_t idx)
{
PyObject* item = PyTuple_GetItem(self->data, idx);
Py_XINCREF(item);
return item;
return Py_XNewRef(PyTuple_GetItem(self->data, idx));
}

static int
Expand Down Expand Up @@ -111,17 +106,14 @@ PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx)
{
Py_ssize_t _idx;
Py_ssize_t nitems, i;
PyObject* item;

if (PyLong_Check(idx)) {
_idx = PyNumber_AsSsize_t(idx, PyExc_IndexError);
if (_idx == -1 && PyErr_Occurred())
return NULL;
if (_idx < 0)
_idx += PyTuple_GET_SIZE(self->data);
item = PyTuple_GetItem(self->data, _idx);
Py_XINCREF(item);
return item;
return Py_XNewRef(PyTuple_GetItem(self->data, _idx));
} else if (PyUnicode_Check(idx)) {
nitems = PyTuple_Size(self->description);

Expand All @@ -135,9 +127,7 @@ PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx)
}
if (eq) {
/* found item */
item = PyTuple_GetItem(self->data, i);
Py_INCREF(item);
return item;
return Py_XNewRef(PyTuple_GetItem(self->data, i));
}
}

Expand Down
12 changes: 4 additions & 8 deletions Modules/_sqlite/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con
}

self->in_weakreflist = NULL;
Py_INCREF(sql);
self->sql = sql;
self->sql = Py_NewRef(sql);

/* Determine if the statement is a DML statement.
SELECT is the only exception. See #9924. */
Expand Down Expand Up @@ -240,11 +239,9 @@ void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* para
}
for (i = 0; i < num_params; i++) {
if (PyTuple_CheckExact(parameters)) {
current_param = PyTuple_GET_ITEM(parameters, i);
Py_INCREF(current_param);
current_param = Py_NewRef(PyTuple_GET_ITEM(parameters, i));
} else if (PyList_CheckExact(parameters)) {
current_param = PyList_GetItem(parameters, i);
Py_XINCREF(current_param);
current_param = Py_XNewRef(PyList_GetItem(parameters, i));
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved
} else {
current_param = PySequence_GetItem(parameters, i);
}
Expand Down Expand Up @@ -290,8 +287,7 @@ void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* para
return;
}
if (PyDict_CheckExact(parameters)) {
current_param = PyDict_GetItemWithError(parameters, binding_name_obj);
Py_XINCREF(current_param);
current_param = Py_XNewRef(PyDict_GetItemWithError(parameters, binding_name_obj));
} else {
current_param = PyObject_GetItem(parameters, binding_name_obj);
}
Expand Down