Skip to content

Commit b207711

Browse files
author
Erlend Egeberg Aasland
authored
bpo-46541: Replace _Py_IDENTIFIER with _Py_ID in sqlite3 (GH-31351)
1 parent d64f3ca commit b207711

File tree

5 files changed

+61
-39
lines changed

5 files changed

+61
-39
lines changed

Modules/_sqlite/connection.c

+18-23
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
* 3. This notice may not be removed or altered from any source distribution.
2222
*/
2323

24-
#define NEEDS_PY_IDENTIFIER
25-
2624
#include "module.h"
2725
#include "structmember.h" // PyMemberDef
2826
#include "connection.h"
@@ -125,13 +123,12 @@ class _sqlite3.Connection "pysqlite_Connection *" "clinic_state()->ConnectionTyp
125123
[clinic start generated code]*/
126124
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=67369db2faf80891]*/
127125

128-
_Py_IDENTIFIER(cursor);
129-
130126
static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self);
131127
static void free_callback_context(callback_context *ctx);
132128
static void set_callback_context(callback_context **ctx_pp,
133129
callback_context *ctx);
134130
static void connection_close(pysqlite_Connection *self);
131+
PyObject *_pysqlite_query_execute(pysqlite_Cursor *, int, PyObject *, PyObject *);
135132

136133
static PyObject *
137134
new_statement_cache(pysqlite_Connection *self, pysqlite_state *state,
@@ -782,7 +779,6 @@ final_callback(sqlite3_context *context)
782779

783780
PyObject* function_result;
784781
PyObject** aggregate_instance;
785-
_Py_IDENTIFIER(finalize);
786782
int ok;
787783
PyObject *exception, *value, *tb;
788784

@@ -801,8 +797,10 @@ final_callback(sqlite3_context *context)
801797
/* Keep the exception (if any) of the last call to step() */
802798
PyErr_Fetch(&exception, &value, &tb);
803799

804-
function_result = _PyObject_CallMethodIdNoArgs(*aggregate_instance, &PyId_finalize);
805-
800+
callback_context *ctx = (callback_context *)sqlite3_user_data(context);
801+
assert(ctx != NULL);
802+
function_result = PyObject_CallMethodNoArgs(*aggregate_instance,
803+
ctx->state->str_finalize);
806804
Py_DECREF(*aggregate_instance);
807805

808806
ok = 0;
@@ -1432,16 +1430,14 @@ pysqlite_connection_execute_impl(pysqlite_Connection *self, PyObject *sql,
14321430
PyObject *parameters)
14331431
/*[clinic end generated code: output=5be05ae01ee17ee4 input=fbd17c75c7140271]*/
14341432
{
1435-
_Py_IDENTIFIER(execute);
1436-
PyObject* cursor = 0;
14371433
PyObject* result = 0;
14381434

1439-
cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
1435+
PyObject *cursor = pysqlite_connection_cursor_impl(self, NULL);
14401436
if (!cursor) {
14411437
goto error;
14421438
}
14431439

1444-
result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_execute, sql, parameters, NULL);
1440+
result = _pysqlite_query_execute((pysqlite_Cursor *)cursor, 0, sql, parameters);
14451441
if (!result) {
14461442
Py_CLEAR(cursor);
14471443
}
@@ -1467,17 +1463,14 @@ pysqlite_connection_executemany_impl(pysqlite_Connection *self,
14671463
PyObject *sql, PyObject *parameters)
14681464
/*[clinic end generated code: output=776cd2fd20bfe71f input=4feab80659ffc82b]*/
14691465
{
1470-
_Py_IDENTIFIER(executemany);
1471-
PyObject* cursor = 0;
14721466
PyObject* result = 0;
14731467

1474-
cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
1468+
PyObject *cursor = pysqlite_connection_cursor_impl(self, NULL);
14751469
if (!cursor) {
14761470
goto error;
14771471
}
14781472

1479-
result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_executemany, sql,
1480-
parameters, NULL);
1473+
result = _pysqlite_query_execute((pysqlite_Cursor *)cursor, 1, sql, parameters);
14811474
if (!result) {
14821475
Py_CLEAR(cursor);
14831476
}
@@ -1502,17 +1495,15 @@ pysqlite_connection_executescript(pysqlite_Connection *self,
15021495
PyObject *script_obj)
15031496
/*[clinic end generated code: output=4c4f9d77aa0ae37d input=b27ae5c24ffb8b43]*/
15041497
{
1505-
_Py_IDENTIFIER(executescript);
1506-
PyObject* cursor = 0;
15071498
PyObject* result = 0;
15081499

1509-
cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
1500+
PyObject *cursor = pysqlite_connection_cursor_impl(self, NULL);
15101501
if (!cursor) {
15111502
goto error;
15121503
}
15131504

1514-
result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_executescript,
1515-
script_obj, NULL);
1505+
PyObject *meth = self->state->str_executescript; // borrowed ref.
1506+
result = PyObject_CallMethodObjArgs(cursor, meth, script_obj, NULL);
15161507
if (!result) {
15171508
Py_CLEAR(cursor);
15181509
}
@@ -1620,7 +1611,6 @@ static PyObject *
16201611
pysqlite_connection_iterdump_impl(pysqlite_Connection *self)
16211612
/*[clinic end generated code: output=586997aaf9808768 input=53bc907cb5eedb85]*/
16221613
{
1623-
_Py_IDENTIFIER(_iterdump);
16241614
PyObject* retval = NULL;
16251615
PyObject* module = NULL;
16261616
PyObject* module_dict;
@@ -1640,7 +1630,12 @@ pysqlite_connection_iterdump_impl(pysqlite_Connection *self)
16401630
goto finally;
16411631
}
16421632

1643-
pyfn_iterdump = _PyDict_GetItemIdWithError(module_dict, &PyId__iterdump);
1633+
PyObject *meth = PyUnicode_InternFromString("_iterdump");
1634+
if (meth == NULL) {
1635+
goto finally;
1636+
}
1637+
pyfn_iterdump = PyDict_GetItemWithError(module_dict, meth);
1638+
Py_DECREF(meth);
16441639
if (!pyfn_iterdump) {
16451640
if (!PyErr_Occurred()) {
16461641
PyErr_SetString(self->OperationalError,

Modules/_sqlite/cursor.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
* 3. This notice may not be removed or altered from any source distribution.
2222
*/
2323

24-
#define NEEDS_PY_IDENTIFIER
25-
2624
#include "cursor.h"
2725
#include "module.h"
2826
#include "util.h"
@@ -131,13 +129,12 @@ _pysqlite_get_converter(pysqlite_state *state, const char *keystr,
131129
PyObject *key;
132130
PyObject *upcase_key;
133131
PyObject *retval;
134-
_Py_IDENTIFIER(upper);
135132

136133
key = PyUnicode_FromStringAndSize(keystr, keylen);
137134
if (!key) {
138135
return NULL;
139136
}
140-
upcase_key = _PyObject_CallMethodIdNoArgs(key, &PyId_upper);
137+
upcase_key = PyObject_CallMethodNoArgs(key, state->str_upper);
141138
Py_DECREF(key);
142139
if (!upcase_key) {
143140
return NULL;
@@ -457,7 +454,7 @@ get_statement_from_cache(pysqlite_Cursor *self, PyObject *operation)
457454
return PyObject_Vectorcall(cache, args + 1, nargsf, NULL);
458455
}
459456

460-
static PyObject *
457+
PyObject *
461458
_pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation, PyObject* second_argument)
462459
{
463460
PyObject* parameters_list = NULL;

Modules/_sqlite/microprotocols.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
* 3. This notice may not be removed or altered from any source distribution.
2424
*/
2525

26-
#define NEEDS_PY_IDENTIFIER
27-
2826
#include <Python.h>
2927

3028
#include "cursor.h"
@@ -76,8 +74,6 @@ PyObject *
7674
pysqlite_microprotocols_adapt(pysqlite_state *state, PyObject *obj,
7775
PyObject *proto, PyObject *alt)
7876
{
79-
_Py_IDENTIFIER(__adapt__);
80-
_Py_IDENTIFIER(__conform__);
8177
PyObject *adapter, *key, *adapted;
8278

8379
/* we don't check for exact type conformance as specified in PEP 246
@@ -102,7 +98,7 @@ pysqlite_microprotocols_adapt(pysqlite_state *state, PyObject *obj,
10298
}
10399

104100
/* try to have the protocol adapt this object */
105-
if (_PyObject_LookupAttrId(proto, &PyId___adapt__, &adapter) < 0) {
101+
if (_PyObject_LookupAttr(proto, state->str___adapt__, &adapter) < 0) {
106102
return NULL;
107103
}
108104
if (adapter) {
@@ -121,7 +117,7 @@ pysqlite_microprotocols_adapt(pysqlite_state *state, PyObject *obj,
121117
}
122118

123119
/* and finally try to have the object adapt itself */
124-
if (_PyObject_LookupAttrId(obj, &PyId___conform__, &adapter) < 0) {
120+
if (_PyObject_LookupAttr(obj, state->str___conform__, &adapter) < 0) {
125121
return NULL;
126122
}
127123
if (adapter) {

Modules/_sqlite/module.c

+32-5
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
* 3. This notice may not be removed or altered from any source distribution.
2222
*/
2323

24-
#define NEEDS_PY_IDENTIFIER
25-
2624
#include "connection.h"
2725
#include "statement.h"
2826
#include "cursor.h"
@@ -185,15 +183,14 @@ pysqlite_register_converter_impl(PyObject *module, PyObject *orig_name,
185183
{
186184
PyObject* name = NULL;
187185
PyObject* retval = NULL;
188-
_Py_IDENTIFIER(upper);
189186

190187
/* convert the name to upper case */
191-
name = _PyObject_CallMethodIdNoArgs(orig_name, &PyId_upper);
188+
pysqlite_state *state = pysqlite_get_state(module);
189+
name = PyObject_CallMethodNoArgs(orig_name, state->str_upper);
192190
if (!name) {
193191
goto error;
194192
}
195193

196-
pysqlite_state *state = pysqlite_get_state(module);
197194
if (PyDict_SetItem(state->converters, name, callable) != 0) {
198195
goto error;
199196
}
@@ -593,6 +590,13 @@ module_traverse(PyObject *module, visitproc visit, void *arg)
593590
Py_VISIT(state->lru_cache);
594591
Py_VISIT(state->psyco_adapters);
595592

593+
// Interned strings
594+
Py_VISIT(state->str___adapt__);
595+
Py_VISIT(state->str___conform__);
596+
Py_VISIT(state->str_executescript);
597+
Py_VISIT(state->str_finalize);
598+
Py_VISIT(state->str_upper);
599+
596600
return 0;
597601
}
598602

@@ -625,6 +629,13 @@ module_clear(PyObject *module)
625629
Py_CLEAR(state->lru_cache);
626630
Py_CLEAR(state->psyco_adapters);
627631

632+
// Interned strings
633+
Py_CLEAR(state->str___adapt__);
634+
Py_CLEAR(state->str___conform__);
635+
Py_CLEAR(state->str_executescript);
636+
Py_CLEAR(state->str_finalize);
637+
Py_CLEAR(state->str_upper);
638+
628639
return 0;
629640
}
630641

@@ -650,6 +661,15 @@ do { \
650661
ADD_TYPE(module, (PyTypeObject *)state->exc); \
651662
} while (0)
652663

664+
#define ADD_INTERNED(state, string) \
665+
do { \
666+
PyObject *tmp = PyUnicode_InternFromString(#string); \
667+
if (tmp == NULL) { \
668+
goto error; \
669+
} \
670+
state->str_ ## string = tmp; \
671+
} while (0)
672+
653673
static int
654674
module_exec(PyObject *module)
655675
{
@@ -695,6 +715,13 @@ module_exec(PyObject *module)
695715
ADD_EXCEPTION(module, state, DataError, state->DatabaseError);
696716
ADD_EXCEPTION(module, state, NotSupportedError, state->DatabaseError);
697717

718+
/* Add interned strings */
719+
ADD_INTERNED(state, __adapt__);
720+
ADD_INTERNED(state, __conform__);
721+
ADD_INTERNED(state, executescript);
722+
ADD_INTERNED(state, finalize);
723+
ADD_INTERNED(state, upper);
724+
698725
/* Set error constants */
699726
if (add_error_constants(module) < 0) {
700727
goto error;

Modules/_sqlite/module.h

+7
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ typedef struct {
5858
PyTypeObject *PrepareProtocolType;
5959
PyTypeObject *RowType;
6060
PyTypeObject *StatementType;
61+
62+
/* Pointers to interned strings */
63+
PyObject *str___adapt__;
64+
PyObject *str___conform__;
65+
PyObject *str_executescript;
66+
PyObject *str_finalize;
67+
PyObject *str_upper;
6168
} pysqlite_state;
6269

6370
extern pysqlite_state pysqlite_global_state;

0 commit comments

Comments
 (0)