Skip to content

Commit

Permalink
pythongh-114569: Use PyMem_* APIs for non-PyObjects in Modules/
Browse files Browse the repository at this point in the history
  • Loading branch information
erlend-aasland committed Jan 25, 2024
1 parent 841eacd commit d85fcc3
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
12 changes: 6 additions & 6 deletions Modules/_elementtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ typedef struct {
LOCAL(int)
create_extra(ElementObject* self, PyObject* attrib)
{
self->extra = PyObject_Malloc(sizeof(ElementObjectExtra));
self->extra = PyMem_Malloc(sizeof(ElementObjectExtra));
if (!self->extra) {
PyErr_NoMemory();
return -1;
Expand Down Expand Up @@ -296,9 +296,9 @@ dealloc_extra(ElementObjectExtra *extra)
Py_DECREF(extra->children[i]);

if (extra->children != extra->_children)
PyObject_Free(extra->children);
PyMem_Free(extra->children);

PyObject_Free(extra);
PyMem_Free(extra);
}

LOCAL(void)
Expand Down Expand Up @@ -495,12 +495,12 @@ element_resize(ElementObject* self, Py_ssize_t extra)
* "children", which needs at least 4 bytes. Although it's a
* false alarm always assume at least one child to be safe.
*/
children = PyObject_Realloc(self->extra->children,
children = PyMem_Realloc(self->extra->children,
size * sizeof(PyObject*));
if (!children)
goto nomemory;
} else {
children = PyObject_Malloc(size * sizeof(PyObject*));
children = PyMem_Malloc(size * sizeof(PyObject*));
if (!children)
goto nomemory;
/* copy existing children from static area to malloc buffer */
Expand Down Expand Up @@ -3044,7 +3044,7 @@ _elementtree_TreeBuilder_start_impl(TreeBuilderObject *self, PyObject *tag,
#define EXPAT(st, func) ((st)->expat_capi->func)

static XML_Memory_Handling_Suite ExpatMemoryHandler = {
PyObject_Malloc, PyObject_Realloc, PyObject_Free};
PyMem_Malloc, PyMem_Realloc, PyMem_Free};

typedef struct {
PyObject_HEAD
Expand Down
4 changes: 2 additions & 2 deletions Modules/_sre/sre_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
/* install new repeat context */
/* TODO(https://github.com/python/cpython/issues/67877): Fix this
* potential memory leak. */
ctx->u.rep = (SRE_REPEAT*) PyObject_Malloc(sizeof(*ctx->u.rep));
ctx->u.rep = (SRE_REPEAT*) PyMem_Malloc(sizeof(*ctx->u.rep));
if (!ctx->u.rep) {
PyErr_NoMemory();
RETURN_FAILURE;
Expand All @@ -1136,7 +1136,7 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
state->ptr = ptr;
DO_JUMP(JUMP_REPEAT, jump_repeat, pattern+pattern[0]);
state->repeat = ctx->u.rep->prev;
PyObject_Free(ctx->u.rep);
PyMem_Free(ctx->u.rep);

if (ret) {
RETURN_ON_ERROR(ret);
Expand Down
12 changes: 6 additions & 6 deletions Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2570,7 +2570,7 @@ math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
goto error_exit;
}
if (n > NUM_STACK_ELEMS) {
diffs = (double *) PyObject_Malloc(n * sizeof(double));
diffs = (double *) PyMem_Malloc(n * sizeof(double));
if (diffs == NULL) {
PyErr_NoMemory();
goto error_exit;
Expand All @@ -2590,7 +2590,7 @@ math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
}
result = vector_norm(n, diffs, max, found_nan);
if (diffs != diffs_on_stack) {
PyObject_Free(diffs);
PyMem_Free(diffs);
}
if (p_allocated) {
Py_DECREF(p);
Expand All @@ -2602,7 +2602,7 @@ math_dist_impl(PyObject *module, PyObject *p, PyObject *q)

error_exit:
if (diffs != diffs_on_stack) {
PyObject_Free(diffs);
PyMem_Free(diffs);
}
if (p_allocated) {
Py_DECREF(p);
Expand All @@ -2626,7 +2626,7 @@ math_hypot(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
double *coordinates = coord_on_stack;

if (nargs > NUM_STACK_ELEMS) {
coordinates = (double *) PyObject_Malloc(nargs * sizeof(double));
coordinates = (double *) PyMem_Malloc(nargs * sizeof(double));
if (coordinates == NULL) {
return PyErr_NoMemory();
}
Expand All @@ -2643,13 +2643,13 @@ math_hypot(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
}
result = vector_norm(nargs, coordinates, max, found_nan);
if (coordinates != coord_on_stack) {
PyObject_Free(coordinates);
PyMem_Free(coordinates);
}
return PyFloat_FromDouble(result);

error_exit:
if (coordinates != coord_on_stack) {
PyObject_Free(coordinates);
PyMem_Free(coordinates);
}
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/pyexpat.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module pyexpat
#define XML_COMBINED_VERSION (10000*XML_MAJOR_VERSION+100*XML_MINOR_VERSION+XML_MICRO_VERSION)

static XML_Memory_Handling_Suite ExpatMemoryHandler = {
PyObject_Malloc, PyObject_Realloc, PyObject_Free};
PyMem_Malloc, PyMem_Realloc, PyMem_Free};

enum HandlerTypes {
StartElement,
Expand Down
4 changes: 2 additions & 2 deletions Parser/lexer/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ set_fstring_expr(struct tok_state* tok, struct token *token, char c) {

if (hash_detected) {
Py_ssize_t input_length = tok_mode->last_expr_size - tok_mode->last_expr_end;
char *result = (char *)PyObject_Malloc((input_length + 1) * sizeof(char));
char *result = (char *)PyMem_Malloc((input_length + 1) * sizeof(char));
if (!result) {
return -1;
}
Expand All @@ -154,7 +154,7 @@ set_fstring_expr(struct tok_state* tok, struct token *token, char c) {

result[j] = '\0'; // Null-terminate the result string
res = PyUnicode_DecodeUTF8(result, j, NULL);
PyObject_Free(result);
PyMem_Free(result);
} else {
res = PyUnicode_DecodeUTF8(
tok_mode->last_expr_buffer,
Expand Down

0 comments on commit d85fcc3

Please sign in to comment.