Skip to content

bpo-41798: Allocate _decimal extension module CAPI on the heap #24117

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 5 commits into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion Include/pydecimal.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ typedef struct {
/* Capsule API */
/****************************************************************************/

#define PyDec_CAPSULE_NAME "_decimal._API"

/* Simple API */
#define PyDec_TypeCheck_INDEX 0
#define PyDec_TypeCheck_RETURN int
Expand Down Expand Up @@ -164,7 +166,7 @@ static void **_decimal_api;
static int
import_decimal(void)
{
_decimal_api = (void **)PyCapsule_Import("_decimal._API", 0);
_decimal_api = (void **)PyCapsule_Import(PyDec_CAPSULE_NAME, 0);
if (_decimal_api == NULL) {
return -1;
}
Expand Down
25 changes: 20 additions & 5 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -5574,8 +5574,6 @@ static PyTypeObject PyDecContext_Type =
/* C-API */
/****************************************************************************/

static void *_decimal_api[CPYTHON_DECIMAL_MAX_API];

/* Simple API */
static int
PyDec_TypeCheck(const PyObject *v)
Expand Down Expand Up @@ -5699,9 +5697,22 @@ PyDec_GetConst(const PyObject *v)
return MPD(v);
}

static void
destroy_api(PyObject *capsule)
{
void *capi = PyCapsule_GetPointer(capsule, PyDec_CAPSULE_NAME);
PyMem_Free(capi);
}

static PyObject *
init_api(void)
{
void **_decimal_api = PyMem_Calloc(CPYTHON_DECIMAL_MAX_API, sizeof(void *));
if (_decimal_api == NULL) {
PyErr_NoMemory();
return NULL;
}

/* Simple API */
_decimal_api[PyDec_TypeCheck_INDEX] = (void *)PyDec_TypeCheck;
_decimal_api[PyDec_IsSpecial_INDEX] = (void *)PyDec_IsSpecial;
Expand All @@ -5716,7 +5727,11 @@ init_api(void)
_decimal_api[PyDec_Get_INDEX] = (void *)PyDec_Get;
_decimal_api[PyDec_GetConst_INDEX] = (void *)PyDec_GetConst;

return PyCapsule_New(_decimal_api, "_decimal._API", NULL);
PyObject *capsule = PyCapsule_New(_decimal_api, PyDec_CAPSULE_NAME, destroy_api);
if (!capsule) {
PyMem_Free(_decimal_api);
}
return capsule;
}


Expand Down Expand Up @@ -6080,8 +6095,7 @@ PyInit__decimal(void)
CHECK_INT(PyModule_AddStringConstant(m, "__libmpdec_version__", mpd_version()));

/* Add capsule API */
Py_INCREF(capsule);
if (PyModule_AddObject(m, "_API", capsule) < 0) {
if (PyModule_AddObjectRef(m, "_API", capsule) < 0) {
goto error;
}

Expand All @@ -6107,6 +6121,7 @@ PyInit__decimal(void)
Py_CLEAR(basic_context_template); /* GCOV_NOT_REACHED */
Py_CLEAR(extended_context_template); /* GCOV_NOT_REACHED */
Py_CLEAR(m); /* GCOV_NOT_REACHED */
Py_CLEAR(capsule); /* GCOV_NOT_REACHED */

return NULL; /* GCOV_NOT_REACHED */
}
Expand Down