Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-90699: Use module state to access insert str object. #91693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 49 additions & 3 deletions Modules/_bisectmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down