Skip to content

Commit 16a89a3

Browse files
erlend-aaslandGlyphack
authored andcommitted
pythongh-114569: Use PyMem_* APIs for most non-PyObject uses (python#114574)
Fix usage in Modules, Objects, and Parser subdirectories.
1 parent 7009078 commit 16a89a3

File tree

7 files changed

+36
-33
lines changed

7 files changed

+36
-33
lines changed

Modules/_elementtree.c

+13-10
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;
@@ -295,10 +295,11 @@ dealloc_extra(ElementObjectExtra *extra)
295295
for (i = 0; i < extra->length; i++)
296296
Py_DECREF(extra->children[i]);
297297

298-
if (extra->children != extra->_children)
299-
PyObject_Free(extra->children);
298+
if (extra->children != extra->_children) {
299+
PyMem_Free(extra->children);
300+
}
300301

301-
PyObject_Free(extra);
302+
PyMem_Free(extra);
302303
}
303304

304305
LOCAL(void)
@@ -495,14 +496,16 @@ element_resize(ElementObject* self, Py_ssize_t extra)
495496
* "children", which needs at least 4 bytes. Although it's a
496497
* false alarm always assume at least one child to be safe.
497498
*/
498-
children = PyObject_Realloc(self->extra->children,
499-
size * sizeof(PyObject*));
500-
if (!children)
499+
children = PyMem_Realloc(self->extra->children,
500+
size * sizeof(PyObject*));
501+
if (!children) {
501502
goto nomemory;
503+
}
502504
} else {
503-
children = PyObject_Malloc(size * sizeof(PyObject*));
504-
if (!children)
505+
children = PyMem_Malloc(size * sizeof(PyObject*));
506+
if (!children) {
505507
goto nomemory;
508+
}
506509
/* copy existing children from static area to malloc buffer */
507510
memcpy(children, self->extra->children,
508511
self->extra->length * sizeof(PyObject*));
@@ -3044,7 +3047,7 @@ _elementtree_TreeBuilder_start_impl(TreeBuilderObject *self, PyObject *tag,
30443047
#define EXPAT(st, func) ((st)->expat_capi->func)
30453048

30463049
static XML_Memory_Handling_Suite ExpatMemoryHandler = {
3047-
PyObject_Malloc, PyObject_Realloc, PyObject_Free};
3050+
PyMem_Malloc, PyMem_Realloc, PyMem_Free};
30483051

30493052
typedef struct {
30503053
PyObject_HEAD

Modules/_sre/sre_lib.h

+2-2
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

+6-6
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

+1-1
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,

Objects/bytearrayobject.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
132132
}
133133
else {
134134
alloc = size + 1;
135-
new->ob_bytes = PyObject_Malloc(alloc);
135+
new->ob_bytes = PyMem_Malloc(alloc);
136136
if (new->ob_bytes == NULL) {
137137
Py_DECREF(new);
138138
return PyErr_NoMemory();
@@ -221,17 +221,17 @@ PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
221221
}
222222

223223
if (logical_offset > 0) {
224-
sval = PyObject_Malloc(alloc);
224+
sval = PyMem_Malloc(alloc);
225225
if (sval == NULL) {
226226
PyErr_NoMemory();
227227
return -1;
228228
}
229229
memcpy(sval, PyByteArray_AS_STRING(self),
230230
Py_MIN((size_t)requested_size, (size_t)Py_SIZE(self)));
231-
PyObject_Free(obj->ob_bytes);
231+
PyMem_Free(obj->ob_bytes);
232232
}
233233
else {
234-
sval = PyObject_Realloc(obj->ob_bytes, alloc);
234+
sval = PyMem_Realloc(obj->ob_bytes, alloc);
235235
if (sval == NULL) {
236236
PyErr_NoMemory();
237237
return -1;
@@ -951,7 +951,7 @@ bytearray_repr(PyByteArrayObject *self)
951951
}
952952

953953
newsize += 6 + length * 4;
954-
buffer = PyObject_Malloc(newsize);
954+
buffer = PyMem_Malloc(newsize);
955955
if (buffer == NULL) {
956956
PyErr_NoMemory();
957957
return NULL;
@@ -1008,7 +1008,7 @@ bytearray_repr(PyByteArrayObject *self)
10081008
}
10091009

10101010
v = PyUnicode_FromStringAndSize(buffer, p - buffer);
1011-
PyObject_Free(buffer);
1011+
PyMem_Free(buffer);
10121012
return v;
10131013
}
10141014

@@ -1088,7 +1088,7 @@ bytearray_dealloc(PyByteArrayObject *self)
10881088
PyErr_Print();
10891089
}
10901090
if (self->ob_bytes != 0) {
1091-
PyObject_Free(self->ob_bytes);
1091+
PyMem_Free(self->ob_bytes);
10921092
}
10931093
Py_TYPE(self)->tp_free((PyObject *)self);
10941094
}

Objects/typeobject.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -3493,7 +3493,7 @@ type_new_set_doc(PyTypeObject *type)
34933493

34943494
// Silently truncate the docstring if it contains a null byte
34953495
Py_ssize_t size = strlen(doc_str) + 1;
3496-
char *tp_doc = (char *)PyObject_Malloc(size);
3496+
char *tp_doc = (char *)PyMem_Malloc(size);
34973497
if (tp_doc == NULL) {
34983498
PyErr_NoMemory();
34993499
return -1;
@@ -4166,12 +4166,12 @@ _PyType_FromMetaclass_impl(
41664166
goto finally;
41674167
}
41684168
if (slot->pfunc == NULL) {
4169-
PyObject_Free(tp_doc);
4169+
PyMem_Free(tp_doc);
41704170
tp_doc = NULL;
41714171
}
41724172
else {
41734173
size_t len = strlen(slot->pfunc)+1;
4174-
tp_doc = PyObject_Malloc(len);
4174+
tp_doc = PyMem_Malloc(len);
41754175
if (tp_doc == NULL) {
41764176
PyErr_NoMemory();
41774177
goto finally;
@@ -4501,7 +4501,7 @@ _PyType_FromMetaclass_impl(
45014501
Py_CLEAR(res);
45024502
}
45034503
Py_XDECREF(bases);
4504-
PyObject_Free(tp_doc);
4504+
PyMem_Free(tp_doc);
45054505
Py_XDECREF(ht_name);
45064506
PyMem_Free(_ht_tpname);
45074507
return (PyObject*)res;
@@ -5099,7 +5099,7 @@ type_dealloc(PyObject *self)
50995099
/* A type's tp_doc is heap allocated, unlike the tp_doc slots
51005100
* of most other objects. It's okay to cast it to char *.
51015101
*/
5102-
PyObject_Free((char *)type->tp_doc);
5102+
PyMem_Free((char *)type->tp_doc);
51035103

51045104
PyHeapTypeObject *et = (PyHeapTypeObject *)type;
51055105
Py_XDECREF(et->ht_name);

Parser/lexer/lexer.c

+2-2
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)