summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDong-hee Na <donghee.na@python.org>2022-04-19 13:12:46 (GMT)
committerGitHub <noreply@github.com>2022-04-19 13:12:46 (GMT)
commit16fc5733b737320e43fe3244bf4be4e6c3b794a5 (patch)
tree62bb46295fe943e1b9f9e6e4acd3361f1d96af19
parent6a7a8a740e61508cb5a0fcdac8b752b6c9e5d1ea (diff)
downloadcpython-16fc5733b737320e43fe3244bf4be4e6c3b794a5.zip
cpython-16fc5733b737320e43fe3244bf4be4e6c3b794a5.tar.gz
cpython-16fc5733b737320e43fe3244bf4be4e6c3b794a5.tar.bz2
gh-90699: Use module state to access insert str object. (GH-91693)
-rw-r--r--Modules/_bisectmodule.c52
1 files changed, 49 insertions, 3 deletions
diff --git a/Modules/_bisectmodule.c b/Modules/_bisectmodule.c
index c54dc49..19e9cd2 100644
--- a/Modules/_bisectmodule.c
+++ b/Modules/_bisectmodule.c
@@ -13,6 +13,18 @@ module _bisect
#include "clinic/_bisectmodule.c.h"
+typedef struct {
+ PyObject *str_insert;
+} bisect_state;
+
+static inline bisect_state*
+get_bisect_state(PyObject *module)
+{
+ void *state = PyModule_GetState(module);
+ assert(state != NULL);
+ return (bisect_state *)state;
+}
+
static inline Py_ssize_t
internal_bisect_right(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t hi,
PyObject* key)
@@ -129,7 +141,8 @@ _bisect_insort_right_impl(PyObject *module, PyObject *a, PyObject *x,
return NULL;
}
else {
- result = PyObject_CallMethod(a, "insert", "nO", index, x);
+ bisect_state *state = get_bisect_state(module);
+ result = _PyObject_CallMethod(a, state->str_insert, "nO", index, x);
if (result == NULL)
return NULL;
Py_DECREF(result);
@@ -255,7 +268,8 @@ _bisect_insort_left_impl(PyObject *module, PyObject *a, PyObject *x,
if (PyList_Insert(a, index, x) < 0)
return NULL;
} else {
- result = PyObject_CallMethod(a, "insert", "nO", index, x);
+ bisect_state *state = get_bisect_state(module);
+ result = _PyObject_CallMethod(a, state->str_insert, "nO", index, x);
if (result == NULL)
return NULL;
Py_DECREF(result);
@@ -280,13 +294,45 @@ having to sort the list after each insertion. For long lists of items with\n\
expensive comparison operations, this can be an improvement over the more\n\
common approach.\n");
+static int
+bisect_clear(PyObject *module)
+{
+ bisect_state *state = get_bisect_state(module);
+ Py_CLEAR(state->str_insert);
+ return 0;
+}
+
+static void
+bisect_free(void *module)
+{
+ bisect_clear((PyObject *)module);
+}
+
+static int
+bisect_modexec(PyObject *m)
+{
+ bisect_state *state = get_bisect_state(m);
+ state->str_insert = PyUnicode_InternFromString("insert");
+ if (state->str_insert == NULL) {
+ return -1;
+ }
+ return 0;
+}
+
+static PyModuleDef_Slot bisect_slots[] = {
+ {Py_mod_exec, bisect_modexec},
+ {0, NULL}
+};
static struct PyModuleDef _bisectmodule = {
PyModuleDef_HEAD_INIT,
.m_name = "_bisect",
+ .m_size = sizeof(bisect_state),
.m_doc = module_doc,
.m_methods = bisect_methods,
- .m_size = 0
+ .m_slots = bisect_slots,
+ .m_clear = bisect_clear,
+ .m_free = bisect_free,
};
PyMODINIT_FUNC