Skip to content

Commit

Permalink
bpo-41132: Use pymalloc allocator in the f-string parser (GH-21173)
Browse files Browse the repository at this point in the history
  • Loading branch information
lysnikolaou authored Jun 27, 2020
1 parent 9cfcdb7 commit 6dcbc24
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
6 changes: 3 additions & 3 deletions Parser/pegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
const char *fstring_msg = "f-string: ";
Py_ssize_t len = strlen(fstring_msg) + strlen(errmsg);

char *new_errmsg = PyMem_RawMalloc(len + 1); // Lengths of both strings plus NULL character
char *new_errmsg = PyMem_Malloc(len + 1); // Lengths of both strings plus NULL character
if (!new_errmsg) {
return (void *) PyErr_NoMemory();
}
Expand Down Expand Up @@ -443,15 +443,15 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
Py_DECREF(errstr);
Py_DECREF(value);
if (p->start_rule == Py_fstring_input) {
PyMem_RawFree((void *)errmsg);
PyMem_Free((void *)errmsg);
}
return NULL;

error:
Py_XDECREF(errstr);
Py_XDECREF(error_line);
if (p->start_rule == Py_fstring_input) {
PyMem_RawFree((void *)errmsg);
PyMem_Free((void *)errmsg);
}
return NULL;
}
Expand Down
14 changes: 7 additions & 7 deletions Parser/string_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ fstring_compile_expr(Parser *p, const char *expr_start, const char *expr_end,

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

struct tok_state* tok = PyTokenizer_FromString(str, 1);
if (tok == NULL) {
PyMem_RawFree(str);
PyMem_Free(str);
return NULL;
}
Py_INCREF(p->tok->filename);
Expand All @@ -631,7 +631,7 @@ fstring_compile_expr(Parser *p, const char *expr_start, const char *expr_end,
result = expr;

exit:
PyMem_RawFree(str);
PyMem_Free(str);
_PyPegen_Parser_Free(p2);
PyTokenizer_Free(tok);
return result;
Expand Down Expand Up @@ -1143,7 +1143,7 @@ ExprList_Append(ExprList *l, expr_ty exp)
Py_ssize_t i;
/* We're still using the cached data. Switch to
alloc-ing. */
l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
l->p = PyMem_Malloc(sizeof(expr_ty) * new_size);
if (!l->p) {
return -1;
}
Expand All @@ -1153,9 +1153,9 @@ ExprList_Append(ExprList *l, expr_ty exp)
}
} else {
/* Just realloc. */
expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
expr_ty *tmp = PyMem_Realloc(l->p, sizeof(expr_ty) * new_size);
if (!tmp) {
PyMem_RawFree(l->p);
PyMem_Free(l->p);
l->p = NULL;
return -1;
}
Expand Down Expand Up @@ -1183,7 +1183,7 @@ ExprList_Dealloc(ExprList *l)
/* Do nothing. */
} else {
/* We have dynamically allocated. Free the memory. */
PyMem_RawFree(l->p);
PyMem_Free(l->p);
}
l->p = NULL;
l->size = -1;
Expand Down

0 comments on commit 6dcbc24

Please sign in to comment.