Skip to content

Commit 6dcbc24

Browse files
authored
bpo-41132: Use pymalloc allocator in the f-string parser (GH-21173)
1 parent 9cfcdb7 commit 6dcbc24

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

Parser/pegen.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
395395
const char *fstring_msg = "f-string: ";
396396
Py_ssize_t len = strlen(fstring_msg) + strlen(errmsg);
397397

398-
char *new_errmsg = PyMem_RawMalloc(len + 1); // Lengths of both strings plus NULL character
398+
char *new_errmsg = PyMem_Malloc(len + 1); // Lengths of both strings plus NULL character
399399
if (!new_errmsg) {
400400
return (void *) PyErr_NoMemory();
401401
}
@@ -443,15 +443,15 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
443443
Py_DECREF(errstr);
444444
Py_DECREF(value);
445445
if (p->start_rule == Py_fstring_input) {
446-
PyMem_RawFree((void *)errmsg);
446+
PyMem_Free((void *)errmsg);
447447
}
448448
return NULL;
449449

450450
error:
451451
Py_XDECREF(errstr);
452452
Py_XDECREF(error_line);
453453
if (p->start_rule == Py_fstring_input) {
454-
PyMem_RawFree((void *)errmsg);
454+
PyMem_Free((void *)errmsg);
455455
}
456456
return NULL;
457457
}

Parser/string_parser.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ fstring_compile_expr(Parser *p, const char *expr_start, const char *expr_end,
592592

593593
len = expr_end - expr_start;
594594
/* Allocate 3 extra bytes: open paren, close paren, null byte. */
595-
str = PyMem_RawMalloc(len + 3);
595+
str = PyMem_Malloc(len + 3);
596596
if (str == NULL) {
597597
PyErr_NoMemory();
598598
return NULL;
@@ -605,7 +605,7 @@ fstring_compile_expr(Parser *p, const char *expr_start, const char *expr_end,
605605

606606
struct tok_state* tok = PyTokenizer_FromString(str, 1);
607607
if (tok == NULL) {
608-
PyMem_RawFree(str);
608+
PyMem_Free(str);
609609
return NULL;
610610
}
611611
Py_INCREF(p->tok->filename);
@@ -631,7 +631,7 @@ fstring_compile_expr(Parser *p, const char *expr_start, const char *expr_end,
631631
result = expr;
632632

633633
exit:
634-
PyMem_RawFree(str);
634+
PyMem_Free(str);
635635
_PyPegen_Parser_Free(p2);
636636
PyTokenizer_Free(tok);
637637
return result;
@@ -1143,7 +1143,7 @@ ExprList_Append(ExprList *l, expr_ty exp)
11431143
Py_ssize_t i;
11441144
/* We're still using the cached data. Switch to
11451145
alloc-ing. */
1146-
l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
1146+
l->p = PyMem_Malloc(sizeof(expr_ty) * new_size);
11471147
if (!l->p) {
11481148
return -1;
11491149
}
@@ -1153,9 +1153,9 @@ ExprList_Append(ExprList *l, expr_ty exp)
11531153
}
11541154
} else {
11551155
/* Just realloc. */
1156-
expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
1156+
expr_ty *tmp = PyMem_Realloc(l->p, sizeof(expr_ty) * new_size);
11571157
if (!tmp) {
1158-
PyMem_RawFree(l->p);
1158+
PyMem_Free(l->p);
11591159
l->p = NULL;
11601160
return -1;
11611161
}
@@ -1183,7 +1183,7 @@ ExprList_Dealloc(ExprList *l)
11831183
/* Do nothing. */
11841184
} else {
11851185
/* We have dynamically allocated. Free the memory. */
1186-
PyMem_RawFree(l->p);
1186+
PyMem_Free(l->p);
11871187
}
11881188
l->p = NULL;
11891189
l->size = -1;

0 commit comments

Comments
 (0)