Skip to content

Commit a31bc26

Browse files
erlend-aaslandaisk
authored andcommitted
pythongh-114569: Use PyMem_* APIs for non-PyObjects in compiler (python#114587)
1 parent df08858 commit a31bc26

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

Python/compile.c

+12-13
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ _PyCompile_EnsureArrayLargeEnough(int idx, void **array, int *alloc,
160160
if (idx >= new_alloc) {
161161
new_alloc = idx + default_alloc;
162162
}
163-
arr = PyObject_Calloc(new_alloc, item_size);
163+
arr = PyMem_Calloc(new_alloc, item_size);
164164
if (arr == NULL) {
165165
PyErr_NoMemory();
166166
return ERROR;
@@ -181,7 +181,7 @@ _PyCompile_EnsureArrayLargeEnough(int idx, void **array, int *alloc,
181181
}
182182

183183
assert(newsize > 0);
184-
void *tmp = PyObject_Realloc(arr, newsize);
184+
void *tmp = PyMem_Realloc(arr, newsize);
185185
if (tmp == NULL) {
186186
PyErr_NoMemory();
187187
return ERROR;
@@ -282,10 +282,10 @@ instr_sequence_insert_instruction(instr_sequence *seq, int pos,
282282

283283
static void
284284
instr_sequence_fini(instr_sequence *seq) {
285-
PyObject_Free(seq->s_labelmap);
285+
PyMem_Free(seq->s_labelmap);
286286
seq->s_labelmap = NULL;
287287

288-
PyObject_Free(seq->s_instrs);
288+
PyMem_Free(seq->s_instrs);
289289
seq->s_instrs = NULL;
290290
}
291291

@@ -690,7 +690,7 @@ compiler_unit_free(struct compiler_unit *u)
690690
Py_CLEAR(u->u_metadata.u_cellvars);
691691
Py_CLEAR(u->u_metadata.u_fasthidden);
692692
Py_CLEAR(u->u_private);
693-
PyObject_Free(u);
693+
PyMem_Free(u);
694694
}
695695

696696
static int
@@ -1262,8 +1262,7 @@ compiler_enter_scope(struct compiler *c, identifier name,
12621262

12631263
struct compiler_unit *u;
12641264

1265-
u = (struct compiler_unit *)PyObject_Calloc(1, sizeof(
1266-
struct compiler_unit));
1265+
u = (struct compiler_unit *)PyMem_Calloc(1, sizeof(struct compiler_unit));
12671266
if (!u) {
12681267
PyErr_NoMemory();
12691268
return ERROR;
@@ -6657,7 +6656,7 @@ ensure_fail_pop(struct compiler *c, pattern_context *pc, Py_ssize_t n)
66576656
return SUCCESS;
66586657
}
66596658
Py_ssize_t needed = sizeof(jump_target_label) * size;
6660-
jump_target_label *resized = PyObject_Realloc(pc->fail_pop, needed);
6659+
jump_target_label *resized = PyMem_Realloc(pc->fail_pop, needed);
66616660
if (resized == NULL) {
66626661
PyErr_NoMemory();
66636662
return ERROR;
@@ -6696,13 +6695,13 @@ emit_and_reset_fail_pop(struct compiler *c, location loc,
66966695
USE_LABEL(c, pc->fail_pop[pc->fail_pop_size]);
66976696
if (codegen_addop_noarg(INSTR_SEQUENCE(c), POP_TOP, loc) < 0) {
66986697
pc->fail_pop_size = 0;
6699-
PyObject_Free(pc->fail_pop);
6698+
PyMem_Free(pc->fail_pop);
67006699
pc->fail_pop = NULL;
67016700
return ERROR;
67026701
}
67036702
}
67046703
USE_LABEL(c, pc->fail_pop[0]);
6705-
PyObject_Free(pc->fail_pop);
6704+
PyMem_Free(pc->fail_pop);
67066705
pc->fail_pop = NULL;
67076706
return SUCCESS;
67086707
}
@@ -7206,7 +7205,7 @@ compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
72067205
Py_DECREF(pc->stores);
72077206
*pc = old_pc;
72087207
Py_INCREF(pc->stores);
7209-
// Need to NULL this for the PyObject_Free call in the error block.
7208+
// Need to NULL this for the PyMem_Free call in the error block.
72107209
old_pc.fail_pop = NULL;
72117210
// No match. Pop the remaining copy of the subject and fail:
72127211
if (codegen_addop_noarg(INSTR_SEQUENCE(c), POP_TOP, LOC(p)) < 0 ||
@@ -7252,7 +7251,7 @@ compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
72527251
diff:
72537252
compiler_error(c, LOC(p), "alternative patterns bind different names");
72547253
error:
7255-
PyObject_Free(old_pc.fail_pop);
7254+
PyMem_Free(old_pc.fail_pop);
72567255
Py_DECREF(old_pc.stores);
72577256
Py_XDECREF(control);
72587257
return ERROR;
@@ -7453,7 +7452,7 @@ compiler_match(struct compiler *c, stmt_ty s)
74537452
pattern_context pc;
74547453
pc.fail_pop = NULL;
74557454
int result = compiler_match_inner(c, s, &pc);
7456-
PyObject_Free(pc.fail_pop);
7455+
PyMem_Free(pc.fail_pop);
74577456
return result;
74587457
}
74597458

Python/flowgraph.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ basicblock_last_instr(const basicblock *b) {
162162
static basicblock *
163163
cfg_builder_new_block(cfg_builder *g)
164164
{
165-
basicblock *b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock));
165+
basicblock *b = (basicblock *)PyMem_Calloc(1, sizeof(basicblock));
166166
if (b == NULL) {
167167
PyErr_NoMemory();
168168
return NULL;
@@ -437,10 +437,10 @@ _PyCfgBuilder_Free(cfg_builder *g)
437437
basicblock *b = g->g_block_list;
438438
while (b != NULL) {
439439
if (b->b_instr) {
440-
PyObject_Free((void *)b->b_instr);
440+
PyMem_Free((void *)b->b_instr);
441441
}
442442
basicblock *next = b->b_list;
443-
PyObject_Free((void *)b);
443+
PyMem_Free((void *)b);
444444
b = next;
445445
}
446446
PyMem_Free(g);

0 commit comments

Comments
 (0)