[Python-checkins] bpo-46541: Replace _Py_IDENTIFIER with _Py_ID in sqlite3 (GH-31351)

corona10 webhook-mailer at python.org
Wed Feb 16 10:24:50 EST 2022


https://github.com/python/cpython/commit/b2077117d125925210148294eefee28797b7ff4c
commit: b2077117d125925210148294eefee28797b7ff4c
branch: main
author: Erlend Egeberg Aasland <erlend.aasland at innova.no>
committer: corona10 <donghee.na92 at gmail.com>
date: 2022-02-17T00:24:44+09:00
summary:

bpo-46541: Replace _Py_IDENTIFIER with _Py_ID in sqlite3 (GH-31351)

files:
M Modules/_sqlite/connection.c
M Modules/_sqlite/cursor.c
M Modules/_sqlite/microprotocols.c
M Modules/_sqlite/module.c
M Modules/_sqlite/module.h

diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c
index cb8cd2a3d1807..0efb5ae35a7f5 100644
--- a/Modules/_sqlite/connection.c
+++ b/Modules/_sqlite/connection.c
@@ -21,8 +21,6 @@
  * 3. This notice may not be removed or altered from any source distribution.
  */
 
-#define NEEDS_PY_IDENTIFIER
-
 #include "module.h"
 #include "structmember.h"         // PyMemberDef
 #include "connection.h"
@@ -125,13 +123,12 @@ class _sqlite3.Connection "pysqlite_Connection *" "clinic_state()->ConnectionTyp
 [clinic start generated code]*/
 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=67369db2faf80891]*/
 
-_Py_IDENTIFIER(cursor);
-
 static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self);
 static void free_callback_context(callback_context *ctx);
 static void set_callback_context(callback_context **ctx_pp,
                                  callback_context *ctx);
 static void connection_close(pysqlite_Connection *self);
+PyObject *_pysqlite_query_execute(pysqlite_Cursor *, int, PyObject *, PyObject *);
 
 static PyObject *
 new_statement_cache(pysqlite_Connection *self, pysqlite_state *state,
@@ -782,7 +779,6 @@ final_callback(sqlite3_context *context)
 
     PyObject* function_result;
     PyObject** aggregate_instance;
-    _Py_IDENTIFIER(finalize);
     int ok;
     PyObject *exception, *value, *tb;
 
@@ -801,8 +797,10 @@ final_callback(sqlite3_context *context)
     /* Keep the exception (if any) of the last call to step() */
     PyErr_Fetch(&exception, &value, &tb);
 
-    function_result = _PyObject_CallMethodIdNoArgs(*aggregate_instance, &PyId_finalize);
-
+    callback_context *ctx = (callback_context *)sqlite3_user_data(context);
+    assert(ctx != NULL);
+    function_result = PyObject_CallMethodNoArgs(*aggregate_instance,
+                                                ctx->state->str_finalize);
     Py_DECREF(*aggregate_instance);
 
     ok = 0;
@@ -1432,16 +1430,14 @@ pysqlite_connection_execute_impl(pysqlite_Connection *self, PyObject *sql,
                                  PyObject *parameters)
 /*[clinic end generated code: output=5be05ae01ee17ee4 input=fbd17c75c7140271]*/
 {
-    _Py_IDENTIFIER(execute);
-    PyObject* cursor = 0;
     PyObject* result = 0;
 
-    cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
+    PyObject *cursor = pysqlite_connection_cursor_impl(self, NULL);
     if (!cursor) {
         goto error;
     }
 
-    result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_execute, sql, parameters, NULL);
+    result = _pysqlite_query_execute((pysqlite_Cursor *)cursor, 0, sql, parameters);
     if (!result) {
         Py_CLEAR(cursor);
     }
@@ -1467,17 +1463,14 @@ pysqlite_connection_executemany_impl(pysqlite_Connection *self,
                                      PyObject *sql, PyObject *parameters)
 /*[clinic end generated code: output=776cd2fd20bfe71f input=4feab80659ffc82b]*/
 {
-    _Py_IDENTIFIER(executemany);
-    PyObject* cursor = 0;
     PyObject* result = 0;
 
-    cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
+    PyObject *cursor = pysqlite_connection_cursor_impl(self, NULL);
     if (!cursor) {
         goto error;
     }
 
-    result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_executemany, sql,
-                                           parameters, NULL);
+    result = _pysqlite_query_execute((pysqlite_Cursor *)cursor, 1, sql, parameters);
     if (!result) {
         Py_CLEAR(cursor);
     }
@@ -1502,17 +1495,15 @@ pysqlite_connection_executescript(pysqlite_Connection *self,
                                   PyObject *script_obj)
 /*[clinic end generated code: output=4c4f9d77aa0ae37d input=b27ae5c24ffb8b43]*/
 {
-    _Py_IDENTIFIER(executescript);
-    PyObject* cursor = 0;
     PyObject* result = 0;
 
-    cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
+    PyObject *cursor = pysqlite_connection_cursor_impl(self, NULL);
     if (!cursor) {
         goto error;
     }
 
-    result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_executescript,
-                                           script_obj, NULL);
+    PyObject *meth = self->state->str_executescript;  // borrowed ref.
+    result = PyObject_CallMethodObjArgs(cursor, meth, script_obj, NULL);
     if (!result) {
         Py_CLEAR(cursor);
     }
@@ -1620,7 +1611,6 @@ static PyObject *
 pysqlite_connection_iterdump_impl(pysqlite_Connection *self)
 /*[clinic end generated code: output=586997aaf9808768 input=53bc907cb5eedb85]*/
 {
-    _Py_IDENTIFIER(_iterdump);
     PyObject* retval = NULL;
     PyObject* module = NULL;
     PyObject* module_dict;
@@ -1640,7 +1630,12 @@ pysqlite_connection_iterdump_impl(pysqlite_Connection *self)
         goto finally;
     }
 
-    pyfn_iterdump = _PyDict_GetItemIdWithError(module_dict, &PyId__iterdump);
+    PyObject *meth = PyUnicode_InternFromString("_iterdump");
+    if (meth == NULL) {
+        goto finally;
+    }
+    pyfn_iterdump = PyDict_GetItemWithError(module_dict, meth);
+    Py_DECREF(meth);
     if (!pyfn_iterdump) {
         if (!PyErr_Occurred()) {
             PyErr_SetString(self->OperationalError,
diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c
index 395b10f33ff3a..b0dd9dd39c8ee 100644
--- a/Modules/_sqlite/cursor.c
+++ b/Modules/_sqlite/cursor.c
@@ -21,8 +21,6 @@
  * 3. This notice may not be removed or altered from any source distribution.
  */
 
-#define NEEDS_PY_IDENTIFIER
-
 #include "cursor.h"
 #include "module.h"
 #include "util.h"
@@ -131,13 +129,12 @@ _pysqlite_get_converter(pysqlite_state *state, const char *keystr,
     PyObject *key;
     PyObject *upcase_key;
     PyObject *retval;
-    _Py_IDENTIFIER(upper);
 
     key = PyUnicode_FromStringAndSize(keystr, keylen);
     if (!key) {
         return NULL;
     }
-    upcase_key = _PyObject_CallMethodIdNoArgs(key, &PyId_upper);
+    upcase_key = PyObject_CallMethodNoArgs(key, state->str_upper);
     Py_DECREF(key);
     if (!upcase_key) {
         return NULL;
@@ -457,7 +454,7 @@ get_statement_from_cache(pysqlite_Cursor *self, PyObject *operation)
     return PyObject_Vectorcall(cache, args + 1, nargsf, NULL);
 }
 
-static PyObject *
+PyObject *
 _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation, PyObject* second_argument)
 {
     PyObject* parameters_list = NULL;
diff --git a/Modules/_sqlite/microprotocols.c b/Modules/_sqlite/microprotocols.c
index 697d1a788a3b0..a79f0067b17e8 100644
--- a/Modules/_sqlite/microprotocols.c
+++ b/Modules/_sqlite/microprotocols.c
@@ -23,8 +23,6 @@
  * 3. This notice may not be removed or altered from any source distribution.
  */
 
-#define NEEDS_PY_IDENTIFIER
-
 #include <Python.h>
 
 #include "cursor.h"
@@ -76,8 +74,6 @@ PyObject *
 pysqlite_microprotocols_adapt(pysqlite_state *state, PyObject *obj,
                               PyObject *proto, PyObject *alt)
 {
-    _Py_IDENTIFIER(__adapt__);
-    _Py_IDENTIFIER(__conform__);
     PyObject *adapter, *key, *adapted;
 
     /* 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,
     }
 
     /* try to have the protocol adapt this object */
-    if (_PyObject_LookupAttrId(proto, &PyId___adapt__, &adapter) < 0) {
+    if (_PyObject_LookupAttr(proto, state->str___adapt__, &adapter) < 0) {
         return NULL;
     }
     if (adapter) {
@@ -121,7 +117,7 @@ pysqlite_microprotocols_adapt(pysqlite_state *state, PyObject *obj,
     }
 
     /* and finally try to have the object adapt itself */
-    if (_PyObject_LookupAttrId(obj, &PyId___conform__, &adapter) < 0) {
+    if (_PyObject_LookupAttr(obj, state->str___conform__, &adapter) < 0) {
         return NULL;
     }
     if (adapter) {
diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c
index cfa4b970e7320..35cdcbe0a5c38 100644
--- a/Modules/_sqlite/module.c
+++ b/Modules/_sqlite/module.c
@@ -21,8 +21,6 @@
  * 3. This notice may not be removed or altered from any source distribution.
  */
 
-#define NEEDS_PY_IDENTIFIER
-
 #include "connection.h"
 #include "statement.h"
 #include "cursor.h"
@@ -185,15 +183,14 @@ pysqlite_register_converter_impl(PyObject *module, PyObject *orig_name,
 {
     PyObject* name = NULL;
     PyObject* retval = NULL;
-    _Py_IDENTIFIER(upper);
 
     /* convert the name to upper case */
-    name = _PyObject_CallMethodIdNoArgs(orig_name, &PyId_upper);
+    pysqlite_state *state = pysqlite_get_state(module);
+    name = PyObject_CallMethodNoArgs(orig_name, state->str_upper);
     if (!name) {
         goto error;
     }
 
-    pysqlite_state *state = pysqlite_get_state(module);
     if (PyDict_SetItem(state->converters, name, callable) != 0) {
         goto error;
     }
@@ -593,6 +590,13 @@ module_traverse(PyObject *module, visitproc visit, void *arg)
     Py_VISIT(state->lru_cache);
     Py_VISIT(state->psyco_adapters);
 
+    // Interned strings
+    Py_VISIT(state->str___adapt__);
+    Py_VISIT(state->str___conform__);
+    Py_VISIT(state->str_executescript);
+    Py_VISIT(state->str_finalize);
+    Py_VISIT(state->str_upper);
+
     return 0;
 }
 
@@ -625,6 +629,13 @@ module_clear(PyObject *module)
     Py_CLEAR(state->lru_cache);
     Py_CLEAR(state->psyco_adapters);
 
+    // Interned strings
+    Py_CLEAR(state->str___adapt__);
+    Py_CLEAR(state->str___conform__);
+    Py_CLEAR(state->str_executescript);
+    Py_CLEAR(state->str_finalize);
+    Py_CLEAR(state->str_upper);
+
     return 0;
 }
 
@@ -650,6 +661,15 @@ do {                                                                   \
     ADD_TYPE(module, (PyTypeObject *)state->exc);                      \
 } while (0)
 
+#define ADD_INTERNED(state, string)                      \
+do {                                                     \
+    PyObject *tmp = PyUnicode_InternFromString(#string); \
+    if (tmp == NULL) {                                   \
+        goto error;                                      \
+    }                                                    \
+    state->str_ ## string = tmp;                         \
+} while (0)
+
 static int
 module_exec(PyObject *module)
 {
@@ -695,6 +715,13 @@ module_exec(PyObject *module)
     ADD_EXCEPTION(module, state, DataError, state->DatabaseError);
     ADD_EXCEPTION(module, state, NotSupportedError, state->DatabaseError);
 
+    /* Add interned strings */
+    ADD_INTERNED(state, __adapt__);
+    ADD_INTERNED(state, __conform__);
+    ADD_INTERNED(state, executescript);
+    ADD_INTERNED(state, finalize);
+    ADD_INTERNED(state, upper);
+
     /* Set error constants */
     if (add_error_constants(module) < 0) {
         goto error;
diff --git a/Modules/_sqlite/module.h b/Modules/_sqlite/module.h
index a248d044af8fd..35c6f38552606 100644
--- a/Modules/_sqlite/module.h
+++ b/Modules/_sqlite/module.h
@@ -58,6 +58,13 @@ typedef struct {
     PyTypeObject *PrepareProtocolType;
     PyTypeObject *RowType;
     PyTypeObject *StatementType;
+
+    /* Pointers to interned strings */
+    PyObject *str___adapt__;
+    PyObject *str___conform__;
+    PyObject *str_executescript;
+    PyObject *str_finalize;
+    PyObject *str_upper;
 } pysqlite_state;
 
 extern pysqlite_state pysqlite_global_state;



More information about the Python-checkins mailing list