#endif /* !Py_LIMITED_API */
-/* These definitions must match corresponding definitions in graminit.h.
- There's code in compile.c that checks that they are the same. */
+/* These definitions must match corresponding definitions in graminit.h. */
#define Py_single_input 256
#define Py_file_input 257
#define Py_eval_input 258
+#define Py_func_type_input 345
#endif /* !Py_COMPILE_H */
check_both_ways("try: # type: int\n pass\nfinally:\n pass\n")
check_both_ways("try:\n pass\nfinally: # type: int\n pass\n")
+ def test_func_type_input(self):
+ return
+
+ def parse_func_type_input(source):
+ from ast import PyCF_ONLY_AST
+ return compile(source, "<unknown>", "func_type", PyCF_ONLY_AST)
+
+ # Some checks below will crash if the return structure is wrong
+ tree = parse_func_type_input("() -> int")
+ self.assertEqual(tree.argtypes, [])
+ self.assertEqual(tree.returns.id, "int")
+
+ tree = parse_func_type_input("(int) -> List[str]")
+ self.assertEqual(len(tree.argtypes), 1)
+ arg = tree.argtypes[0]
+ self.assertEqual(arg.value.int, "int")
+ self.assertEqual(tree.returns.value.id, "List")
+ self.assertEqual(tree.returns.slice.vale.id, "str")
+
if __name__ == '__main__':
unittest.main()
}
/* 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;
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;
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;
}