Skip to content

Commit

Permalink
pythongh-114569: Use PyMem_* APIs for non-PyObjects in compiler (pyth…
Browse files Browse the repository at this point in the history
  • Loading branch information
erlend-aasland authored and aisk committed Feb 11, 2024
1 parent df08858 commit a31bc26
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
25 changes: 12 additions & 13 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ _PyCompile_EnsureArrayLargeEnough(int idx, void **array, int *alloc,
if (idx >= new_alloc) {
new_alloc = idx + default_alloc;
}
arr = PyObject_Calloc(new_alloc, item_size);
arr = PyMem_Calloc(new_alloc, item_size);
if (arr == NULL) {
PyErr_NoMemory();
return ERROR;
Expand All @@ -181,7 +181,7 @@ _PyCompile_EnsureArrayLargeEnough(int idx, void **array, int *alloc,
}

assert(newsize > 0);
void *tmp = PyObject_Realloc(arr, newsize);
void *tmp = PyMem_Realloc(arr, newsize);
if (tmp == NULL) {
PyErr_NoMemory();
return ERROR;
Expand Down Expand Up @@ -282,10 +282,10 @@ instr_sequence_insert_instruction(instr_sequence *seq, int pos,

static void
instr_sequence_fini(instr_sequence *seq) {
PyObject_Free(seq->s_labelmap);
PyMem_Free(seq->s_labelmap);
seq->s_labelmap = NULL;

PyObject_Free(seq->s_instrs);
PyMem_Free(seq->s_instrs);
seq->s_instrs = NULL;
}

Expand Down Expand Up @@ -690,7 +690,7 @@ compiler_unit_free(struct compiler_unit *u)
Py_CLEAR(u->u_metadata.u_cellvars);
Py_CLEAR(u->u_metadata.u_fasthidden);
Py_CLEAR(u->u_private);
PyObject_Free(u);
PyMem_Free(u);
}

static int
Expand Down Expand Up @@ -1262,8 +1262,7 @@ compiler_enter_scope(struct compiler *c, identifier name,

struct compiler_unit *u;

u = (struct compiler_unit *)PyObject_Calloc(1, sizeof(
struct compiler_unit));
u = (struct compiler_unit *)PyMem_Calloc(1, sizeof(struct compiler_unit));
if (!u) {
PyErr_NoMemory();
return ERROR;
Expand Down Expand Up @@ -6657,7 +6656,7 @@ ensure_fail_pop(struct compiler *c, pattern_context *pc, Py_ssize_t n)
return SUCCESS;
}
Py_ssize_t needed = sizeof(jump_target_label) * size;
jump_target_label *resized = PyObject_Realloc(pc->fail_pop, needed);
jump_target_label *resized = PyMem_Realloc(pc->fail_pop, needed);
if (resized == NULL) {
PyErr_NoMemory();
return ERROR;
Expand Down Expand Up @@ -6696,13 +6695,13 @@ emit_and_reset_fail_pop(struct compiler *c, location loc,
USE_LABEL(c, pc->fail_pop[pc->fail_pop_size]);
if (codegen_addop_noarg(INSTR_SEQUENCE(c), POP_TOP, loc) < 0) {
pc->fail_pop_size = 0;
PyObject_Free(pc->fail_pop);
PyMem_Free(pc->fail_pop);
pc->fail_pop = NULL;
return ERROR;
}
}
USE_LABEL(c, pc->fail_pop[0]);
PyObject_Free(pc->fail_pop);
PyMem_Free(pc->fail_pop);
pc->fail_pop = NULL;
return SUCCESS;
}
Expand Down Expand Up @@ -7206,7 +7205,7 @@ compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
Py_DECREF(pc->stores);
*pc = old_pc;
Py_INCREF(pc->stores);
// Need to NULL this for the PyObject_Free call in the error block.
// Need to NULL this for the PyMem_Free call in the error block.
old_pc.fail_pop = NULL;
// No match. Pop the remaining copy of the subject and fail:
if (codegen_addop_noarg(INSTR_SEQUENCE(c), POP_TOP, LOC(p)) < 0 ||
Expand Down Expand Up @@ -7252,7 +7251,7 @@ compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
diff:
compiler_error(c, LOC(p), "alternative patterns bind different names");
error:
PyObject_Free(old_pc.fail_pop);
PyMem_Free(old_pc.fail_pop);
Py_DECREF(old_pc.stores);
Py_XDECREF(control);
return ERROR;
Expand Down Expand Up @@ -7453,7 +7452,7 @@ compiler_match(struct compiler *c, stmt_ty s)
pattern_context pc;
pc.fail_pop = NULL;
int result = compiler_match_inner(c, s, &pc);
PyObject_Free(pc.fail_pop);
PyMem_Free(pc.fail_pop);
return result;
}

Expand Down
6 changes: 3 additions & 3 deletions Python/flowgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ basicblock_last_instr(const basicblock *b) {
static basicblock *
cfg_builder_new_block(cfg_builder *g)
{
basicblock *b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock));
basicblock *b = (basicblock *)PyMem_Calloc(1, sizeof(basicblock));
if (b == NULL) {
PyErr_NoMemory();
return NULL;
Expand Down Expand Up @@ -437,10 +437,10 @@ _PyCfgBuilder_Free(cfg_builder *g)
basicblock *b = g->g_block_list;
while (b != NULL) {
if (b->b_instr) {
PyObject_Free((void *)b->b_instr);
PyMem_Free((void *)b->b_instr);
}
basicblock *next = b->b_list;
PyObject_Free((void *)b);
PyMem_Free((void *)b);
b = next;
}
PyMem_Free(g);
Expand Down

0 comments on commit a31bc26

Please sign in to comment.