Skip to content

Commit 5193d0a

Browse files
authored
[3.9] bpo-41132: Use pymalloc allocator in the f-string parser (GH-21173) (GH-21183)
(cherry picked from commit 6dcbc24) Automerge-Triggered-By: @pablogsal
1 parent 9191eac commit 5193d0a

2 files changed

Lines changed: 14 additions & 14 deletions

File tree

Parser/pegen/parse_string.c

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

598598
len = expr_end - expr_start;
599599
/* Allocate 3 extra bytes: open paren, close paren, null byte. */
600-
str = PyMem_RawMalloc(len + 3);
600+
str = PyMem_Malloc(len + 3);
601601
if (str == NULL) {
602602
PyErr_NoMemory();
603603
return NULL;
@@ -610,7 +610,7 @@ fstring_compile_expr(Parser *p, const char *expr_start, const char *expr_end,
610610

611611
struct tok_state* tok = PyTokenizer_FromString(str, 1);
612612
if (tok == NULL) {
613-
PyMem_RawFree(str);
613+
PyMem_Free(str);
614614
return NULL;
615615
}
616616
Py_INCREF(p->tok->filename);
@@ -636,7 +636,7 @@ fstring_compile_expr(Parser *p, const char *expr_start, const char *expr_end,
636636
result = expr;
637637

638638
exit:
639-
PyMem_RawFree(str);
639+
PyMem_Free(str);
640640
_PyPegen_Parser_Free(p2);
641641
PyTokenizer_Free(tok);
642642
return result;
@@ -1148,7 +1148,7 @@ ExprList_Append(ExprList *l, expr_ty exp)
11481148
Py_ssize_t i;
11491149
/* We're still using the cached data. Switch to
11501150
alloc-ing. */
1151-
l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
1151+
l->p = PyMem_Malloc(sizeof(expr_ty) * new_size);
11521152
if (!l->p) {
11531153
return -1;
11541154
}
@@ -1158,9 +1158,9 @@ ExprList_Append(ExprList *l, expr_ty exp)
11581158
}
11591159
} else {
11601160
/* Just realloc. */
1161-
expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
1161+
expr_ty *tmp = PyMem_Realloc(l->p, sizeof(expr_ty) * new_size);
11621162
if (!tmp) {
1163-
PyMem_RawFree(l->p);
1163+
PyMem_Free(l->p);
11641164
l->p = NULL;
11651165
return -1;
11661166
}
@@ -1188,7 +1188,7 @@ ExprList_Dealloc(ExprList *l)
11881188
/* Do nothing. */
11891189
} else {
11901190
/* We have dynamically allocated. Free the memory. */
1191-
PyMem_RawFree(l->p);
1191+
PyMem_Free(l->p);
11921192
}
11931193
l->p = NULL;
11941194
l->size = -1;

Python/ast.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4765,7 +4765,7 @@ fstring_compile_expr(const char *expr_start, const char *expr_end,
47654765

47664766
len = expr_end - expr_start;
47674767
/* Allocate 3 extra bytes: open paren, close paren, null byte. */
4768-
str = PyMem_RawMalloc(len + 3);
4768+
str = PyMem_Malloc(len + 3);
47694769
if (str == NULL) {
47704770
PyErr_NoMemory();
47714771
return NULL;
@@ -4781,15 +4781,15 @@ fstring_compile_expr(const char *expr_start, const char *expr_end,
47814781
mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
47824782
Py_eval_input, 0);
47834783
if (!mod_n) {
4784-
PyMem_RawFree(str);
4784+
PyMem_Free(str);
47854785
return NULL;
47864786
}
47874787
/* Reuse str to find the correct column offset. */
47884788
str[0] = '{';
47894789
str[len+1] = '}';
47904790
fstring_fix_node_location(n, mod_n, str);
47914791
mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
4792-
PyMem_RawFree(str);
4792+
PyMem_Free(str);
47934793
PyNode_Free(mod_n);
47944794
if (!mod)
47954795
return NULL;
@@ -5305,17 +5305,17 @@ ExprList_Append(ExprList *l, expr_ty exp)
53055305
Py_ssize_t i;
53065306
/* We're still using the cached data. Switch to
53075307
alloc-ing. */
5308-
l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
5308+
l->p = PyMem_Malloc(sizeof(expr_ty) * new_size);
53095309
if (!l->p)
53105310
return -1;
53115311
/* Copy the cached data into the new buffer. */
53125312
for (i = 0; i < l->size; i++)
53135313
l->p[i] = l->data[i];
53145314
} else {
53155315
/* Just realloc. */
5316-
expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
5316+
expr_ty *tmp = PyMem_Realloc(l->p, sizeof(expr_ty) * new_size);
53175317
if (!tmp) {
5318-
PyMem_RawFree(l->p);
5318+
PyMem_Free(l->p);
53195319
l->p = NULL;
53205320
return -1;
53215321
}
@@ -5343,7 +5343,7 @@ ExprList_Dealloc(ExprList *l)
53435343
/* Do nothing. */
53445344
} else {
53455345
/* We have dynamically allocated. Free the memory. */
5346-
PyMem_RawFree(l->p);
5346+
PyMem_Free(l->p);
53475347
}
53485348
l->p = NULL;
53495349
l->size = -1;

0 commit comments

Comments
 (0)