Skip to content

Commit cd12313

Browse files
committed
Simplify annotations tuple creation
1 parent f39234a commit cd12313

File tree

1 file changed

+18
-42
lines changed

1 file changed

+18
-42
lines changed

Python/compile.c

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2027,39 +2027,24 @@ compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
20272027

20282028
static int
20292029
compiler_visit_argannotation(struct compiler *c, identifier id,
2030-
expr_ty annotation, PyObject *annotations)
2030+
expr_ty annotation, Py_ssize_t *annotations_len)
20312031
{
20322032
if (annotation) {
2033-
PyObject *mangled, *value;
2034-
mangled = _Py_Mangle(c->u->u_private, id);
2033+
PyObject *mangled = _Py_Mangle(c->u->u_private, id);
20352034
if (!mangled)
20362035
return 0;
2037-
if (PyList_Append(annotations, mangled) < 0) {
2038-
Py_DECREF(mangled);
2039-
return 0;
2040-
}
2041-
2042-
value = _PyAST_ExprAsUnicode(annotation);
2043-
if (!value) {
2044-
Py_DECREF(mangled);
2045-
return 0;
2046-
}
2047-
2048-
if (PyList_Append(annotations, value) < 0) {
2049-
Py_DECREF(mangled);
2050-
Py_DECREF(value);
2051-
return 0;
2052-
}
20532036

2037+
ADDOP_LOAD_CONST(c, mangled);
20542038
Py_DECREF(mangled);
2055-
Py_DECREF(value);
2039+
VISIT(c, annexpr, annotation);
2040+
*annotations_len += 2;
20562041
}
20572042
return 1;
20582043
}
20592044

20602045
static int
20612046
compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args,
2062-
PyObject *annotations)
2047+
Py_ssize_t *annotations_len)
20632048
{
20642049
int i;
20652050
for (i = 0; i < asdl_seq_LEN(args); i++) {
@@ -2068,7 +2053,7 @@ compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args,
20682053
c,
20692054
arg->arg,
20702055
arg->annotation,
2071-
annotations ))
2056+
annotations_len))
20722057
return 0;
20732058
}
20742059
return 1;
@@ -2078,56 +2063,47 @@ static int
20782063
compiler_visit_annotations(struct compiler *c, arguments_ty args,
20792064
expr_ty returns)
20802065
{
2081-
/* Push arg annotation dict.
2066+
/* Push arg annotation names and values.
20822067
The expressions are evaluated out-of-order wrt the source code.
20832068
2084-
Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
2069+
Return 0 on error, -1 if no annotations pushed, 1 if a annotations is pushed.
20852070
*/
20862071
static identifier return_str;
2087-
PyObject *annotations;
2088-
Py_ssize_t len;
2089-
annotations = PyList_New(0);
2090-
if (!annotations)
2091-
return 0;
2072+
Py_ssize_t annotations_len = 0;
20922073

2093-
if (!compiler_visit_argannotations(c, args->args, annotations))
2074+
if (!compiler_visit_argannotations(c, args->args, &annotations_len))
20942075
goto error;
2095-
if (!compiler_visit_argannotations(c, args->posonlyargs, annotations))
2076+
if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len))
20962077
goto error;
20972078
if (args->vararg && args->vararg->annotation &&
20982079
!compiler_visit_argannotation(c, args->vararg->arg,
2099-
args->vararg->annotation, annotations))
2080+
args->vararg->annotation, &annotations_len))
21002081
goto error;
2101-
if (!compiler_visit_argannotations(c, args->kwonlyargs, annotations))
2082+
if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len))
21022083
goto error;
21032084
if (args->kwarg && args->kwarg->annotation &&
21042085
!compiler_visit_argannotation(c, args->kwarg->arg,
2105-
args->kwarg->annotation, annotations))
2086+
args->kwarg->annotation, &annotations_len))
21062087
goto error;
21072088

21082089
if (!return_str) {
21092090
return_str = PyUnicode_InternFromString("return");
21102091
if (!return_str)
21112092
goto error;
21122093
}
2113-
if (!compiler_visit_argannotation(c, return_str, returns, annotations)) {
2094+
if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) {
21142095
goto error;
21152096
}
21162097

2117-
len = PyList_GET_SIZE(annotations);
2118-
if (len) {
2119-
PyObject *annotationstuple = PyList_AsTuple(annotations);
2120-
Py_DECREF(annotations);
2121-
ADDOP_LOAD_CONST_NEW(c, annotationstuple);
2098+
if (annotations_len) {
2099+
ADDOP_I(c, BUILD_TUPLE, annotations_len);
21222100
return 1;
21232101
}
21242102
else {
2125-
Py_DECREF(annotations);
21262103
return -1;
21272104
}
21282105

21292106
error:
2130-
Py_DECREF(annotations);
21312107
return 0;
21322108
}
21332109

0 commit comments

Comments
 (0)