#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)
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);
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);
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