Skip to content

Commit d85fcc3

Browse files
pythongh-114569: Use PyMem_* APIs for non-PyObjects in Modules/
1 parent 841eacd commit d85fcc3

File tree

5 files changed

+17
-17
lines changed

5 files changed

+17
-17
lines changed

Modules/_elementtree.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ typedef struct {
267267
LOCAL(int)
268268
create_extra(ElementObject* self, PyObject* attrib)
269269
{
270-
self->extra = PyObject_Malloc(sizeof(ElementObjectExtra));
270+
self->extra = PyMem_Malloc(sizeof(ElementObjectExtra));
271271
if (!self->extra) {
272272
PyErr_NoMemory();
273273
return -1;
@@ -296,9 +296,9 @@ dealloc_extra(ElementObjectExtra *extra)
296296
Py_DECREF(extra->children[i]);
297297

298298
if (extra->children != extra->_children)
299-
PyObject_Free(extra->children);
299+
PyMem_Free(extra->children);
300300

301-
PyObject_Free(extra);
301+
PyMem_Free(extra);
302302
}
303303

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

30463046
static XML_Memory_Handling_Suite ExpatMemoryHandler = {
3047-
PyObject_Malloc, PyObject_Realloc, PyObject_Free};
3047+
PyMem_Malloc, PyMem_Realloc, PyMem_Free};
30483048

30493049
typedef struct {
30503050
PyObject_HEAD

Modules/_sre/sre_lib.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
11221122
/* install new repeat context */
11231123
/* TODO(https://github.com/python/cpython/issues/67877): Fix this
11241124
* potential memory leak. */
1125-
ctx->u.rep = (SRE_REPEAT*) PyObject_Malloc(sizeof(*ctx->u.rep));
1125+
ctx->u.rep = (SRE_REPEAT*) PyMem_Malloc(sizeof(*ctx->u.rep));
11261126
if (!ctx->u.rep) {
11271127
PyErr_NoMemory();
11281128
RETURN_FAILURE;
@@ -1136,7 +1136,7 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
11361136
state->ptr = ptr;
11371137
DO_JUMP(JUMP_REPEAT, jump_repeat, pattern+pattern[0]);
11381138
state->repeat = ctx->u.rep->prev;
1139-
PyObject_Free(ctx->u.rep);
1139+
PyMem_Free(ctx->u.rep);
11401140

11411141
if (ret) {
11421142
RETURN_ON_ERROR(ret);

Modules/mathmodule.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2570,7 +2570,7 @@ math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
25702570
goto error_exit;
25712571
}
25722572
if (n > NUM_STACK_ELEMS) {
2573-
diffs = (double *) PyObject_Malloc(n * sizeof(double));
2573+
diffs = (double *) PyMem_Malloc(n * sizeof(double));
25742574
if (diffs == NULL) {
25752575
PyErr_NoMemory();
25762576
goto error_exit;
@@ -2590,7 +2590,7 @@ math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
25902590
}
25912591
result = vector_norm(n, diffs, max, found_nan);
25922592
if (diffs != diffs_on_stack) {
2593-
PyObject_Free(diffs);
2593+
PyMem_Free(diffs);
25942594
}
25952595
if (p_allocated) {
25962596
Py_DECREF(p);
@@ -2602,7 +2602,7 @@ math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
26022602

26032603
error_exit:
26042604
if (diffs != diffs_on_stack) {
2605-
PyObject_Free(diffs);
2605+
PyMem_Free(diffs);
26062606
}
26072607
if (p_allocated) {
26082608
Py_DECREF(p);
@@ -2626,7 +2626,7 @@ math_hypot(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
26262626
double *coordinates = coord_on_stack;
26272627

26282628
if (nargs > NUM_STACK_ELEMS) {
2629-
coordinates = (double *) PyObject_Malloc(nargs * sizeof(double));
2629+
coordinates = (double *) PyMem_Malloc(nargs * sizeof(double));
26302630
if (coordinates == NULL) {
26312631
return PyErr_NoMemory();
26322632
}
@@ -2643,13 +2643,13 @@ math_hypot(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
26432643
}
26442644
result = vector_norm(nargs, coordinates, max, found_nan);
26452645
if (coordinates != coord_on_stack) {
2646-
PyObject_Free(coordinates);
2646+
PyMem_Free(coordinates);
26472647
}
26482648
return PyFloat_FromDouble(result);
26492649

26502650
error_exit:
26512651
if (coordinates != coord_on_stack) {
2652-
PyObject_Free(coordinates);
2652+
PyMem_Free(coordinates);
26532653
}
26542654
return NULL;
26552655
}

Modules/pyexpat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module pyexpat
2121
#define XML_COMBINED_VERSION (10000*XML_MAJOR_VERSION+100*XML_MINOR_VERSION+XML_MICRO_VERSION)
2222

2323
static XML_Memory_Handling_Suite ExpatMemoryHandler = {
24-
PyObject_Malloc, PyObject_Realloc, PyObject_Free};
24+
PyMem_Malloc, PyMem_Realloc, PyMem_Free};
2525

2626
enum HandlerTypes {
2727
StartElement,

Parser/lexer/lexer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ set_fstring_expr(struct tok_state* tok, struct token *token, char c) {
129129

130130
if (hash_detected) {
131131
Py_ssize_t input_length = tok_mode->last_expr_size - tok_mode->last_expr_end;
132-
char *result = (char *)PyObject_Malloc((input_length + 1) * sizeof(char));
132+
char *result = (char *)PyMem_Malloc((input_length + 1) * sizeof(char));
133133
if (!result) {
134134
return -1;
135135
}
@@ -154,7 +154,7 @@ set_fstring_expr(struct tok_state* tok, struct token *token, char c) {
154154

155155
result[j] = '\0'; // Null-terminate the result string
156156
res = PyUnicode_DecodeUTF8(result, j, NULL);
157-
PyObject_Free(result);
157+
PyMem_Free(result);
158158
} else {
159159
res = PyUnicode_DecodeUTF8(
160160
tok_mode->last_expr_buffer,

0 commit comments

Comments
 (0)