@@ -13,6 +13,18 @@ module _bisect
13
13
14
14
#include "clinic/_bisectmodule.c.h"
15
15
16
+ typedef struct {
17
+ PyObject * str_insert ;
18
+ } bisect_state ;
19
+
20
+ static inline bisect_state *
21
+ get_bisect_state (PyObject * module )
22
+ {
23
+ void * state = PyModule_GetState (module );
24
+ assert (state != NULL );
25
+ return (bisect_state * )state ;
26
+ }
27
+
16
28
static inline Py_ssize_t
17
29
internal_bisect_right (PyObject * list , PyObject * item , Py_ssize_t lo , Py_ssize_t hi ,
18
30
PyObject * key )
@@ -129,7 +141,8 @@ _bisect_insort_right_impl(PyObject *module, PyObject *a, PyObject *x,
129
141
return NULL ;
130
142
}
131
143
else {
132
- result = PyObject_CallMethod (a , "insert" , "nO" , index , x );
144
+ bisect_state * state = get_bisect_state (module );
145
+ result = _PyObject_CallMethod (a , state -> str_insert , "nO" , index , x );
133
146
if (result == NULL )
134
147
return NULL ;
135
148
Py_DECREF (result );
@@ -255,7 +268,8 @@ _bisect_insort_left_impl(PyObject *module, PyObject *a, PyObject *x,
255
268
if (PyList_Insert (a , index , x ) < 0 )
256
269
return NULL ;
257
270
} else {
258
- result = PyObject_CallMethod (a , "insert" , "nO" , index , x );
271
+ bisect_state * state = get_bisect_state (module );
272
+ result = _PyObject_CallMethod (a , state -> str_insert , "nO" , index , x );
259
273
if (result == NULL )
260
274
return NULL ;
261
275
Py_DECREF (result );
@@ -280,13 +294,45 @@ having to sort the list after each insertion. For long lists of items with\n\
280
294
expensive comparison operations, this can be an improvement over the more\n\
281
295
common approach.\n" );
282
296
297
+ static int
298
+ bisect_clear (PyObject * module )
299
+ {
300
+ bisect_state * state = get_bisect_state (module );
301
+ Py_CLEAR (state -> str_insert );
302
+ return 0 ;
303
+ }
304
+
305
+ static void
306
+ bisect_free (void * module )
307
+ {
308
+ bisect_clear ((PyObject * )module );
309
+ }
310
+
311
+ static int
312
+ bisect_modexec (PyObject * m )
313
+ {
314
+ bisect_state * state = get_bisect_state (m );
315
+ state -> str_insert = PyUnicode_InternFromString ("insert" );
316
+ if (state -> str_insert == NULL ) {
317
+ return -1 ;
318
+ }
319
+ return 0 ;
320
+ }
321
+
322
+ static PyModuleDef_Slot bisect_slots [] = {
323
+ {Py_mod_exec , bisect_modexec },
324
+ {0 , NULL }
325
+ };
283
326
284
327
static struct PyModuleDef _bisectmodule = {
285
328
PyModuleDef_HEAD_INIT ,
286
329
.m_name = "_bisect" ,
330
+ .m_size = sizeof (bisect_state ),
287
331
.m_doc = module_doc ,
288
332
.m_methods = bisect_methods ,
289
- .m_size = 0
333
+ .m_slots = bisect_slots ,
334
+ .m_clear = bisect_clear ,
335
+ .m_free = bisect_free ,
290
336
};
291
337
292
338
PyMODINIT_FUNC
0 commit comments