typedef struct _withitem *withitem_ty;
+typedef struct _type_ignore *type_ignore_ty;
+
enum _mod_kind {Module_kind=1, Interactive_kind=2, Expression_kind=3,
- Suite_kind=4};
+ FunctionType_kind=4, Suite_kind=5};
struct _mod {
enum _mod_kind kind;
union {
struct {
asdl_seq *body;
+ asdl_seq *type_ignores;
} Module;
struct {
expr_ty body;
} Expression;
+ struct {
+ asdl_seq *argtypes;
+ expr_ty returns;
+ } FunctionType;
+
struct {
asdl_seq *body;
} Suite;
asdl_seq *body;
asdl_seq *decorator_list;
expr_ty returns;
+ string type_comment;
} FunctionDef;
struct {
asdl_seq *body;
asdl_seq *decorator_list;
expr_ty returns;
+ string type_comment;
} AsyncFunctionDef;
struct {
struct {
asdl_seq *targets;
expr_ty value;
+ string type_comment;
} Assign;
struct {
expr_ty iter;
asdl_seq *body;
asdl_seq *orelse;
+ string type_comment;
} For;
struct {
expr_ty iter;
asdl_seq *body;
asdl_seq *orelse;
+ string type_comment;
} AsyncFor;
struct {
struct {
asdl_seq *items;
asdl_seq *body;
+ string type_comment;
} With;
struct {
asdl_seq *items;
asdl_seq *body;
+ string type_comment;
} AsyncWith;
struct {
struct _arg {
identifier arg;
expr_ty annotation;
+ string type_comment;
int lineno;
int col_offset;
int end_lineno;
expr_ty optional_vars;
};
+enum _type_ignore_kind {TypeIgnore_kind=1};
+struct _type_ignore {
+ enum _type_ignore_kind kind;
+ union {
+ struct {
+ int lineno;
+ } TypeIgnore;
+
+ } v;
+};
+
// Note: these macros affect function definitions, not only call sites.
-#define Module(a0, a1) _Py_Module(a0, a1)
-mod_ty _Py_Module(asdl_seq * body, PyArena *arena);
+#define Module(a0, a1, a2) _Py_Module(a0, a1, a2)
+mod_ty _Py_Module(asdl_seq * body, asdl_seq * type_ignores, PyArena *arena);
#define Interactive(a0, a1) _Py_Interactive(a0, a1)
mod_ty _Py_Interactive(asdl_seq * body, PyArena *arena);
#define Expression(a0, a1) _Py_Expression(a0, a1)
mod_ty _Py_Expression(expr_ty body, PyArena *arena);
+#define FunctionType(a0, a1, a2) _Py_FunctionType(a0, a1, a2)
+mod_ty _Py_FunctionType(asdl_seq * argtypes, expr_ty returns, PyArena *arena);
#define Suite(a0, a1) _Py_Suite(a0, a1)
mod_ty _Py_Suite(asdl_seq * body, PyArena *arena);
-#define FunctionDef(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) _Py_FunctionDef(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9)
+#define FunctionDef(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) _Py_FunctionDef(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
stmt_ty _Py_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);
-#define AsyncFunctionDef(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) _Py_AsyncFunctionDef(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9)
+ asdl_seq * decorator_list, expr_ty returns, string
+ type_comment, int lineno, int col_offset, int
+ end_lineno, int end_col_offset, PyArena *arena);
+#define AsyncFunctionDef(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) _Py_AsyncFunctionDef(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
stmt_ty _Py_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);
+ string type_comment, int lineno, int col_offset,
+ int end_lineno, int end_col_offset, PyArena
+ *arena);
#define ClassDef(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) _Py_ClassDef(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9)
stmt_ty _Py_ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords,
asdl_seq * body, asdl_seq * decorator_list, int lineno,
#define Delete(a0, a1, a2, a3, a4, a5) _Py_Delete(a0, a1, a2, a3, a4, a5)
stmt_ty _Py_Delete(asdl_seq * targets, int lineno, int col_offset, int
end_lineno, int end_col_offset, PyArena *arena);
-#define Assign(a0, a1, a2, a3, a4, a5, a6) _Py_Assign(a0, a1, a2, a3, a4, a5, a6)
-stmt_ty _Py_Assign(asdl_seq * targets, expr_ty value, int lineno, int
- col_offset, int end_lineno, int end_col_offset, PyArena
- *arena);
+#define Assign(a0, a1, a2, a3, a4, a5, a6, a7) _Py_Assign(a0, a1, a2, a3, a4, a5, a6, a7)
+stmt_ty _Py_Assign(asdl_seq * targets, expr_ty value, string type_comment, int
+ lineno, int col_offset, int end_lineno, int end_col_offset,
+ PyArena *arena);
#define AugAssign(a0, a1, a2, a3, a4, a5, a6, a7) _Py_AugAssign(a0, a1, a2, a3, a4, a5, a6, a7)
stmt_ty _Py_AugAssign(expr_ty target, operator_ty op, expr_ty value, int
lineno, int col_offset, int end_lineno, int
stmt_ty _Py_AnnAssign(expr_ty target, expr_ty annotation, expr_ty value, int
simple, int lineno, int col_offset, int end_lineno, int
end_col_offset, PyArena *arena);
-#define For(a0, a1, a2, a3, a4, a5, a6, a7, a8) _Py_For(a0, a1, a2, a3, a4, a5, a6, a7, a8)
+#define For(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) _Py_For(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9)
stmt_ty _Py_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);
-#define AsyncFor(a0, a1, a2, a3, a4, a5, a6, a7, a8) _Py_AsyncFor(a0, a1, a2, a3, a4, a5, a6, a7, a8)
+ orelse, string type_comment, int lineno, int col_offset, int
+ end_lineno, int end_col_offset, PyArena *arena);
+#define AsyncFor(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) _Py_AsyncFor(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9)
stmt_ty _Py_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);
+ orelse, string type_comment, int lineno, int col_offset,
+ int end_lineno, int end_col_offset, PyArena *arena);
#define While(a0, a1, a2, a3, a4, a5, a6, a7) _Py_While(a0, a1, a2, a3, a4, a5, a6, a7)
stmt_ty _Py_While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno,
int col_offset, int end_lineno, int end_col_offset, PyArena
stmt_ty _Py_If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno,
int col_offset, int end_lineno, int end_col_offset, PyArena
*arena);
-#define With(a0, a1, a2, a3, a4, a5, a6) _Py_With(a0, a1, a2, a3, a4, a5, a6)
-stmt_ty _Py_With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset,
- int end_lineno, int end_col_offset, PyArena *arena);
-#define AsyncWith(a0, a1, a2, a3, a4, a5, a6) _Py_AsyncWith(a0, a1, a2, a3, a4, a5, a6)
-stmt_ty _Py_AsyncWith(asdl_seq * items, asdl_seq * body, int lineno, int
- col_offset, int end_lineno, int end_col_offset, PyArena
- *arena);
+#define With(a0, a1, a2, a3, a4, a5, a6, a7) _Py_With(a0, a1, a2, a3, a4, a5, a6, a7)
+stmt_ty _Py_With(asdl_seq * items, asdl_seq * body, string type_comment, int
+ lineno, int col_offset, int end_lineno, int end_col_offset,
+ PyArena *arena);
+#define AsyncWith(a0, a1, a2, a3, a4, a5, a6, a7) _Py_AsyncWith(a0, a1, a2, a3, a4, a5, a6, a7)
+stmt_ty _Py_AsyncWith(asdl_seq * items, asdl_seq * body, string type_comment,
+ int lineno, int col_offset, int end_lineno, int
+ end_col_offset, PyArena *arena);
#define Raise(a0, a1, a2, a3, a4, a5, a6) _Py_Raise(a0, a1, a2, a3, a4, a5, a6)
stmt_ty _Py_Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, int
end_lineno, int end_col_offset, PyArena *arena);
arguments_ty _Py_arguments(asdl_seq * args, arg_ty vararg, asdl_seq *
kwonlyargs, asdl_seq * kw_defaults, arg_ty kwarg,
asdl_seq * defaults, PyArena *arena);
-#define arg(a0, a1, a2, a3, a4, a5, a6) _Py_arg(a0, a1, a2, a3, a4, a5, a6)
-arg_ty _Py_arg(identifier arg, expr_ty annotation, int lineno, int col_offset,
- int end_lineno, int end_col_offset, PyArena *arena);
+#define arg(a0, a1, a2, a3, a4, a5, a6, a7) _Py_arg(a0, a1, a2, a3, a4, a5, a6, a7)
+arg_ty _Py_arg(identifier arg, expr_ty annotation, string type_comment, int
+ lineno, int col_offset, int end_lineno, int end_col_offset,
+ PyArena *arena);
#define keyword(a0, a1, a2) _Py_keyword(a0, a1, a2)
keyword_ty _Py_keyword(identifier arg, expr_ty value, PyArena *arena);
#define alias(a0, a1, a2) _Py_alias(a0, a1, a2)
#define withitem(a0, a1, a2) _Py_withitem(a0, a1, a2)
withitem_ty _Py_withitem(expr_ty context_expr, expr_ty optional_vars, PyArena
*arena);
+#define TypeIgnore(a0, a1) _Py_TypeIgnore(a0, a1)
+type_ignore_ty _Py_TypeIgnore(int lineno, PyArena *arena);
PyObject* PyAST_mod2obj(mod_ty t);
mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode);
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[]={
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",
_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[]={
"body",
"decorator_list",
"returns",
+ "type_comment",
};
static PyTypeObject *ClassDef_type;
_Py_IDENTIFIER(bases);
static char *Assign_fields[]={
"targets",
"value",
+ "type_comment",
};
static PyTypeObject *AugAssign_type;
_Py_IDENTIFIER(target);
"iter",
"body",
"orelse",
+ "type_comment",
};
static PyTypeObject *AsyncFor_type;
static char *AsyncFor_fields[]={
"iter",
"body",
"orelse",
+ "type_comment",
};
static PyTypeObject *While_type;
_Py_IDENTIFIER(test);
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);
static char *arg_fields[]={
"arg",
"annotation",
+ "type_comment",
};
static PyTypeObject *keyword_type;
static PyObject* ast2obj_keyword(void*);
"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);
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;
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;
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;
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);
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;
}
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));
return NULL;
p->kind = Module_kind;
p->v.Module.body = body;
+ p->v.Module.type_ignores = type_ignores;
return p;
}
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)
{
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) {
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;
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) {
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;
}
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) {
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;
}
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) {
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;
}
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) {
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;
}
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));
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;
}
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));
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;
}
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) {
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;
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)
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);
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;
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);
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);
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);
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);
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);
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);
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);
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)
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)
}
if (isinstance) {
asdl_seq* body;
+ asdl_seq* type_ignores;
if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
return 1;
}
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;
}
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;
asdl_seq* body;
asdl_seq* decorator_list;
expr_ty returns;
+ string type_comment;
if (_PyObject_LookupAttrId(obj, &PyId_name, &tmp) < 0) {
return 1;
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;
}
asdl_seq* body;
asdl_seq* decorator_list;
expr_ty returns;
+ string type_comment;
if (_PyObject_LookupAttrId(obj, &PyId_name, &tmp) < 0) {
return 1;
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;
}
if (isinstance) {
asdl_seq* targets;
expr_ty value;
+ string type_comment;
if (_PyObject_LookupAttrId(obj, &PyId_targets, &tmp) < 0) {
return 1;
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;
}
expr_ty iter;
asdl_seq* body;
asdl_seq* orelse;
+ string type_comment;
if (_PyObject_LookupAttrId(obj, &PyId_target, &tmp) < 0) {
return 1;
}
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;
}
expr_ty iter;
asdl_seq* body;
asdl_seq* orelse;
+ string type_comment;
if (_PyObject_LookupAttrId(obj, &PyId_target, &tmp) < 0) {
return 1;
}
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;
}
if (isinstance) {
asdl_seq* items;
asdl_seq* body;
+ string type_comment;
if (_PyObject_LookupAttrId(obj, &PyId_items, &tmp) < 0) {
return 1;
}
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;
if (isinstance) {
asdl_seq* items;
asdl_seq* body;
+ string type_comment;
if (_PyObject_LookupAttrId(obj, &PyId_items, &tmp) < 0) {
return 1;
}
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;
}
PyObject* tmp = NULL;
identifier arg;
expr_ty annotation;
+ string type_comment;
int lineno;
int col_offset;
int end_lineno;
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;
}
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);
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"
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;
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;
}