Skip to content

Commit 8e264d6

Browse files
authored
Merge branch 'python:main' into main
2 parents e09cbd3 + 2dd6e59 commit 8e264d6

5 files changed

Lines changed: 148 additions & 113 deletions

File tree

Lib/test/test_ast/test_ast.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,10 +1098,17 @@ def test_required_field_messages(self):
10981098
):
10991099
compile(expr_with_wrong_body, "<test>", "eval")
11001100

1101+
variable = ast.parse("test", mode="eval")
1102+
variable.body.id = b'test'
1103+
with self.assertRaisesRegex(TypeError,
1104+
"field 'id' was expecting a string object, got bytes"
1105+
):
1106+
compile(variable, "<test>", "eval")
1107+
11011108
constant = ast.parse("u'test'", mode="eval")
11021109
constant.body.kind = 0xFF
1103-
with self.assertRaisesRegex(
1104-
TypeError, "field 'kind' was expecting a string or bytes object"
1110+
with self.assertRaisesRegex(TypeError,
1111+
"field 'kind' was expecting a string or bytes object, got int"
11051112
):
11061113
compile(constant, "<test>", "eval")
11071114

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix missing error propagation in parser action helpers when memory allocation
2+
fails. Patch by Thomas Kowalski.

Parser/action_helpers.c

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,11 @@ _set_seq_context(Parser *p, asdl_expr_seq *seq, expr_context_ty ctx)
254254
}
255255
for (Py_ssize_t i = 0; i < len; i++) {
256256
expr_ty e = asdl_seq_GET(seq, i);
257-
asdl_seq_SET(new_seq, i, _PyPegen_set_expr_context(p, e, ctx));
257+
expr_ty new_e = _PyPegen_set_expr_context(p, e, ctx);
258+
if (!new_e) {
259+
return NULL;
260+
}
261+
asdl_seq_SET(new_seq, i, new_e);
258262
}
259263
return new_seq;
260264
}
@@ -268,19 +272,21 @@ _set_name_context(Parser *p, expr_ty e, expr_context_ty ctx)
268272
static expr_ty
269273
_set_tuple_context(Parser *p, expr_ty e, expr_context_ty ctx)
270274
{
271-
return _PyAST_Tuple(
272-
_set_seq_context(p, e->v.Tuple.elts, ctx),
273-
ctx,
274-
EXTRA_EXPR(e, e));
275+
asdl_expr_seq *seq = _set_seq_context(p, e->v.Tuple.elts, ctx);
276+
if (!seq && PyErr_Occurred()) {
277+
return NULL;
278+
}
279+
return _PyAST_Tuple(seq, ctx, EXTRA_EXPR(e, e));
275280
}
276281

277282
static expr_ty
278283
_set_list_context(Parser *p, expr_ty e, expr_context_ty ctx)
279284
{
280-
return _PyAST_List(
281-
_set_seq_context(p, e->v.List.elts, ctx),
282-
ctx,
283-
EXTRA_EXPR(e, e));
285+
asdl_expr_seq *seq = _set_seq_context(p, e->v.List.elts, ctx);
286+
if (!seq && PyErr_Occurred()) {
287+
return NULL;
288+
}
289+
return _PyAST_List(seq, ctx, EXTRA_EXPR(e, e));
284290
}
285291

286292
static expr_ty
@@ -300,8 +306,11 @@ _set_attribute_context(Parser *p, expr_ty e, expr_context_ty ctx)
300306
static expr_ty
301307
_set_starred_context(Parser *p, expr_ty e, expr_context_ty ctx)
302308
{
303-
return _PyAST_Starred(_PyPegen_set_expr_context(p, e->v.Starred.value, ctx),
304-
ctx, EXTRA_EXPR(e, e));
309+
expr_ty inner = _PyPegen_set_expr_context(p, e->v.Starred.value, ctx);
310+
if (!inner) {
311+
return NULL;
312+
}
313+
return _PyAST_Starred(inner, ctx, EXTRA_EXPR(e, e));
305314
}
306315

307316
/* Creates an `expr_ty` equivalent to `expr` but with `ctx` as context */
@@ -1168,7 +1177,14 @@ expr_ty _PyPegen_collect_call_seqs(Parser *p, asdl_expr_seq *a, asdl_seq *b,
11681177
}
11691178

11701179
asdl_expr_seq *starreds = _PyPegen_seq_extract_starred_exprs(p, b);
1180+
if (!starreds && PyErr_Occurred()) {
1181+
return NULL;
1182+
}
1183+
11711184
asdl_keyword_seq *keywords = _PyPegen_seq_delete_starred_exprs(p, b);
1185+
if (!keywords && PyErr_Occurred()) {
1186+
return NULL;
1187+
}
11721188

11731189
if (starreds) {
11741190
total_len += asdl_seq_LEN(starreds);
@@ -1580,7 +1596,7 @@ expr_ty _PyPegen_interpolation(Parser *p, expr_ty expression, Token *debug, Resu
15801596
end_col_offset, arena
15811597
);
15821598

1583-
if (!debug) {
1599+
if (!interpolation || !debug) {
15841600
return interpolation;
15851601
}
15861602

@@ -1591,6 +1607,9 @@ expr_ty _PyPegen_interpolation(Parser *p, expr_ty expression, Token *debug, Resu
15911607
}
15921608

15931609
asdl_expr_seq *values = _Py_asdl_expr_seq_new(2, arena);
1610+
if (!values) {
1611+
return NULL;
1612+
}
15941613
asdl_seq_SET(values, 0, debug_text);
15951614
asdl_seq_SET(values, 1, interpolation);
15961615
return _PyAST_JoinedStr(values, lineno, col_offset, debug_end_line, debug_end_offset, p->arena);
@@ -1607,7 +1626,7 @@ expr_ty _PyPegen_formatted_value(Parser *p, expr_ty expression, Token *debug, Re
16071626
end_col_offset, arena
16081627
);
16091628

1610-
if (!debug) {
1629+
if (!formatted_value || !debug) {
16111630
return formatted_value;
16121631
}
16131632

@@ -1637,6 +1656,9 @@ expr_ty _PyPegen_formatted_value(Parser *p, expr_ty expression, Token *debug, Re
16371656
}
16381657

16391658
asdl_expr_seq *values = _Py_asdl_expr_seq_new(2, arena);
1659+
if (!values) {
1660+
return NULL;
1661+
}
16401662
asdl_seq_SET(values, 0, debug_text);
16411663
asdl_seq_SET(values, 1, formatted_value);
16421664
return _PyAST_JoinedStr(values, lineno, col_offset, debug_end_line, debug_end_offset, p->arena);
@@ -1904,6 +1926,9 @@ _build_concatenated_joined_str(Parser *p, asdl_expr_seq *strings,
19041926
{
19051927
asdl_expr_seq *values = _build_concatenated_str(p, strings, lineno,
19061928
col_offset, end_lineno, end_col_offset, arena);
1929+
if (!values) {
1930+
return NULL;
1931+
}
19071932
return _PyAST_JoinedStr(values, lineno, col_offset, end_lineno, end_col_offset, p->arena);
19081933
}
19091934

@@ -1914,6 +1939,9 @@ _PyPegen_concatenate_tstrings(Parser *p, asdl_expr_seq *strings,
19141939
{
19151940
asdl_expr_seq *values = _build_concatenated_str(p, strings, lineno,
19161941
col_offset, end_lineno, end_col_offset, arena);
1942+
if (!values) {
1943+
return NULL;
1944+
}
19171945
return _PyAST_TemplateStr(values, lineno, col_offset, end_lineno,
19181946
end_col_offset, arena);
19191947
}

Parser/asdl_c.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -554,8 +554,8 @@ def typeCheck(self, name):
554554
self.emit("return 1;", 2)
555555
self.emit("}", 1)
556556
self.emit("if (!isinstance && field != NULL) {", 1)
557-
error = "field '%%s' was expecting node of type '%s', got '%%s'" % name
558-
self.emit("PyErr_Format(PyExc_TypeError, \"%s\", field, _PyType_Name(Py_TYPE(obj)));" % error, 2, reflow=False)
557+
error = "field '%%s' was expecting node of type '%s', got '%%T'" % name
558+
self.emit("PyErr_Format(PyExc_TypeError, \"%s\", field, obj);" % error, 2, reflow=False)
559559
self.emit("return 1;", 2)
560560
self.emit("}", 1)
561561

@@ -692,7 +692,7 @@ def visitField(self, field, name, sum=None, prod=None, depth=0):
692692
self.emit("Py_ssize_t i;", depth+1)
693693
self.emit("if (!PyList_Check(tmp)) {", depth+1)
694694
self.emit("PyErr_Format(PyExc_TypeError, \"%s field \\\"%s\\\" must "
695-
"be a list, not a %%.200s\", _PyType_Name(Py_TYPE(tmp)));" %
695+
"be a list, not a %%T\", tmp);" %
696696
(name, field.name),
697697
depth+2, reflow=False)
698698
self.emit("goto failed;", depth+2)
@@ -991,10 +991,9 @@ def visitModule(self, mod):
991991
992992
res = 0; /* if no error occurs, this stays 0 to the end */
993993
if (numfields < PyTuple_GET_SIZE(args)) {
994-
PyErr_Format(PyExc_TypeError, "%.400s constructor takes at most "
994+
PyErr_Format(PyExc_TypeError, "%T constructor takes at most "
995995
"%zd positional argument%s",
996-
_PyType_Name(Py_TYPE(self)),
997-
numfields, numfields == 1 ? "" : "s");
996+
self, numfields, numfields == 1 ? "" : "s");
998997
res = -1;
999998
goto cleanup;
1000999
}
@@ -1748,7 +1747,7 @@ def visitModule(self, mod):
17481747
static int obj2ast_identifier(struct ast_state *state, PyObject* obj, PyObject** out, const char* field, PyArena* arena)
17491748
{
17501749
if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
1751-
PyErr_Format(PyExc_TypeError, "field '%s' was expecting a string object", field);
1750+
PyErr_Format(PyExc_TypeError, "field '%s' was expecting a string object, got %T", field, obj);
17521751
return -1;
17531752
}
17541753
return obj2ast_object(state, obj, out, field, arena);
@@ -1757,7 +1756,7 @@ def visitModule(self, mod):
17571756
static int obj2ast_string(struct ast_state *state, PyObject* obj, PyObject** out, const char* field, PyArena* arena)
17581757
{
17591758
if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
1760-
PyErr_Format(PyExc_TypeError, "field '%s' was expecting a string or bytes object", field);
1759+
PyErr_Format(PyExc_TypeError, "field '%s' was expecting a string or bytes object, got %T", field, obj);
17611760
return -1;
17621761
}
17631762
return obj2ast_object(state, obj, out, field, arena);
@@ -2144,8 +2143,8 @@ class PartingShots(StaticVisitor):
21442143
return -1;
21452144
}
21462145
if (!isinstance) {
2147-
PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
2148-
req_name[mode], _PyType_Name(Py_TYPE(ast)));
2146+
PyErr_Format(PyExc_TypeError, "expected %s node, got %T",
2147+
req_name[mode], ast);
21492148
return -1;
21502149
}
21512150
return 0;

0 commit comments

Comments
 (0)