mirror of
https://github.com/python/cpython.git
synced 2025-07-14 14:55:17 +00:00
bpo-35766: Merge typed_ast back into CPython (GH-11645)
This commit is contained in:
parent
d97daebfa6
commit
dcfcd146f8
30 changed files with 2043 additions and 655 deletions
512
Python/Python-ast.c
generated
512
Python/Python-ast.c
generated
|
@ -10,8 +10,10 @@ static PyTypeObject *mod_type;
|
|||
static PyObject* ast2obj_mod(void*);
|
||||
static PyTypeObject *Module_type;
|
||||
_Py_IDENTIFIER(body);
|
||||
_Py_IDENTIFIER(type_ignores);
|
||||
static char *Module_fields[]={
|
||||
"body",
|
||||
"type_ignores",
|
||||
};
|
||||
static PyTypeObject *Interactive_type;
|
||||
static char *Interactive_fields[]={
|
||||
|
@ -21,6 +23,13 @@ static PyTypeObject *Expression_type;
|
|||
static char *Expression_fields[]={
|
||||
"body",
|
||||
};
|
||||
static PyTypeObject *FunctionType_type;
|
||||
_Py_IDENTIFIER(argtypes);
|
||||
_Py_IDENTIFIER(returns);
|
||||
static char *FunctionType_fields[]={
|
||||
"argtypes",
|
||||
"returns",
|
||||
};
|
||||
static PyTypeObject *Suite_type;
|
||||
static char *Suite_fields[]={
|
||||
"body",
|
||||
|
@ -41,13 +50,14 @@ static PyTypeObject *FunctionDef_type;
|
|||
_Py_IDENTIFIER(name);
|
||||
_Py_IDENTIFIER(args);
|
||||
_Py_IDENTIFIER(decorator_list);
|
||||
_Py_IDENTIFIER(returns);
|
||||
_Py_IDENTIFIER(type_comment);
|
||||
static char *FunctionDef_fields[]={
|
||||
"name",
|
||||
"args",
|
||||
"body",
|
||||
"decorator_list",
|
||||
"returns",
|
||||
"type_comment",
|
||||
};
|
||||
static PyTypeObject *AsyncFunctionDef_type;
|
||||
static char *AsyncFunctionDef_fields[]={
|
||||
|
@ -56,6 +66,7 @@ static char *AsyncFunctionDef_fields[]={
|
|||
"body",
|
||||
"decorator_list",
|
||||
"returns",
|
||||
"type_comment",
|
||||
};
|
||||
static PyTypeObject *ClassDef_type;
|
||||
_Py_IDENTIFIER(bases);
|
||||
|
@ -81,6 +92,7 @@ static PyTypeObject *Assign_type;
|
|||
static char *Assign_fields[]={
|
||||
"targets",
|
||||
"value",
|
||||
"type_comment",
|
||||
};
|
||||
static PyTypeObject *AugAssign_type;
|
||||
_Py_IDENTIFIER(target);
|
||||
|
@ -107,6 +119,7 @@ static char *For_fields[]={
|
|||
"iter",
|
||||
"body",
|
||||
"orelse",
|
||||
"type_comment",
|
||||
};
|
||||
static PyTypeObject *AsyncFor_type;
|
||||
static char *AsyncFor_fields[]={
|
||||
|
@ -114,6 +127,7 @@ static char *AsyncFor_fields[]={
|
|||
"iter",
|
||||
"body",
|
||||
"orelse",
|
||||
"type_comment",
|
||||
};
|
||||
static PyTypeObject *While_type;
|
||||
_Py_IDENTIFIER(test);
|
||||
|
@ -133,11 +147,13 @@ _Py_IDENTIFIER(items);
|
|||
static char *With_fields[]={
|
||||
"items",
|
||||
"body",
|
||||
"type_comment",
|
||||
};
|
||||
static PyTypeObject *AsyncWith_type;
|
||||
static char *AsyncWith_fields[]={
|
||||
"items",
|
||||
"body",
|
||||
"type_comment",
|
||||
};
|
||||
static PyTypeObject *Raise_type;
|
||||
_Py_IDENTIFIER(exc);
|
||||
|
@ -478,6 +494,7 @@ _Py_IDENTIFIER(arg);
|
|||
static char *arg_fields[]={
|
||||
"arg",
|
||||
"annotation",
|
||||
"type_comment",
|
||||
};
|
||||
static PyTypeObject *keyword_type;
|
||||
static PyObject* ast2obj_keyword(void*);
|
||||
|
@ -500,6 +517,12 @@ static char *withitem_fields[]={
|
|||
"context_expr",
|
||||
"optional_vars",
|
||||
};
|
||||
static PyTypeObject *type_ignore_type;
|
||||
static PyObject* ast2obj_type_ignore(void*);
|
||||
static PyTypeObject *TypeIgnore_type;
|
||||
static char *TypeIgnore_fields[]={
|
||||
"lineno",
|
||||
};
|
||||
|
||||
|
||||
_Py_IDENTIFIER(_fields);
|
||||
|
@ -769,6 +792,15 @@ static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
|
|||
return obj2ast_object(obj, out, arena);
|
||||
}
|
||||
|
||||
static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
|
||||
{
|
||||
if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
|
||||
PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
|
||||
return 1;
|
||||
}
|
||||
return obj2ast_object(obj, out, arena);
|
||||
}
|
||||
|
||||
static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
|
||||
{
|
||||
int i;
|
||||
|
@ -810,23 +842,26 @@ static int init_types(void)
|
|||
mod_type = make_type("mod", &AST_type, NULL, 0);
|
||||
if (!mod_type) return 0;
|
||||
if (!add_attributes(mod_type, NULL, 0)) return 0;
|
||||
Module_type = make_type("Module", mod_type, Module_fields, 1);
|
||||
Module_type = make_type("Module", mod_type, Module_fields, 2);
|
||||
if (!Module_type) return 0;
|
||||
Interactive_type = make_type("Interactive", mod_type, Interactive_fields,
|
||||
1);
|
||||
if (!Interactive_type) return 0;
|
||||
Expression_type = make_type("Expression", mod_type, Expression_fields, 1);
|
||||
if (!Expression_type) return 0;
|
||||
FunctionType_type = make_type("FunctionType", mod_type,
|
||||
FunctionType_fields, 2);
|
||||
if (!FunctionType_type) return 0;
|
||||
Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
|
||||
if (!Suite_type) return 0;
|
||||
stmt_type = make_type("stmt", &AST_type, NULL, 0);
|
||||
if (!stmt_type) return 0;
|
||||
if (!add_attributes(stmt_type, stmt_attributes, 4)) return 0;
|
||||
FunctionDef_type = make_type("FunctionDef", stmt_type, FunctionDef_fields,
|
||||
5);
|
||||
6);
|
||||
if (!FunctionDef_type) return 0;
|
||||
AsyncFunctionDef_type = make_type("AsyncFunctionDef", stmt_type,
|
||||
AsyncFunctionDef_fields, 5);
|
||||
AsyncFunctionDef_fields, 6);
|
||||
if (!AsyncFunctionDef_type) return 0;
|
||||
ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 5);
|
||||
if (!ClassDef_type) return 0;
|
||||
|
@ -834,23 +869,23 @@ static int init_types(void)
|
|||
if (!Return_type) return 0;
|
||||
Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
|
||||
if (!Delete_type) return 0;
|
||||
Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
|
||||
Assign_type = make_type("Assign", stmt_type, Assign_fields, 3);
|
||||
if (!Assign_type) return 0;
|
||||
AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
|
||||
if (!AugAssign_type) return 0;
|
||||
AnnAssign_type = make_type("AnnAssign", stmt_type, AnnAssign_fields, 4);
|
||||
if (!AnnAssign_type) return 0;
|
||||
For_type = make_type("For", stmt_type, For_fields, 4);
|
||||
For_type = make_type("For", stmt_type, For_fields, 5);
|
||||
if (!For_type) return 0;
|
||||
AsyncFor_type = make_type("AsyncFor", stmt_type, AsyncFor_fields, 4);
|
||||
AsyncFor_type = make_type("AsyncFor", stmt_type, AsyncFor_fields, 5);
|
||||
if (!AsyncFor_type) return 0;
|
||||
While_type = make_type("While", stmt_type, While_fields, 3);
|
||||
if (!While_type) return 0;
|
||||
If_type = make_type("If", stmt_type, If_fields, 3);
|
||||
if (!If_type) return 0;
|
||||
With_type = make_type("With", stmt_type, With_fields, 2);
|
||||
With_type = make_type("With", stmt_type, With_fields, 3);
|
||||
if (!With_type) return 0;
|
||||
AsyncWith_type = make_type("AsyncWith", stmt_type, AsyncWith_fields, 2);
|
||||
AsyncWith_type = make_type("AsyncWith", stmt_type, AsyncWith_fields, 3);
|
||||
if (!AsyncWith_type) return 0;
|
||||
Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
|
||||
if (!Raise_type) return 0;
|
||||
|
@ -1113,7 +1148,7 @@ static int init_types(void)
|
|||
arguments_type = make_type("arguments", &AST_type, arguments_fields, 6);
|
||||
if (!arguments_type) return 0;
|
||||
if (!add_attributes(arguments_type, NULL, 0)) return 0;
|
||||
arg_type = make_type("arg", &AST_type, arg_fields, 2);
|
||||
arg_type = make_type("arg", &AST_type, arg_fields, 3);
|
||||
if (!arg_type) return 0;
|
||||
if (!add_attributes(arg_type, arg_attributes, 4)) return 0;
|
||||
keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
|
||||
|
@ -1125,6 +1160,12 @@ static int init_types(void)
|
|||
withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);
|
||||
if (!withitem_type) return 0;
|
||||
if (!add_attributes(withitem_type, NULL, 0)) return 0;
|
||||
type_ignore_type = make_type("type_ignore", &AST_type, NULL, 0);
|
||||
if (!type_ignore_type) return 0;
|
||||
if (!add_attributes(type_ignore_type, NULL, 0)) return 0;
|
||||
TypeIgnore_type = make_type("TypeIgnore", type_ignore_type,
|
||||
TypeIgnore_fields, 1);
|
||||
if (!TypeIgnore_type) return 0;
|
||||
initialized = 1;
|
||||
return 1;
|
||||
}
|
||||
|
@ -1148,9 +1189,11 @@ static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
|
|||
static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
|
||||
static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
|
||||
static int obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena);
|
||||
static int obj2ast_type_ignore(PyObject* obj, type_ignore_ty* out, PyArena*
|
||||
arena);
|
||||
|
||||
mod_ty
|
||||
Module(asdl_seq * body, PyArena *arena)
|
||||
Module(asdl_seq * body, asdl_seq * type_ignores, PyArena *arena)
|
||||
{
|
||||
mod_ty p;
|
||||
p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
|
||||
|
@ -1158,6 +1201,7 @@ Module(asdl_seq * body, PyArena *arena)
|
|||
return NULL;
|
||||
p->kind = Module_kind;
|
||||
p->v.Module.body = body;
|
||||
p->v.Module.type_ignores = type_ignores;
|
||||
return p;
|
||||
}
|
||||
|
||||
|
@ -1190,6 +1234,24 @@ Expression(expr_ty body, PyArena *arena)
|
|||
return p;
|
||||
}
|
||||
|
||||
mod_ty
|
||||
FunctionType(asdl_seq * argtypes, expr_ty returns, PyArena *arena)
|
||||
{
|
||||
mod_ty p;
|
||||
if (!returns) {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"field returns is required for FunctionType");
|
||||
return NULL;
|
||||
}
|
||||
p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
|
||||
if (!p)
|
||||
return NULL;
|
||||
p->kind = FunctionType_kind;
|
||||
p->v.FunctionType.argtypes = argtypes;
|
||||
p->v.FunctionType.returns = returns;
|
||||
return p;
|
||||
}
|
||||
|
||||
mod_ty
|
||||
Suite(asdl_seq * body, PyArena *arena)
|
||||
{
|
||||
|
@ -1204,8 +1266,8 @@ Suite(asdl_seq * body, PyArena *arena)
|
|||
|
||||
stmt_ty
|
||||
FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
|
||||
decorator_list, expr_ty returns, int lineno, int col_offset, int
|
||||
end_lineno, int end_col_offset, PyArena *arena)
|
||||
decorator_list, expr_ty returns, string type_comment, int lineno,
|
||||
int col_offset, int end_lineno, int end_col_offset, PyArena *arena)
|
||||
{
|
||||
stmt_ty p;
|
||||
if (!name) {
|
||||
|
@ -1227,6 +1289,7 @@ FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
|
|||
p->v.FunctionDef.body = body;
|
||||
p->v.FunctionDef.decorator_list = decorator_list;
|
||||
p->v.FunctionDef.returns = returns;
|
||||
p->v.FunctionDef.type_comment = type_comment;
|
||||
p->lineno = lineno;
|
||||
p->col_offset = col_offset;
|
||||
p->end_lineno = end_lineno;
|
||||
|
@ -1236,8 +1299,9 @@ FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
|
|||
|
||||
stmt_ty
|
||||
AsyncFunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq
|
||||
* decorator_list, expr_ty returns, int lineno, int col_offset,
|
||||
int end_lineno, int end_col_offset, PyArena *arena)
|
||||
* decorator_list, expr_ty returns, string type_comment, int
|
||||
lineno, int col_offset, int end_lineno, int end_col_offset,
|
||||
PyArena *arena)
|
||||
{
|
||||
stmt_ty p;
|
||||
if (!name) {
|
||||
|
@ -1259,6 +1323,7 @@ AsyncFunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq
|
|||
p->v.AsyncFunctionDef.body = body;
|
||||
p->v.AsyncFunctionDef.decorator_list = decorator_list;
|
||||
p->v.AsyncFunctionDef.returns = returns;
|
||||
p->v.AsyncFunctionDef.type_comment = type_comment;
|
||||
p->lineno = lineno;
|
||||
p->col_offset = col_offset;
|
||||
p->end_lineno = end_lineno;
|
||||
|
@ -1328,8 +1393,8 @@ Delete(asdl_seq * targets, int lineno, int col_offset, int end_lineno, int
|
|||
}
|
||||
|
||||
stmt_ty
|
||||
Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, int
|
||||
end_lineno, int end_col_offset, PyArena *arena)
|
||||
Assign(asdl_seq * targets, expr_ty value, string type_comment, int lineno, int
|
||||
col_offset, int end_lineno, int end_col_offset, PyArena *arena)
|
||||
{
|
||||
stmt_ty p;
|
||||
if (!value) {
|
||||
|
@ -1343,6 +1408,7 @@ Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, int
|
|||
p->kind = Assign_kind;
|
||||
p->v.Assign.targets = targets;
|
||||
p->v.Assign.value = value;
|
||||
p->v.Assign.type_comment = type_comment;
|
||||
p->lineno = lineno;
|
||||
p->col_offset = col_offset;
|
||||
p->end_lineno = end_lineno;
|
||||
|
@ -1416,8 +1482,9 @@ AnnAssign(expr_ty target, expr_ty annotation, expr_ty value, int simple, int
|
|||
}
|
||||
|
||||
stmt_ty
|
||||
For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
|
||||
lineno, int col_offset, int end_lineno, int end_col_offset, PyArena *arena)
|
||||
For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, string
|
||||
type_comment, int lineno, int col_offset, int end_lineno, int
|
||||
end_col_offset, PyArena *arena)
|
||||
{
|
||||
stmt_ty p;
|
||||
if (!target) {
|
||||
|
@ -1438,6 +1505,7 @@ For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
|
|||
p->v.For.iter = iter;
|
||||
p->v.For.body = body;
|
||||
p->v.For.orelse = orelse;
|
||||
p->v.For.type_comment = type_comment;
|
||||
p->lineno = lineno;
|
||||
p->col_offset = col_offset;
|
||||
p->end_lineno = end_lineno;
|
||||
|
@ -1446,9 +1514,9 @@ For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
|
|||
}
|
||||
|
||||
stmt_ty
|
||||
AsyncFor(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
|
||||
lineno, int col_offset, int end_lineno, int end_col_offset, PyArena
|
||||
*arena)
|
||||
AsyncFor(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse,
|
||||
string type_comment, int lineno, int col_offset, int end_lineno, int
|
||||
end_col_offset, PyArena *arena)
|
||||
{
|
||||
stmt_ty p;
|
||||
if (!target) {
|
||||
|
@ -1469,6 +1537,7 @@ AsyncFor(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
|
|||
p->v.AsyncFor.iter = iter;
|
||||
p->v.AsyncFor.body = body;
|
||||
p->v.AsyncFor.orelse = orelse;
|
||||
p->v.AsyncFor.type_comment = type_comment;
|
||||
p->lineno = lineno;
|
||||
p->col_offset = col_offset;
|
||||
p->end_lineno = end_lineno;
|
||||
|
@ -1525,8 +1594,8 @@ If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
|
|||
}
|
||||
|
||||
stmt_ty
|
||||
With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, int
|
||||
end_lineno, int end_col_offset, PyArena *arena)
|
||||
With(asdl_seq * items, asdl_seq * body, string type_comment, int lineno, int
|
||||
col_offset, int end_lineno, int end_col_offset, PyArena *arena)
|
||||
{
|
||||
stmt_ty p;
|
||||
p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
|
||||
|
@ -1535,6 +1604,7 @@ With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, int
|
|||
p->kind = With_kind;
|
||||
p->v.With.items = items;
|
||||
p->v.With.body = body;
|
||||
p->v.With.type_comment = type_comment;
|
||||
p->lineno = lineno;
|
||||
p->col_offset = col_offset;
|
||||
p->end_lineno = end_lineno;
|
||||
|
@ -1543,8 +1613,8 @@ With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, int
|
|||
}
|
||||
|
||||
stmt_ty
|
||||
AsyncWith(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, int
|
||||
end_lineno, int end_col_offset, PyArena *arena)
|
||||
AsyncWith(asdl_seq * items, asdl_seq * body, string type_comment, int lineno,
|
||||
int col_offset, int end_lineno, int end_col_offset, PyArena *arena)
|
||||
{
|
||||
stmt_ty p;
|
||||
p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
|
||||
|
@ -1553,6 +1623,7 @@ AsyncWith(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, int
|
|||
p->kind = AsyncWith_kind;
|
||||
p->v.AsyncWith.items = items;
|
||||
p->v.AsyncWith.body = body;
|
||||
p->v.AsyncWith.type_comment = type_comment;
|
||||
p->lineno = lineno;
|
||||
p->col_offset = col_offset;
|
||||
p->end_lineno = end_lineno;
|
||||
|
@ -2518,8 +2589,8 @@ arguments(asdl_seq * args, arg_ty vararg, asdl_seq * kwonlyargs, asdl_seq *
|
|||
}
|
||||
|
||||
arg_ty
|
||||
arg(identifier arg, expr_ty annotation, int lineno, int col_offset, int
|
||||
end_lineno, int end_col_offset, PyArena *arena)
|
||||
arg(identifier arg, expr_ty annotation, string type_comment, int lineno, int
|
||||
col_offset, int end_lineno, int end_col_offset, PyArena *arena)
|
||||
{
|
||||
arg_ty p;
|
||||
if (!arg) {
|
||||
|
@ -2532,6 +2603,7 @@ arg(identifier arg, expr_ty annotation, int lineno, int col_offset, int
|
|||
return NULL;
|
||||
p->arg = arg;
|
||||
p->annotation = annotation;
|
||||
p->type_comment = type_comment;
|
||||
p->lineno = lineno;
|
||||
p->col_offset = col_offset;
|
||||
p->end_lineno = end_lineno;
|
||||
|
@ -2590,6 +2662,18 @@ withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
|
|||
return p;
|
||||
}
|
||||
|
||||
type_ignore_ty
|
||||
TypeIgnore(int lineno, PyArena *arena)
|
||||
{
|
||||
type_ignore_ty p;
|
||||
p = (type_ignore_ty)PyArena_Malloc(arena, sizeof(*p));
|
||||
if (!p)
|
||||
return NULL;
|
||||
p->kind = TypeIgnore_kind;
|
||||
p->v.TypeIgnore.lineno = lineno;
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
PyObject*
|
||||
ast2obj_mod(void* _o)
|
||||
|
@ -2609,6 +2693,11 @@ ast2obj_mod(void* _o)
|
|||
if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
value = ast2obj_list(o->v.Module.type_ignores, ast2obj_type_ignore);
|
||||
if (!value) goto failed;
|
||||
if (_PyObject_SetAttrId(result, &PyId_type_ignores, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
break;
|
||||
case Interactive_kind:
|
||||
result = PyType_GenericNew(Interactive_type, NULL, NULL);
|
||||
|
@ -2628,6 +2717,20 @@ ast2obj_mod(void* _o)
|
|||
goto failed;
|
||||
Py_DECREF(value);
|
||||
break;
|
||||
case FunctionType_kind:
|
||||
result = PyType_GenericNew(FunctionType_type, NULL, NULL);
|
||||
if (!result) goto failed;
|
||||
value = ast2obj_list(o->v.FunctionType.argtypes, ast2obj_expr);
|
||||
if (!value) goto failed;
|
||||
if (_PyObject_SetAttrId(result, &PyId_argtypes, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
value = ast2obj_expr(o->v.FunctionType.returns);
|
||||
if (!value) goto failed;
|
||||
if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
break;
|
||||
case Suite_kind:
|
||||
result = PyType_GenericNew(Suite_type, NULL, NULL);
|
||||
if (!result) goto failed;
|
||||
|
@ -2683,6 +2786,11 @@ ast2obj_stmt(void* _o)
|
|||
if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
value = ast2obj_string(o->v.FunctionDef.type_comment);
|
||||
if (!value) goto failed;
|
||||
if (_PyObject_SetAttrId(result, &PyId_type_comment, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
break;
|
||||
case AsyncFunctionDef_kind:
|
||||
result = PyType_GenericNew(AsyncFunctionDef_type, NULL, NULL);
|
||||
|
@ -2713,6 +2821,11 @@ ast2obj_stmt(void* _o)
|
|||
if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
value = ast2obj_string(o->v.AsyncFunctionDef.type_comment);
|
||||
if (!value) goto failed;
|
||||
if (_PyObject_SetAttrId(result, &PyId_type_comment, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
break;
|
||||
case ClassDef_kind:
|
||||
result = PyType_GenericNew(ClassDef_type, NULL, NULL);
|
||||
|
@ -2774,6 +2887,11 @@ ast2obj_stmt(void* _o)
|
|||
if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
value = ast2obj_string(o->v.Assign.type_comment);
|
||||
if (!value) goto failed;
|
||||
if (_PyObject_SetAttrId(result, &PyId_type_comment, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
break;
|
||||
case AugAssign_kind:
|
||||
result = PyType_GenericNew(AugAssign_type, NULL, NULL);
|
||||
|
@ -2841,6 +2959,11 @@ ast2obj_stmt(void* _o)
|
|||
if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
value = ast2obj_string(o->v.For.type_comment);
|
||||
if (!value) goto failed;
|
||||
if (_PyObject_SetAttrId(result, &PyId_type_comment, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
break;
|
||||
case AsyncFor_kind:
|
||||
result = PyType_GenericNew(AsyncFor_type, NULL, NULL);
|
||||
|
@ -2865,6 +2988,11 @@ ast2obj_stmt(void* _o)
|
|||
if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
value = ast2obj_string(o->v.AsyncFor.type_comment);
|
||||
if (!value) goto failed;
|
||||
if (_PyObject_SetAttrId(result, &PyId_type_comment, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
break;
|
||||
case While_kind:
|
||||
result = PyType_GenericNew(While_type, NULL, NULL);
|
||||
|
@ -2917,6 +3045,11 @@ ast2obj_stmt(void* _o)
|
|||
if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
value = ast2obj_string(o->v.With.type_comment);
|
||||
if (!value) goto failed;
|
||||
if (_PyObject_SetAttrId(result, &PyId_type_comment, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
break;
|
||||
case AsyncWith_kind:
|
||||
result = PyType_GenericNew(AsyncWith_type, NULL, NULL);
|
||||
|
@ -2931,6 +3064,11 @@ ast2obj_stmt(void* _o)
|
|||
if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
value = ast2obj_string(o->v.AsyncWith.type_comment);
|
||||
if (!value) goto failed;
|
||||
if (_PyObject_SetAttrId(result, &PyId_type_comment, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
break;
|
||||
case Raise_kind:
|
||||
result = PyType_GenericNew(Raise_type, NULL, NULL);
|
||||
|
@ -3870,6 +4008,11 @@ ast2obj_arg(void* _o)
|
|||
if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
value = ast2obj_string(o->type_comment);
|
||||
if (!value) goto failed;
|
||||
if (_PyObject_SetAttrId(result, &PyId_type_comment, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
value = ast2obj_int(o->lineno);
|
||||
if (!value) goto failed;
|
||||
if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
|
||||
|
@ -3981,6 +4124,33 @@ failed:
|
|||
return NULL;
|
||||
}
|
||||
|
||||
PyObject*
|
||||
ast2obj_type_ignore(void* _o)
|
||||
{
|
||||
type_ignore_ty o = (type_ignore_ty)_o;
|
||||
PyObject *result = NULL, *value = NULL;
|
||||
if (!o) {
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
switch (o->kind) {
|
||||
case TypeIgnore_kind:
|
||||
result = PyType_GenericNew(TypeIgnore_type, NULL, NULL);
|
||||
if (!result) goto failed;
|
||||
value = ast2obj_int(o->v.TypeIgnore.lineno);
|
||||
if (!value) goto failed;
|
||||
if (_PyObject_SetAttrId(result, &PyId_lineno, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
failed:
|
||||
Py_XDECREF(value);
|
||||
Py_XDECREF(result);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
|
||||
|
@ -3999,6 +4169,7 @@ obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
|
|||
}
|
||||
if (isinstance) {
|
||||
asdl_seq* body;
|
||||
asdl_seq* type_ignores;
|
||||
|
||||
if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
|
||||
return 1;
|
||||
|
@ -4030,7 +4201,37 @@ obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
|
|||
}
|
||||
Py_CLEAR(tmp);
|
||||
}
|
||||
*out = Module(body, arena);
|
||||
if (_PyObject_LookupAttrId(obj, &PyId_type_ignores, &tmp) < 0) {
|
||||
return 1;
|
||||
}
|
||||
if (tmp == NULL) {
|
||||
PyErr_SetString(PyExc_TypeError, "required field \"type_ignores\" missing from Module");
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
int res;
|
||||
Py_ssize_t len;
|
||||
Py_ssize_t i;
|
||||
if (!PyList_Check(tmp)) {
|
||||
PyErr_Format(PyExc_TypeError, "Module field \"type_ignores\" must be a list, not a %.200s", tmp->ob_type->tp_name);
|
||||
goto failed;
|
||||
}
|
||||
len = PyList_GET_SIZE(tmp);
|
||||
type_ignores = _Py_asdl_seq_new(len, arena);
|
||||
if (type_ignores == NULL) goto failed;
|
||||
for (i = 0; i < len; i++) {
|
||||
type_ignore_ty val;
|
||||
res = obj2ast_type_ignore(PyList_GET_ITEM(tmp, i), &val, arena);
|
||||
if (res != 0) goto failed;
|
||||
if (len != PyList_GET_SIZE(tmp)) {
|
||||
PyErr_SetString(PyExc_RuntimeError, "Module field \"type_ignores\" changed size during iteration");
|
||||
goto failed;
|
||||
}
|
||||
asdl_seq_SET(type_ignores, i, val);
|
||||
}
|
||||
Py_CLEAR(tmp);
|
||||
}
|
||||
*out = Module(body, type_ignores, arena);
|
||||
if (*out == NULL) goto failed;
|
||||
return 0;
|
||||
}
|
||||
|
@ -4099,6 +4300,61 @@ obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
|
|||
if (*out == NULL) goto failed;
|
||||
return 0;
|
||||
}
|
||||
isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionType_type);
|
||||
if (isinstance == -1) {
|
||||
return 1;
|
||||
}
|
||||
if (isinstance) {
|
||||
asdl_seq* argtypes;
|
||||
expr_ty returns;
|
||||
|
||||
if (_PyObject_LookupAttrId(obj, &PyId_argtypes, &tmp) < 0) {
|
||||
return 1;
|
||||
}
|
||||
if (tmp == NULL) {
|
||||
PyErr_SetString(PyExc_TypeError, "required field \"argtypes\" missing from FunctionType");
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
int res;
|
||||
Py_ssize_t len;
|
||||
Py_ssize_t i;
|
||||
if (!PyList_Check(tmp)) {
|
||||
PyErr_Format(PyExc_TypeError, "FunctionType field \"argtypes\" must be a list, not a %.200s", tmp->ob_type->tp_name);
|
||||
goto failed;
|
||||
}
|
||||
len = PyList_GET_SIZE(tmp);
|
||||
argtypes = _Py_asdl_seq_new(len, arena);
|
||||
if (argtypes == NULL) goto failed;
|
||||
for (i = 0; i < len; i++) {
|
||||
expr_ty val;
|
||||
res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
|
||||
if (res != 0) goto failed;
|
||||
if (len != PyList_GET_SIZE(tmp)) {
|
||||
PyErr_SetString(PyExc_RuntimeError, "FunctionType field \"argtypes\" changed size during iteration");
|
||||
goto failed;
|
||||
}
|
||||
asdl_seq_SET(argtypes, i, val);
|
||||
}
|
||||
Py_CLEAR(tmp);
|
||||
}
|
||||
if (_PyObject_LookupAttrId(obj, &PyId_returns, &tmp) < 0) {
|
||||
return 1;
|
||||
}
|
||||
if (tmp == NULL) {
|
||||
PyErr_SetString(PyExc_TypeError, "required field \"returns\" missing from FunctionType");
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
int res;
|
||||
res = obj2ast_expr(tmp, &returns, arena);
|
||||
if (res != 0) goto failed;
|
||||
Py_CLEAR(tmp);
|
||||
}
|
||||
*out = FunctionType(argtypes, returns, arena);
|
||||
if (*out == NULL) goto failed;
|
||||
return 0;
|
||||
}
|
||||
isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
|
||||
if (isinstance == -1) {
|
||||
return 1;
|
||||
|
@ -4224,6 +4480,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
|
|||
asdl_seq* body;
|
||||
asdl_seq* decorator_list;
|
||||
expr_ty returns;
|
||||
string type_comment;
|
||||
|
||||
if (_PyObject_LookupAttrId(obj, &PyId_name, &tmp) < 0) {
|
||||
return 1;
|
||||
|
@ -4324,8 +4581,22 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
|
|||
if (res != 0) goto failed;
|
||||
Py_CLEAR(tmp);
|
||||
}
|
||||
*out = FunctionDef(name, args, body, decorator_list, returns, lineno,
|
||||
col_offset, end_lineno, end_col_offset, arena);
|
||||
if (_PyObject_LookupAttrId(obj, &PyId_type_comment, &tmp) < 0) {
|
||||
return 1;
|
||||
}
|
||||
if (tmp == NULL || tmp == Py_None) {
|
||||
Py_CLEAR(tmp);
|
||||
type_comment = NULL;
|
||||
}
|
||||
else {
|
||||
int res;
|
||||
res = obj2ast_string(tmp, &type_comment, arena);
|
||||
if (res != 0) goto failed;
|
||||
Py_CLEAR(tmp);
|
||||
}
|
||||
*out = FunctionDef(name, args, body, decorator_list, returns,
|
||||
type_comment, lineno, col_offset, end_lineno,
|
||||
end_col_offset, arena);
|
||||
if (*out == NULL) goto failed;
|
||||
return 0;
|
||||
}
|
||||
|
@ -4339,6 +4610,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
|
|||
asdl_seq* body;
|
||||
asdl_seq* decorator_list;
|
||||
expr_ty returns;
|
||||
string type_comment;
|
||||
|
||||
if (_PyObject_LookupAttrId(obj, &PyId_name, &tmp) < 0) {
|
||||
return 1;
|
||||
|
@ -4439,9 +4711,22 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
|
|||
if (res != 0) goto failed;
|
||||
Py_CLEAR(tmp);
|
||||
}
|
||||
if (_PyObject_LookupAttrId(obj, &PyId_type_comment, &tmp) < 0) {
|
||||
return 1;
|
||||
}
|
||||
if (tmp == NULL || tmp == Py_None) {
|
||||
Py_CLEAR(tmp);
|
||||
type_comment = NULL;
|
||||
}
|
||||
else {
|
||||
int res;
|
||||
res = obj2ast_string(tmp, &type_comment, arena);
|
||||
if (res != 0) goto failed;
|
||||
Py_CLEAR(tmp);
|
||||
}
|
||||
*out = AsyncFunctionDef(name, args, body, decorator_list, returns,
|
||||
lineno, col_offset, end_lineno, end_col_offset,
|
||||
arena);
|
||||
type_comment, lineno, col_offset, end_lineno,
|
||||
end_col_offset, arena);
|
||||
if (*out == NULL) goto failed;
|
||||
return 0;
|
||||
}
|
||||
|
@ -4668,6 +4953,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
|
|||
if (isinstance) {
|
||||
asdl_seq* targets;
|
||||
expr_ty value;
|
||||
string type_comment;
|
||||
|
||||
if (_PyObject_LookupAttrId(obj, &PyId_targets, &tmp) < 0) {
|
||||
return 1;
|
||||
|
@ -4712,8 +4998,21 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
|
|||
if (res != 0) goto failed;
|
||||
Py_CLEAR(tmp);
|
||||
}
|
||||
*out = Assign(targets, value, lineno, col_offset, end_lineno,
|
||||
end_col_offset, arena);
|
||||
if (_PyObject_LookupAttrId(obj, &PyId_type_comment, &tmp) < 0) {
|
||||
return 1;
|
||||
}
|
||||
if (tmp == NULL || tmp == Py_None) {
|
||||
Py_CLEAR(tmp);
|
||||
type_comment = NULL;
|
||||
}
|
||||
else {
|
||||
int res;
|
||||
res = obj2ast_string(tmp, &type_comment, arena);
|
||||
if (res != 0) goto failed;
|
||||
Py_CLEAR(tmp);
|
||||
}
|
||||
*out = Assign(targets, value, type_comment, lineno, col_offset,
|
||||
end_lineno, end_col_offset, arena);
|
||||
if (*out == NULL) goto failed;
|
||||
return 0;
|
||||
}
|
||||
|
@ -4846,6 +5145,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
|
|||
expr_ty iter;
|
||||
asdl_seq* body;
|
||||
asdl_seq* orelse;
|
||||
string type_comment;
|
||||
|
||||
if (_PyObject_LookupAttrId(obj, &PyId_target, &tmp) < 0) {
|
||||
return 1;
|
||||
|
@ -4933,8 +5233,21 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
|
|||
}
|
||||
Py_CLEAR(tmp);
|
||||
}
|
||||
*out = For(target, iter, body, orelse, lineno, col_offset, end_lineno,
|
||||
end_col_offset, arena);
|
||||
if (_PyObject_LookupAttrId(obj, &PyId_type_comment, &tmp) < 0) {
|
||||
return 1;
|
||||
}
|
||||
if (tmp == NULL || tmp == Py_None) {
|
||||
Py_CLEAR(tmp);
|
||||
type_comment = NULL;
|
||||
}
|
||||
else {
|
||||
int res;
|
||||
res = obj2ast_string(tmp, &type_comment, arena);
|
||||
if (res != 0) goto failed;
|
||||
Py_CLEAR(tmp);
|
||||
}
|
||||
*out = For(target, iter, body, orelse, type_comment, lineno,
|
||||
col_offset, end_lineno, end_col_offset, arena);
|
||||
if (*out == NULL) goto failed;
|
||||
return 0;
|
||||
}
|
||||
|
@ -4947,6 +5260,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
|
|||
expr_ty iter;
|
||||
asdl_seq* body;
|
||||
asdl_seq* orelse;
|
||||
string type_comment;
|
||||
|
||||
if (_PyObject_LookupAttrId(obj, &PyId_target, &tmp) < 0) {
|
||||
return 1;
|
||||
|
@ -5034,8 +5348,21 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
|
|||
}
|
||||
Py_CLEAR(tmp);
|
||||
}
|
||||
*out = AsyncFor(target, iter, body, orelse, lineno, col_offset,
|
||||
end_lineno, end_col_offset, arena);
|
||||
if (_PyObject_LookupAttrId(obj, &PyId_type_comment, &tmp) < 0) {
|
||||
return 1;
|
||||
}
|
||||
if (tmp == NULL || tmp == Py_None) {
|
||||
Py_CLEAR(tmp);
|
||||
type_comment = NULL;
|
||||
}
|
||||
else {
|
||||
int res;
|
||||
res = obj2ast_string(tmp, &type_comment, arena);
|
||||
if (res != 0) goto failed;
|
||||
Py_CLEAR(tmp);
|
||||
}
|
||||
*out = AsyncFor(target, iter, body, orelse, type_comment, lineno,
|
||||
col_offset, end_lineno, end_col_offset, arena);
|
||||
if (*out == NULL) goto failed;
|
||||
return 0;
|
||||
}
|
||||
|
@ -5220,6 +5547,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
|
|||
if (isinstance) {
|
||||
asdl_seq* items;
|
||||
asdl_seq* body;
|
||||
string type_comment;
|
||||
|
||||
if (_PyObject_LookupAttrId(obj, &PyId_items, &tmp) < 0) {
|
||||
return 1;
|
||||
|
@ -5281,7 +5609,20 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
|
|||
}
|
||||
Py_CLEAR(tmp);
|
||||
}
|
||||
*out = With(items, body, lineno, col_offset, end_lineno,
|
||||
if (_PyObject_LookupAttrId(obj, &PyId_type_comment, &tmp) < 0) {
|
||||
return 1;
|
||||
}
|
||||
if (tmp == NULL || tmp == Py_None) {
|
||||
Py_CLEAR(tmp);
|
||||
type_comment = NULL;
|
||||
}
|
||||
else {
|
||||
int res;
|
||||
res = obj2ast_string(tmp, &type_comment, arena);
|
||||
if (res != 0) goto failed;
|
||||
Py_CLEAR(tmp);
|
||||
}
|
||||
*out = With(items, body, type_comment, lineno, col_offset, end_lineno,
|
||||
end_col_offset, arena);
|
||||
if (*out == NULL) goto failed;
|
||||
return 0;
|
||||
|
@ -5293,6 +5634,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
|
|||
if (isinstance) {
|
||||
asdl_seq* items;
|
||||
asdl_seq* body;
|
||||
string type_comment;
|
||||
|
||||
if (_PyObject_LookupAttrId(obj, &PyId_items, &tmp) < 0) {
|
||||
return 1;
|
||||
|
@ -5354,8 +5696,21 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
|
|||
}
|
||||
Py_CLEAR(tmp);
|
||||
}
|
||||
*out = AsyncWith(items, body, lineno, col_offset, end_lineno,
|
||||
end_col_offset, arena);
|
||||
if (_PyObject_LookupAttrId(obj, &PyId_type_comment, &tmp) < 0) {
|
||||
return 1;
|
||||
}
|
||||
if (tmp == NULL || tmp == Py_None) {
|
||||
Py_CLEAR(tmp);
|
||||
type_comment = NULL;
|
||||
}
|
||||
else {
|
||||
int res;
|
||||
res = obj2ast_string(tmp, &type_comment, arena);
|
||||
if (res != 0) goto failed;
|
||||
Py_CLEAR(tmp);
|
||||
}
|
||||
*out = AsyncWith(items, body, type_comment, lineno, col_offset,
|
||||
end_lineno, end_col_offset, arena);
|
||||
if (*out == NULL) goto failed;
|
||||
return 0;
|
||||
}
|
||||
|
@ -8073,6 +8428,7 @@ obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
|
|||
PyObject* tmp = NULL;
|
||||
identifier arg;
|
||||
expr_ty annotation;
|
||||
string type_comment;
|
||||
int lineno;
|
||||
int col_offset;
|
||||
int end_lineno;
|
||||
|
@ -8104,6 +8460,19 @@ obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
|
|||
if (res != 0) goto failed;
|
||||
Py_CLEAR(tmp);
|
||||
}
|
||||
if (_PyObject_LookupAttrId(obj, &PyId_type_comment, &tmp) < 0) {
|
||||
return 1;
|
||||
}
|
||||
if (tmp == NULL || tmp == Py_None) {
|
||||
Py_CLEAR(tmp);
|
||||
type_comment = NULL;
|
||||
}
|
||||
else {
|
||||
int res;
|
||||
res = obj2ast_string(tmp, &type_comment, arena);
|
||||
if (res != 0) goto failed;
|
||||
Py_CLEAR(tmp);
|
||||
}
|
||||
if (_PyObject_LookupAttrId(obj, &PyId_lineno, &tmp) < 0) {
|
||||
return 1;
|
||||
}
|
||||
|
@ -8156,8 +8525,8 @@ obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
|
|||
if (res != 0) goto failed;
|
||||
Py_CLEAR(tmp);
|
||||
}
|
||||
*out = arg(arg, annotation, lineno, col_offset, end_lineno, end_col_offset,
|
||||
arena);
|
||||
*out = arg(arg, annotation, type_comment, lineno, col_offset, end_lineno,
|
||||
end_col_offset, arena);
|
||||
return 0;
|
||||
failed:
|
||||
Py_XDECREF(tmp);
|
||||
|
@ -8284,6 +8653,48 @@ failed:
|
|||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
obj2ast_type_ignore(PyObject* obj, type_ignore_ty* out, PyArena* arena)
|
||||
{
|
||||
int isinstance;
|
||||
|
||||
PyObject *tmp = NULL;
|
||||
|
||||
if (obj == Py_None) {
|
||||
*out = NULL;
|
||||
return 0;
|
||||
}
|
||||
isinstance = PyObject_IsInstance(obj, (PyObject*)TypeIgnore_type);
|
||||
if (isinstance == -1) {
|
||||
return 1;
|
||||
}
|
||||
if (isinstance) {
|
||||
int lineno;
|
||||
|
||||
if (_PyObject_LookupAttrId(obj, &PyId_lineno, &tmp) < 0) {
|
||||
return 1;
|
||||
}
|
||||
if (tmp == NULL) {
|
||||
PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from TypeIgnore");
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
int res;
|
||||
res = obj2ast_int(tmp, &lineno, arena);
|
||||
if (res != 0) goto failed;
|
||||
Py_CLEAR(tmp);
|
||||
}
|
||||
*out = TypeIgnore(lineno, arena);
|
||||
if (*out == NULL) goto failed;
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyErr_Format(PyExc_TypeError, "expected some sort of type_ignore, but got %R", obj);
|
||||
failed:
|
||||
Py_XDECREF(tmp);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static struct PyModuleDef _astmodule = {
|
||||
PyModuleDef_HEAD_INIT, "_ast"
|
||||
|
@ -8299,6 +8710,8 @@ PyInit__ast(void)
|
|||
if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;
|
||||
if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0)
|
||||
return NULL;
|
||||
if (PyModule_AddIntMacro(m, PyCF_TYPE_COMMENTS) < 0)
|
||||
return NULL;
|
||||
if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return NULL;
|
||||
if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0) return
|
||||
NULL;
|
||||
|
@ -8306,6 +8719,8 @@ PyInit__ast(void)
|
|||
0) return NULL;
|
||||
if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) < 0)
|
||||
return NULL;
|
||||
if (PyDict_SetItemString(d, "FunctionType", (PyObject*)FunctionType_type) <
|
||||
0) return NULL;
|
||||
if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
|
||||
NULL;
|
||||
if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return NULL;
|
||||
|
@ -8486,6 +8901,10 @@ PyInit__ast(void)
|
|||
NULL;
|
||||
if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
|
||||
return NULL;
|
||||
if (PyDict_SetItemString(d, "type_ignore", (PyObject*)type_ignore_type) <
|
||||
0) return NULL;
|
||||
if (PyDict_SetItemString(d, "TypeIgnore", (PyObject*)TypeIgnore_type) < 0)
|
||||
return NULL;
|
||||
return m;
|
||||
}
|
||||
|
||||
|
@ -8498,18 +8917,19 @@ PyObject* PyAST_mod2obj(mod_ty t)
|
|||
}
|
||||
|
||||
/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
|
||||
/* and 3 for "func_type" */
|
||||
mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
|
||||
{
|
||||
mod_ty res;
|
||||
PyObject *req_type[3];
|
||||
char *req_name[] = {"Module", "Expression", "Interactive"};
|
||||
char *req_name[] = {"Module", "Expression", "Interactive", "FunctionType"};
|
||||
int isinstance;
|
||||
|
||||
req_type[0] = (PyObject*)Module_type;
|
||||
req_type[1] = (PyObject*)Expression_type;
|
||||
req_type[2] = (PyObject*)Interactive_type;
|
||||
|
||||
assert(0 <= mode && mode <= 2);
|
||||
assert(0 <= mode && mode <= 3);
|
||||
|
||||
if (!init_types())
|
||||
return NULL;
|
||||
|
|
304
Python/ast.c
304
Python/ast.c
|
@ -698,6 +698,13 @@ ast_error(struct compiling *c, const node *n, const char *errmsg, ...)
|
|||
small_stmt elements is returned.
|
||||
*/
|
||||
|
||||
static string
|
||||
new_type_comment(const char *s)
|
||||
{
|
||||
return PyUnicode_DecodeUTF8(s, strlen(s), NULL);
|
||||
}
|
||||
#define NEW_TYPE_COMMENT(n) new_type_comment(STR(n))
|
||||
|
||||
static int
|
||||
num_stmts(const node *n)
|
||||
{
|
||||
|
@ -725,11 +732,17 @@ num_stmts(const node *n)
|
|||
case simple_stmt:
|
||||
return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
|
||||
case suite:
|
||||
case func_body_suite:
|
||||
/* func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
|
||||
/* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
|
||||
if (NCH(n) == 1)
|
||||
return num_stmts(CHILD(n, 0));
|
||||
else {
|
||||
i = 2;
|
||||
l = 0;
|
||||
for (i = 2; i < (NCH(n) - 1); i++)
|
||||
if (TYPE(CHILD(n, 1)) == TYPE_COMMENT)
|
||||
i += 2;
|
||||
for (; i < (NCH(n) - 1); i++)
|
||||
l += num_stmts(CHILD(n, i));
|
||||
return l;
|
||||
}
|
||||
|
@ -753,10 +766,13 @@ PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
|
|||
{
|
||||
int i, j, k, num;
|
||||
asdl_seq *stmts = NULL;
|
||||
asdl_seq *type_ignores = NULL;
|
||||
stmt_ty s;
|
||||
node *ch;
|
||||
struct compiling c;
|
||||
mod_ty res = NULL;
|
||||
asdl_seq *argtypes = NULL;
|
||||
expr_ty ret, arg;
|
||||
|
||||
c.c_arena = arena;
|
||||
/* borrowed reference */
|
||||
|
@ -795,7 +811,23 @@ PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
|
|||
}
|
||||
}
|
||||
}
|
||||
res = Module(stmts, arena);
|
||||
|
||||
/* Type ignores are stored under the ENDMARKER in file_input. */
|
||||
ch = CHILD(n, NCH(n) - 1);
|
||||
REQ(ch, ENDMARKER);
|
||||
num = NCH(ch);
|
||||
type_ignores = _Py_asdl_seq_new(num, arena);
|
||||
if (!type_ignores)
|
||||
goto out;
|
||||
|
||||
for (i = 0; i < num; i++) {
|
||||
type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), arena);
|
||||
if (!ti)
|
||||
goto out;
|
||||
asdl_seq_SET(type_ignores, i, ti);
|
||||
}
|
||||
|
||||
res = Module(stmts, type_ignores, arena);
|
||||
break;
|
||||
case eval_input: {
|
||||
expr_ty testlist_ast;
|
||||
|
@ -847,6 +879,46 @@ PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
|
|||
res = Interactive(stmts, arena);
|
||||
}
|
||||
break;
|
||||
case func_type_input:
|
||||
n = CHILD(n, 0);
|
||||
REQ(n, func_type);
|
||||
|
||||
if (TYPE(CHILD(n, 1)) == typelist) {
|
||||
ch = CHILD(n, 1);
|
||||
/* this is overly permissive -- we don't pay any attention to
|
||||
* stars on the args -- just parse them into an ordered list */
|
||||
num = 0;
|
||||
for (i = 0; i < NCH(ch); i++) {
|
||||
if (TYPE(CHILD(ch, i)) == test) {
|
||||
num++;
|
||||
}
|
||||
}
|
||||
|
||||
argtypes = _Py_asdl_seq_new(num, arena);
|
||||
if (!argtypes)
|
||||
goto out;
|
||||
|
||||
j = 0;
|
||||
for (i = 0; i < NCH(ch); i++) {
|
||||
if (TYPE(CHILD(ch, i)) == test) {
|
||||
arg = ast_for_expr(&c, CHILD(ch, i));
|
||||
if (!arg)
|
||||
goto out;
|
||||
asdl_seq_SET(argtypes, j++, arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
argtypes = _Py_asdl_seq_new(0, arena);
|
||||
if (!argtypes)
|
||||
goto out;
|
||||
}
|
||||
|
||||
ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1));
|
||||
if (!ret)
|
||||
goto out;
|
||||
res = FunctionType(argtypes, ret, arena);
|
||||
break;
|
||||
default:
|
||||
PyErr_Format(PyExc_SystemError,
|
||||
"invalid node %d for PyAST_FromNode", TYPE(n));
|
||||
|
@ -1269,7 +1341,7 @@ ast_for_arg(struct compiling *c, const node *n)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
ret = arg(name, annotation, LINENO(n), n->n_col_offset,
|
||||
ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset,
|
||||
n->n_end_lineno, n->n_end_col_offset, c->c_arena);
|
||||
if (!ret)
|
||||
return NULL;
|
||||
|
@ -1328,13 +1400,22 @@ handle_keywordonly_args(struct compiling *c, const node *n, int start,
|
|||
goto error;
|
||||
if (forbidden_name(c, argname, ch, 0))
|
||||
goto error;
|
||||
arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset,
|
||||
arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset,
|
||||
ch->n_end_lineno, ch->n_end_col_offset,
|
||||
c->c_arena);
|
||||
if (!arg)
|
||||
goto error;
|
||||
asdl_seq_SET(kwonlyargs, j++, arg);
|
||||
i += 2; /* the name and the comma */
|
||||
i += 1; /* the name */
|
||||
if (TYPE(CHILD(n, i)) == COMMA)
|
||||
i += 1; /* the comma, if present */
|
||||
break;
|
||||
case TYPE_COMMENT:
|
||||
/* arg will be equal to the last argument processed */
|
||||
arg->type_comment = NEW_TYPE_COMMENT(ch);
|
||||
if (!arg->type_comment)
|
||||
goto error;
|
||||
i += 1;
|
||||
break;
|
||||
case DOUBLESTAR:
|
||||
return i;
|
||||
|
@ -1464,19 +1545,29 @@ ast_for_arguments(struct compiling *c, const node *n)
|
|||
if (!arg)
|
||||
return NULL;
|
||||
asdl_seq_SET(posargs, k++, arg);
|
||||
i += 2; /* the name and the comma */
|
||||
i += 1; /* the name */
|
||||
if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
|
||||
i += 1; /* the comma, if present */
|
||||
break;
|
||||
case STAR:
|
||||
if (i+1 >= NCH(n) ||
|
||||
(i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
|
||||
(i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA
|
||||
|| TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) {
|
||||
ast_error(c, CHILD(n, i),
|
||||
"named arguments must follow bare *");
|
||||
"named arguments must follow bare *");
|
||||
return NULL;
|
||||
}
|
||||
ch = CHILD(n, i+1); /* tfpdef or COMMA */
|
||||
if (TYPE(ch) == COMMA) {
|
||||
int res = 0;
|
||||
i += 2; /* now follows keyword only arguments */
|
||||
|
||||
if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
|
||||
ast_error(c, CHILD(n, i),
|
||||
"bare * has associated type comment");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
res = handle_keywordonly_args(c, n, i,
|
||||
kwonlyargs, kwdefaults);
|
||||
if (res == -1) return NULL;
|
||||
|
@ -1487,7 +1578,17 @@ ast_for_arguments(struct compiling *c, const node *n)
|
|||
if (!vararg)
|
||||
return NULL;
|
||||
|
||||
i += 3;
|
||||
i += 2; /* the star and the name */
|
||||
if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
|
||||
i += 1; /* the comma, if present */
|
||||
|
||||
if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
|
||||
vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i));
|
||||
if (!vararg->type_comment)
|
||||
return NULL;
|
||||
i += 1;
|
||||
}
|
||||
|
||||
if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
|
||||
|| TYPE(CHILD(n, i)) == vfpdef)) {
|
||||
int res = 0;
|
||||
|
@ -1504,7 +1605,21 @@ ast_for_arguments(struct compiling *c, const node *n)
|
|||
kwarg = ast_for_arg(c, ch);
|
||||
if (!kwarg)
|
||||
return NULL;
|
||||
i += 3;
|
||||
i += 2; /* the double star and the name */
|
||||
if (TYPE(CHILD(n, i)) == COMMA)
|
||||
i += 1; /* the comma, if present */
|
||||
break;
|
||||
case TYPE_COMMENT:
|
||||
assert(i);
|
||||
|
||||
if (kwarg)
|
||||
arg = kwarg;
|
||||
|
||||
/* arg will be equal to the last argument processed */
|
||||
arg->type_comment = NEW_TYPE_COMMENT(ch);
|
||||
if (!arg->type_comment)
|
||||
return NULL;
|
||||
i += 1;
|
||||
break;
|
||||
default:
|
||||
PyErr_Format(PyExc_SystemError,
|
||||
|
@ -1613,7 +1728,7 @@ static stmt_ty
|
|||
ast_for_funcdef_impl(struct compiling *c, const node *n0,
|
||||
asdl_seq *decorator_seq, bool is_async)
|
||||
{
|
||||
/* funcdef: 'def' NAME parameters ['->' test] ':' suite */
|
||||
/* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */
|
||||
const node * const n = is_async ? CHILD(n0, 1) : n0;
|
||||
identifier name;
|
||||
arguments_ty args;
|
||||
|
@ -1621,6 +1736,8 @@ ast_for_funcdef_impl(struct compiling *c, const node *n0,
|
|||
expr_ty returns = NULL;
|
||||
int name_i = 1;
|
||||
int end_lineno, end_col_offset;
|
||||
node *tc;
|
||||
string type_comment = NULL;
|
||||
|
||||
REQ(n, funcdef);
|
||||
|
||||
|
@ -1638,16 +1755,37 @@ ast_for_funcdef_impl(struct compiling *c, const node *n0,
|
|||
return NULL;
|
||||
name_i += 2;
|
||||
}
|
||||
if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) {
|
||||
type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3));
|
||||
if (!type_comment)
|
||||
return NULL;
|
||||
name_i += 1;
|
||||
}
|
||||
body = ast_for_suite(c, CHILD(n, name_i + 3));
|
||||
if (!body)
|
||||
return NULL;
|
||||
get_last_end_pos(body, &end_lineno, &end_col_offset);
|
||||
|
||||
if (NCH(CHILD(n, name_i + 3)) > 1) {
|
||||
/* Check if the suite has a type comment in it. */
|
||||
tc = CHILD(CHILD(n, name_i + 3), 1);
|
||||
|
||||
if (TYPE(tc) == TYPE_COMMENT) {
|
||||
if (type_comment != NULL) {
|
||||
ast_error(c, n, "Cannot have two type comments on def");
|
||||
return NULL;
|
||||
}
|
||||
type_comment = NEW_TYPE_COMMENT(tc);
|
||||
if (!type_comment)
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (is_async)
|
||||
return AsyncFunctionDef(name, args, body, decorator_seq, returns,
|
||||
return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment,
|
||||
LINENO(n0), n0->n_col_offset, end_lineno, end_col_offset, c->c_arena);
|
||||
else
|
||||
return FunctionDef(name, args, body, decorator_seq, returns,
|
||||
return FunctionDef(name, args, body, decorator_seq, returns, type_comment,
|
||||
LINENO(n), n->n_col_offset, end_lineno, end_col_offset, c->c_arena);
|
||||
}
|
||||
|
||||
|
@ -2295,7 +2433,7 @@ ast_for_atom(struct compiling *c, const node *n)
|
|||
/* It's a dictionary comprehension. */
|
||||
if (is_dict) {
|
||||
ast_error(c, n, "dict unpacking cannot be used in "
|
||||
"dict comprehension");
|
||||
"dict comprehension");
|
||||
return NULL;
|
||||
}
|
||||
res = ast_for_dictcomp(c, ch);
|
||||
|
@ -2870,13 +3008,13 @@ ast_for_call(struct compiling *c, const node *n, expr_ty func,
|
|||
if (nkeywords) {
|
||||
if (ndoublestars) {
|
||||
ast_error(c, chch,
|
||||
"positional argument follows "
|
||||
"keyword argument unpacking");
|
||||
"positional argument follows "
|
||||
"keyword argument unpacking");
|
||||
}
|
||||
else {
|
||||
ast_error(c, chch,
|
||||
"positional argument follows "
|
||||
"keyword argument");
|
||||
"positional argument follows "
|
||||
"keyword argument");
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
@ -2890,8 +3028,8 @@ ast_for_call(struct compiling *c, const node *n, expr_ty func,
|
|||
expr_ty starred;
|
||||
if (ndoublestars) {
|
||||
ast_error(c, chch,
|
||||
"iterable argument unpacking follows "
|
||||
"keyword argument unpacking");
|
||||
"iterable argument unpacking follows "
|
||||
"keyword argument unpacking");
|
||||
return NULL;
|
||||
}
|
||||
e = ast_for_expr(c, CHILD(ch, 1));
|
||||
|
@ -2929,13 +3067,13 @@ ast_for_call(struct compiling *c, const node *n, expr_ty func,
|
|||
if (nkeywords) {
|
||||
if (ndoublestars) {
|
||||
ast_error(c, chch,
|
||||
"positional argument follows "
|
||||
"keyword argument unpacking");
|
||||
"positional argument follows "
|
||||
"keyword argument unpacking");
|
||||
}
|
||||
else {
|
||||
ast_error(c, chch,
|
||||
"positional argument follows "
|
||||
"keyword argument");
|
||||
"positional argument follows "
|
||||
"keyword argument");
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
@ -2996,7 +3134,7 @@ ast_for_call(struct compiling *c, const node *n, expr_ty func,
|
|||
tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
|
||||
if (tmp && !PyUnicode_Compare(tmp, key)) {
|
||||
ast_error(c, chch,
|
||||
"keyword argument repeated");
|
||||
"keyword argument repeated");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
@ -3045,15 +3183,16 @@ ast_for_expr_stmt(struct compiling *c, const node *n)
|
|||
{
|
||||
REQ(n, expr_stmt);
|
||||
/* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
|
||||
('=' (yield_expr|testlist_star_expr))*)
|
||||
annassign: ':' test ['=' test]
|
||||
testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
|
||||
augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
|
||||
| '<<=' | '>>=' | '**=' | '//='
|
||||
[('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )
|
||||
annassign: ':' test ['=' (yield_expr|testlist)]
|
||||
testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
|
||||
augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
|
||||
'<<=' | '>>=' | '**=' | '//=')
|
||||
test: ... here starts the operator precedence dance
|
||||
*/
|
||||
int num = NCH(n);
|
||||
|
||||
if (NCH(n) == 1) {
|
||||
if (num == 1) {
|
||||
expr_ty e = ast_for_testlist(c, CHILD(n, 0));
|
||||
if (!e)
|
||||
return NULL;
|
||||
|
@ -3178,17 +3317,22 @@ ast_for_expr_stmt(struct compiling *c, const node *n)
|
|||
}
|
||||
}
|
||||
else {
|
||||
int i;
|
||||
int i, nch_minus_type, has_type_comment;
|
||||
asdl_seq *targets;
|
||||
node *value;
|
||||
expr_ty expression;
|
||||
string type_comment;
|
||||
|
||||
/* a normal assignment */
|
||||
REQ(CHILD(n, 1), EQUAL);
|
||||
targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
|
||||
|
||||
has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT;
|
||||
nch_minus_type = num - has_type_comment;
|
||||
|
||||
targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena);
|
||||
if (!targets)
|
||||
return NULL;
|
||||
for (i = 0; i < NCH(n) - 2; i += 2) {
|
||||
for (i = 0; i < nch_minus_type - 2; i += 2) {
|
||||
expr_ty e;
|
||||
node *ch = CHILD(n, i);
|
||||
if (TYPE(ch) == yield_expr) {
|
||||
|
@ -3205,14 +3349,21 @@ ast_for_expr_stmt(struct compiling *c, const node *n)
|
|||
|
||||
asdl_seq_SET(targets, i / 2, e);
|
||||
}
|
||||
value = CHILD(n, NCH(n) - 1);
|
||||
value = CHILD(n, nch_minus_type - 1);
|
||||
if (TYPE(value) == testlist_star_expr)
|
||||
expression = ast_for_testlist(c, value);
|
||||
else
|
||||
expression = ast_for_expr(c, value);
|
||||
if (!expression)
|
||||
return NULL;
|
||||
return Assign(targets, expression, LINENO(n), n->n_col_offset,
|
||||
if (has_type_comment) {
|
||||
type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type));
|
||||
if (!type_comment)
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
type_comment = NULL;
|
||||
return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset,
|
||||
n->n_end_lineno, n->n_end_col_offset, c->c_arena);
|
||||
}
|
||||
}
|
||||
|
@ -3520,8 +3671,9 @@ ast_for_import_stmt(struct compiling *c, const node *n)
|
|||
n = CHILD(n, idx);
|
||||
n_children = NCH(n);
|
||||
if (n_children % 2 == 0) {
|
||||
ast_error(c, n, "trailing comma not allowed without"
|
||||
" surrounding parentheses");
|
||||
ast_error(c, n,
|
||||
"trailing comma not allowed without"
|
||||
" surrounding parentheses");
|
||||
return NULL;
|
||||
}
|
||||
break;
|
||||
|
@ -3639,13 +3791,15 @@ ast_for_assert_stmt(struct compiling *c, const node *n)
|
|||
static asdl_seq *
|
||||
ast_for_suite(struct compiling *c, const node *n)
|
||||
{
|
||||
/* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
|
||||
/* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
|
||||
asdl_seq *seq;
|
||||
stmt_ty s;
|
||||
int i, total, num, end, pos = 0;
|
||||
node *ch;
|
||||
|
||||
REQ(n, suite);
|
||||
if (TYPE(n) != func_body_suite) {
|
||||
REQ(n, suite);
|
||||
}
|
||||
|
||||
total = num_stmts(n);
|
||||
seq = _Py_asdl_seq_new(total, c->c_arena);
|
||||
|
@ -3669,7 +3823,13 @@ ast_for_suite(struct compiling *c, const node *n)
|
|||
}
|
||||
}
|
||||
else {
|
||||
for (i = 2; i < (NCH(n) - 1); i++) {
|
||||
i = 2;
|
||||
if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) {
|
||||
i += 2;
|
||||
REQ(CHILD(n, 2), NEWLINE);
|
||||
}
|
||||
|
||||
for (; i < (NCH(n) - 1); i++) {
|
||||
ch = CHILD(n, i);
|
||||
REQ(ch, stmt);
|
||||
num = num_stmts(ch);
|
||||
|
@ -3903,11 +4063,15 @@ ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
|
|||
expr_ty target, first;
|
||||
const node *node_target;
|
||||
int end_lineno, end_col_offset;
|
||||
/* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
|
||||
int has_type_comment;
|
||||
string type_comment;
|
||||
/* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */
|
||||
REQ(n, for_stmt);
|
||||
|
||||
if (NCH(n) == 9) {
|
||||
seq = ast_for_suite(c, CHILD(n, 8));
|
||||
has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT;
|
||||
|
||||
if (NCH(n) == 9 + has_type_comment) {
|
||||
seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment));
|
||||
if (!seq)
|
||||
return NULL;
|
||||
}
|
||||
|
@ -3929,7 +4093,7 @@ ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
|
|||
expression = ast_for_testlist(c, CHILD(n, 3));
|
||||
if (!expression)
|
||||
return NULL;
|
||||
suite_seq = ast_for_suite(c, CHILD(n, 5));
|
||||
suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment));
|
||||
if (!suite_seq)
|
||||
return NULL;
|
||||
|
||||
|
@ -3938,12 +4102,21 @@ ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
|
|||
} else {
|
||||
get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
|
||||
}
|
||||
|
||||
if (has_type_comment) {
|
||||
type_comment = NEW_TYPE_COMMENT(CHILD(n, 5));
|
||||
if (!type_comment)
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
type_comment = NULL;
|
||||
|
||||
if (is_async)
|
||||
return AsyncFor(target, expression, suite_seq, seq,
|
||||
return AsyncFor(target, expression, suite_seq, seq, type_comment,
|
||||
LINENO(n0), n0->n_col_offset,
|
||||
end_lineno, end_col_offset, c->c_arena);
|
||||
else
|
||||
return For(target, expression, suite_seq, seq,
|
||||
return For(target, expression, suite_seq, seq, type_comment,
|
||||
LINENO(n), n->n_col_offset,
|
||||
end_lineno, end_col_offset, c->c_arena);
|
||||
}
|
||||
|
@ -4111,21 +4284,25 @@ ast_for_with_item(struct compiling *c, const node *n)
|
|||
return withitem(context_expr, optional_vars, c->c_arena);
|
||||
}
|
||||
|
||||
/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
|
||||
/* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */
|
||||
static stmt_ty
|
||||
ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
|
||||
{
|
||||
const node * const n = is_async ? CHILD(n0, 1) : n0;
|
||||
int i, n_items, end_lineno, end_col_offset;
|
||||
int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset;
|
||||
asdl_seq *items, *body;
|
||||
string type_comment;
|
||||
|
||||
REQ(n, with_stmt);
|
||||
|
||||
n_items = (NCH(n) - 2) / 2;
|
||||
has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT;
|
||||
nch_minus_type = NCH(n) - has_type_comment;
|
||||
|
||||
n_items = (nch_minus_type - 2) / 2;
|
||||
items = _Py_asdl_seq_new(n_items, c->c_arena);
|
||||
if (!items)
|
||||
return NULL;
|
||||
for (i = 1; i < NCH(n) - 2; i += 2) {
|
||||
for (i = 1; i < nch_minus_type - 2; i += 2) {
|
||||
withitem_ty item = ast_for_with_item(c, CHILD(n, i));
|
||||
if (!item)
|
||||
return NULL;
|
||||
|
@ -4137,11 +4314,19 @@ ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
|
|||
return NULL;
|
||||
get_last_end_pos(body, &end_lineno, &end_col_offset);
|
||||
|
||||
if (has_type_comment) {
|
||||
type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2));
|
||||
if (!type_comment)
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
type_comment = NULL;
|
||||
|
||||
if (is_async)
|
||||
return AsyncWith(items, body, LINENO(n0), n0->n_col_offset,
|
||||
return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset,
|
||||
end_lineno, end_col_offset, c->c_arena);
|
||||
else
|
||||
return With(items, body, LINENO(n), n->n_col_offset,
|
||||
return With(items, body, type_comment, LINENO(n), n->n_col_offset,
|
||||
end_lineno, end_col_offset, c->c_arena);
|
||||
}
|
||||
|
||||
|
@ -4768,8 +4953,9 @@ fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
|
|||
if (ch == '\\') {
|
||||
/* Error: can't include a backslash character, inside
|
||||
parens or strings or not. */
|
||||
ast_error(c, n, "f-string expression part "
|
||||
"cannot include a backslash");
|
||||
ast_error(c, n,
|
||||
"f-string expression part "
|
||||
"cannot include a backslash");
|
||||
return -1;
|
||||
}
|
||||
if (quote_char) {
|
||||
|
@ -4893,8 +5079,9 @@ fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
|
|||
/* Validate the conversion. */
|
||||
if (!(conversion == 's' || conversion == 'r'
|
||||
|| conversion == 'a')) {
|
||||
ast_error(c, n, "f-string: invalid conversion character: "
|
||||
"expected 's', 'r', or 'a'");
|
||||
ast_error(c, n,
|
||||
"f-string: invalid conversion character: "
|
||||
"expected 's', 'r', or 'a'");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
@ -5446,7 +5633,8 @@ parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
|
|||
const char *ch;
|
||||
for (ch = s; *ch; ch++) {
|
||||
if (Py_CHARMASK(*ch) >= 0x80) {
|
||||
ast_error(c, n, "bytes can only contain ASCII "
|
||||
ast_error(c, n,
|
||||
"bytes can only contain ASCII "
|
||||
"literal characters.");
|
||||
return -1;
|
||||
}
|
||||
|
|
|
@ -765,13 +765,13 @@ builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
|
|||
int compile_mode = -1;
|
||||
int is_ast;
|
||||
PyCompilerFlags cf;
|
||||
int start[] = {Py_file_input, Py_eval_input, Py_single_input};
|
||||
int start[] = {Py_file_input, Py_eval_input, Py_single_input, Py_func_type_input};
|
||||
PyObject *result;
|
||||
|
||||
cf.cf_flags = flags | PyCF_SOURCE_IS_UTF8;
|
||||
|
||||
if (flags &
|
||||
~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
|
||||
~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST | PyCF_TYPE_COMMENTS))
|
||||
{
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"compile(): unrecognised flags");
|
||||
|
@ -795,9 +795,21 @@ builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
|
|||
compile_mode = 1;
|
||||
else if (strcmp(mode, "single") == 0)
|
||||
compile_mode = 2;
|
||||
else if (strcmp(mode, "func_type") == 0) {
|
||||
if (!(flags & PyCF_ONLY_AST)) {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"compile() mode 'func_type' requires flag PyCF_ONLY_AST");
|
||||
goto error;
|
||||
}
|
||||
compile_mode = 3;
|
||||
}
|
||||
else {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"compile() mode must be 'exec', 'eval' or 'single'");
|
||||
const char *msg;
|
||||
if (flags & PyCF_ONLY_AST)
|
||||
msg = "compile() mode must be 'exec', 'eval', 'single' or 'func_type'";
|
||||
else
|
||||
msg = "compile() mode must be 'exec', 'eval' or 'single'";
|
||||
PyErr_SetString(PyExc_ValueError, msg);
|
||||
goto error;
|
||||
}
|
||||
|
||||
|
|
1033
Python/graminit.c
1033
Python/graminit.c
File diff suppressed because it is too large
Load diff
|
@ -158,6 +158,8 @@ static int PARSER_FLAGS(PyCompilerFlags *flags)
|
|||
parser_flags |= PyPARSE_IGNORE_COOKIE;
|
||||
if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL)
|
||||
parser_flags |= PyPARSE_BARRY_AS_BDFL;
|
||||
if (flags->cf_flags & PyCF_TYPE_COMMENTS)
|
||||
parser_flags |= PyPARSE_TYPE_COMMENTS;
|
||||
return parser_flags;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue