Skip to content

Commit afac3c9

Browse files
gh-111789: Simplify the sqlite code (GH-111829)
Use new C API functions PyDict_GetItemRef() and PyMapping_GetOptionalItemString().
1 parent 771bd3c commit afac3c9

File tree

2 files changed

+6
-17
lines changed

2 files changed

+6
-17
lines changed

Modules/_sqlite/cursor.c

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,6 @@ bind_parameters(pysqlite_state *state, pysqlite_Statement *self,
721721
} else if (PyDict_Check(parameters)) {
722722
/* parameters passed as dictionary */
723723
for (i = 1; i <= num_params_needed; i++) {
724-
PyObject *binding_name_obj;
725724
Py_BEGIN_ALLOW_THREADS
726725
binding_name = sqlite3_bind_parameter_name(self->st, i);
727726
Py_END_ALLOW_THREADS
@@ -733,17 +732,8 @@ bind_parameters(pysqlite_state *state, pysqlite_Statement *self,
733732
}
734733

735734
binding_name++; /* skip first char (the colon) */
736-
binding_name_obj = PyUnicode_FromString(binding_name);
737-
if (!binding_name_obj) {
738-
return;
739-
}
740-
if (PyDict_CheckExact(parameters)) {
741-
PyObject *item = PyDict_GetItemWithError(parameters, binding_name_obj);
742-
current_param = Py_XNewRef(item);
743-
} else {
744-
current_param = PyObject_GetItem(parameters, binding_name_obj);
745-
}
746-
Py_DECREF(binding_name_obj);
735+
PyObject *current_param;
736+
(void)PyMapping_GetOptionalItemString(parameters, binding_name, &current_param);
747737
if (!current_param) {
748738
if (!PyErr_Occurred() || PyErr_ExceptionMatches(PyExc_LookupError)) {
749739
PyErr_Format(state->ProgrammingError,

Modules/_sqlite/microprotocols.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,16 @@ pysqlite_microprotocols_adapt(pysqlite_state *state, PyObject *obj,
8585
if (!key) {
8686
return NULL;
8787
}
88-
adapter = PyDict_GetItemWithError(state->psyco_adapters, key);
88+
if (PyDict_GetItemRef(state->psyco_adapters, key, &adapter) < 0) {
89+
Py_DECREF(key);
90+
return NULL;
91+
}
8992
Py_DECREF(key);
9093
if (adapter) {
91-
Py_INCREF(adapter);
9294
adapted = PyObject_CallOneArg(adapter, obj);
9395
Py_DECREF(adapter);
9496
return adapted;
9597
}
96-
if (PyErr_Occurred()) {
97-
return NULL;
98-
}
9998

10099
/* try to have the protocol adapt this object */
101100
if (PyObject_GetOptionalAttr(proto, state->str___adapt__, &adapter) < 0) {

0 commit comments

Comments
 (0)