Skip to content

Commit bf64d90

Browse files
author
Erlend Egeberg Aasland
authored
bpo-1635741: sqlite3 uses Py_NewRef/Py_XNewRef (GH-23170)
1 parent 3ccef1c commit bf64d90

File tree

7 files changed

+45
-82
lines changed

7 files changed

+45
-82
lines changed

Modules/_sqlite/cache.c

+4-9
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,8 @@ pysqlite_Node* pysqlite_new_node(PyObject* key, PyObject* data)
3434
return NULL;
3535
}
3636

37-
Py_INCREF(key);
38-
node->key = key;
39-
40-
Py_INCREF(data);
41-
node->data = data;
37+
node->key = Py_NewRef(key);
38+
node->data = Py_NewRef(data);
4239

4340
node->prev = NULL;
4441
node->next = NULL;
@@ -81,8 +78,7 @@ int pysqlite_cache_init(pysqlite_Cache* self, PyObject* args, PyObject* kwargs)
8178
return -1;
8279
}
8380

84-
Py_INCREF(factory);
85-
self->factory = factory;
81+
self->factory = Py_NewRef(factory);
8682

8783
self->decref_factory = 1;
8884

@@ -218,8 +214,7 @@ PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* key)
218214
self->last = node;
219215
}
220216

221-
Py_INCREF(node->data);
222-
return node->data;
217+
return Py_NewRef(node->data);
223218
}
224219

225220
PyObject* pysqlite_cache_display(pysqlite_Cache* self, PyObject* args)

Modules/_sqlite/connection.c

+9-20
Original file line numberDiff line numberDiff line change
@@ -575,8 +575,7 @@ PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_
575575
/* TODO: have a way to show errors here */
576576
if (!cur_py_value) {
577577
PyErr_Clear();
578-
Py_INCREF(Py_None);
579-
cur_py_value = Py_None;
578+
cur_py_value = Py_NewRef(Py_None);
580579
}
581580
break;
582581
case SQLITE_BLOB:
@@ -586,8 +585,7 @@ PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_
586585
break;
587586
case SQLITE_NULL:
588587
default:
589-
Py_INCREF(Py_None);
590-
cur_py_value = Py_None;
588+
cur_py_value = Py_NewRef(Py_None);
591589
}
592590

593591
if (!cur_py_value) {
@@ -853,12 +851,11 @@ pysqlite_connection_create_function_impl(pysqlite_Connection *self,
853851
flags |= SQLITE_DETERMINISTIC;
854852
#endif
855853
}
856-
Py_INCREF(func);
857854
rc = sqlite3_create_function_v2(self->db,
858855
name,
859856
narg,
860857
flags,
861-
(void*)func,
858+
(void*)Py_NewRef(func),
862859
_pysqlite_func_callback,
863860
NULL,
864861
NULL,
@@ -899,7 +896,7 @@ pysqlite_connection_create_aggregate_impl(pysqlite_Connection *self,
899896
name,
900897
n_arg,
901898
SQLITE_UTF8,
902-
(void*)aggregate_class,
899+
(void*)Py_NewRef(aggregate_class),
903900
0,
904901
&_pysqlite_step_callback,
905902
&_pysqlite_final_callback,
@@ -1212,8 +1209,7 @@ int pysqlite_check_thread(pysqlite_Connection* self)
12121209

12131210
static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
12141211
{
1215-
Py_INCREF(self->isolation_level);
1216-
return self->isolation_level;
1212+
return Py_NewRef(self->isolation_level);
12171213
}
12181214

12191215
static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
@@ -1526,8 +1522,7 @@ pysqlite_connection_interrupt_impl(pysqlite_Connection *self)
15261522

15271523
sqlite3_interrupt(self->db);
15281524

1529-
Py_INCREF(Py_None);
1530-
retval = Py_None;
1525+
retval = Py_NewRef(Py_None);
15311526

15321527
finally:
15331528
return retval;
@@ -1746,7 +1741,6 @@ pysqlite_connection_create_collation_impl(pysqlite_Connection *self,
17461741
/*[clinic end generated code: output=0f63b8995565ae22 input=5c3898813a776cf2]*/
17471742
{
17481743
PyObject* uppercase_name = 0;
1749-
PyObject* retval;
17501744
Py_ssize_t i, len;
17511745
_Py_IDENTIFIER(upper);
17521746
const char *uppercase_name_str;
@@ -1814,13 +1808,9 @@ pysqlite_connection_create_collation_impl(pysqlite_Connection *self,
18141808
Py_XDECREF(uppercase_name);
18151809

18161810
if (PyErr_Occurred()) {
1817-
retval = NULL;
1818-
} else {
1819-
Py_INCREF(Py_None);
1820-
retval = Py_None;
1811+
return NULL;
18211812
}
1822-
1823-
return retval;
1813+
return Py_NewRef(Py_None);
18241814
}
18251815

18261816
/*[clinic input]
@@ -1835,8 +1825,7 @@ static PyObject *
18351825
pysqlite_connection_enter_impl(pysqlite_Connection *self)
18361826
/*[clinic end generated code: output=457b09726d3e9dcd input=127d7a4f17e86d8f]*/
18371827
{
1838-
Py_INCREF(self);
1839-
return (PyObject*)self;
1828+
return Py_NewRef((PyObject *)self);
18401829
}
18411830

18421831
/*[clinic input]

Modules/_sqlite/cursor.c

+12-18
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,7 @@ _pysqlite_fetch_one_row(pysqlite_Cursor* self)
271271
nbytes = sqlite3_column_bytes(self->statement->st, i);
272272
val_str = (const char*)sqlite3_column_blob(self->statement->st, i);
273273
if (!val_str) {
274-
Py_INCREF(Py_None);
275-
converted = Py_None;
274+
converted = Py_NewRef(Py_None);
276275
} else {
277276
item = PyBytes_FromStringAndSize(val_str, nbytes);
278277
if (!item)
@@ -285,8 +284,7 @@ _pysqlite_fetch_one_row(pysqlite_Cursor* self)
285284
coltype = sqlite3_column_type(self->statement->st, i);
286285
Py_END_ALLOW_THREADS
287286
if (coltype == SQLITE_NULL) {
288-
Py_INCREF(Py_None);
289-
converted = Py_None;
287+
converted = Py_NewRef(Py_None);
290288
} else if (coltype == SQLITE_INTEGER) {
291289
converted = PyLong_FromLongLong(sqlite3_column_int64(self->statement->st, i));
292290
} else if (coltype == SQLITE_FLOAT) {
@@ -402,8 +400,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
402400

403401
if (PyIter_Check(second_argument)) {
404402
/* iterator */
405-
Py_INCREF(second_argument);
406-
parameters_iter = second_argument;
403+
parameters_iter = Py_NewRef(second_argument);
407404
} else {
408405
/* sequence */
409406
parameters_iter = PyObject_GetIter(second_argument);
@@ -456,8 +453,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
456453
if (!func_args) {
457454
goto error;
458455
}
459-
Py_INCREF(operation);
460-
if (PyTuple_SetItem(func_args, 0, operation) != 0) {
456+
if (PyTuple_SetItem(func_args, 0, Py_NewRef(operation)) != 0) {
461457
goto error;
462458
}
463459

@@ -555,12 +551,12 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
555551
goto error;
556552
}
557553
PyTuple_SetItem(descriptor, 0, column_name);
558-
Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 1, Py_None);
559-
Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 2, Py_None);
560-
Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 3, Py_None);
561-
Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 4, Py_None);
562-
Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 5, Py_None);
563-
Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 6, Py_None);
554+
PyTuple_SetItem(descriptor, 1, Py_NewRef(Py_None));
555+
PyTuple_SetItem(descriptor, 2, Py_NewRef(Py_None));
556+
PyTuple_SetItem(descriptor, 3, Py_NewRef(Py_None));
557+
PyTuple_SetItem(descriptor, 4, Py_NewRef(Py_None));
558+
PyTuple_SetItem(descriptor, 5, Py_NewRef(Py_None));
559+
PyTuple_SetItem(descriptor, 6, Py_NewRef(Py_None));
564560
PyTuple_SetItem(self->description, i, descriptor);
565561
}
566562
}
@@ -610,8 +606,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
610606
self->rowcount = -1L;
611607
return NULL;
612608
} else {
613-
Py_INCREF(self);
614-
return (PyObject*)self;
609+
return Py_NewRef((PyObject *)self);
615610
}
616611
}
617612

@@ -706,8 +701,7 @@ pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args)
706701
if (PyErr_Occurred()) {
707702
return NULL;
708703
} else {
709-
Py_INCREF(self);
710-
return (PyObject*)self;
704+
return Py_NewRef((PyObject *)self);
711705
}
712706
}
713707

Modules/_sqlite/microprotocols.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ pysqlite_microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt)
140140
}
141141

142142
if (alt) {
143-
Py_INCREF(alt);
144-
return alt;
143+
return Py_NewRef(alt);
145144
}
146145
/* else set the right exception and return NULL */
147146
PyErr_SetString(pysqlite_ProgrammingError, "can't adapt");

Modules/_sqlite/module.c

+3-10
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,11 @@ static PyObject *
120120
pysqlite_complete_statement_impl(PyObject *module, const char *statement)
121121
/*[clinic end generated code: output=e55f1ff1952df558 input=f6b24996b31c5c33]*/
122122
{
123-
PyObject* result;
124-
125123
if (sqlite3_complete(statement)) {
126-
result = Py_True;
124+
return Py_NewRef(Py_True);
127125
} else {
128-
result = Py_False;
126+
return Py_NewRef(Py_False);
129127
}
130-
131-
Py_INCREF(result);
132-
133-
return result;
134128
}
135129

136130
/*[clinic input]
@@ -219,8 +213,7 @@ pysqlite_register_converter_impl(PyObject *module, PyObject *orig_name,
219213
goto error;
220214
}
221215

222-
Py_INCREF(Py_None);
223-
retval = Py_None;
216+
retval = Py_NewRef(Py_None);
224217
error:
225218
Py_XDECREF(name);
226219
return retval;

Modules/_sqlite/row.c

+9-15
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,16 @@ pysqlite_row_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
6363
if (self == NULL)
6464
return NULL;
6565

66-
Py_INCREF(data);
67-
self->data = data;
68-
69-
Py_INCREF(cursor->description);
70-
self->description = cursor->description;
66+
self->data = Py_NewRef(data);
67+
self->description = Py_NewRef(cursor->description);
7168

7269
return (PyObject *) self;
7370
}
7471

7572
PyObject* pysqlite_row_item(pysqlite_Row* self, Py_ssize_t idx)
7673
{
77-
PyObject* item = PyTuple_GetItem(self->data, idx);
78-
Py_XINCREF(item);
79-
return item;
74+
PyObject *item = PyTuple_GetItem(self->data, idx);
75+
return Py_XNewRef(item);
8076
}
8177

8278
static int
@@ -111,17 +107,16 @@ PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx)
111107
{
112108
Py_ssize_t _idx;
113109
Py_ssize_t nitems, i;
114-
PyObject* item;
115110

116111
if (PyLong_Check(idx)) {
117112
_idx = PyNumber_AsSsize_t(idx, PyExc_IndexError);
118113
if (_idx == -1 && PyErr_Occurred())
119114
return NULL;
120115
if (_idx < 0)
121116
_idx += PyTuple_GET_SIZE(self->data);
122-
item = PyTuple_GetItem(self->data, _idx);
123-
Py_XINCREF(item);
124-
return item;
117+
118+
PyObject *item = PyTuple_GetItem(self->data, _idx);
119+
return Py_XNewRef(item);
125120
} else if (PyUnicode_Check(idx)) {
126121
nitems = PyTuple_Size(self->description);
127122

@@ -135,9 +130,8 @@ PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx)
135130
}
136131
if (eq) {
137132
/* found item */
138-
item = PyTuple_GetItem(self->data, i);
139-
Py_INCREF(item);
140-
return item;
133+
PyObject *item = PyTuple_GetItem(self->data, i);
134+
return Py_XNewRef(item);
141135
}
142136
}
143137

Modules/_sqlite/statement.c

+7-8
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con
7272
}
7373

7474
self->in_weakreflist = NULL;
75-
Py_INCREF(sql);
76-
self->sql = sql;
75+
self->sql = Py_NewRef(sql);
7776

7877
/* Determine if the statement is a DML statement.
7978
SELECT is the only exception. See #9924. */
@@ -240,11 +239,11 @@ void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* para
240239
}
241240
for (i = 0; i < num_params; i++) {
242241
if (PyTuple_CheckExact(parameters)) {
243-
current_param = PyTuple_GET_ITEM(parameters, i);
244-
Py_INCREF(current_param);
242+
PyObject *item = PyTuple_GET_ITEM(parameters, i);
243+
current_param = Py_NewRef(item);
245244
} else if (PyList_CheckExact(parameters)) {
246-
current_param = PyList_GetItem(parameters, i);
247-
Py_XINCREF(current_param);
245+
PyObject *item = PyList_GetItem(parameters, i);
246+
current_param = Py_XNewRef(item);
248247
} else {
249248
current_param = PySequence_GetItem(parameters, i);
250249
}
@@ -290,8 +289,8 @@ void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* para
290289
return;
291290
}
292291
if (PyDict_CheckExact(parameters)) {
293-
current_param = PyDict_GetItemWithError(parameters, binding_name_obj);
294-
Py_XINCREF(current_param);
292+
PyObject *item = PyDict_GetItemWithError(parameters, binding_name_obj);
293+
current_param = Py_XNewRef(item);
295294
} else {
296295
current_param = PyObject_GetItem(parameters, binding_name_obj);
297296
}

0 commit comments

Comments
 (0)