diff options
Diffstat (limited to 'Python/future.c')
-rw-r--r-- | Python/future.c | 53 |
1 files changed, 36 insertions, 17 deletions
diff --git a/Python/future.c b/Python/future.c index d24ae41..81eab54 100644 --- a/Python/future.c +++ b/Python/future.c @@ -11,7 +11,7 @@ "from __future__ imports must occur at the beginning of the file" static int -future_check_features(PyFutureFeatures *ff, stmt_ty s, const char *filename) +future_check_features(PyFutureFeatures *ff, stmt_ty s, PyObject *filename) { int i; asdl_seq *names; @@ -43,12 +43,12 @@ future_check_features(PyFutureFeatures *ff, stmt_ty s, const char *filename) } else if (strcmp(feature, "braces") == 0) { PyErr_SetString(PyExc_SyntaxError, "not a chance"); - PyErr_SyntaxLocationEx(filename, s->lineno, s->col_offset); + PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset); return 0; } else { PyErr_Format(PyExc_SyntaxError, UNDEFINED_FUTURE_FEATURE, feature); - PyErr_SyntaxLocationEx(filename, s->lineno, s->col_offset); + PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset); return 0; } } @@ -56,13 +56,17 @@ future_check_features(PyFutureFeatures *ff, stmt_ty s, const char *filename) } static int -future_parse(PyFutureFeatures *ff, mod_ty mod, const char *filename) +future_parse(PyFutureFeatures *ff, mod_ty mod, PyObject *filename) { - int i, found_docstring = 0, done = 0, prev_line = 0; + int i, done = 0, prev_line = 0; + stmt_ty first; if (!(mod->kind == Module_kind || mod->kind == Interactive_kind)) return 1; + if (asdl_seq_LEN(mod->v.Module.body) == 0) + return 1; + /* A subsequent pass will detect future imports that don't appear at the beginning of the file. There's one case, however, that is easier to handle here: A series of imports @@ -71,8 +75,13 @@ future_parse(PyFutureFeatures *ff, mod_ty mod, const char *filename) but is preceded by a regular import. */ + i = 0; + first = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i); + if (first->kind == Expr_kind && first->v.Expr.value->kind == Str_kind) + i++; - for (i = 0; i < asdl_seq_LEN(mod->v.Module.body); i++) { + + for (; i < asdl_seq_LEN(mod->v.Module.body); i++) { stmt_ty s = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i); if (done && s->lineno > prev_line) @@ -92,32 +101,27 @@ future_parse(PyFutureFeatures *ff, mod_ty mod, const char *filename) if (done) { PyErr_SetString(PyExc_SyntaxError, ERR_LATE_FUTURE); - PyErr_SyntaxLocationEx(filename, s->lineno, s->col_offset); + PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset); return 0; } if (!future_check_features(ff, s, filename)) return 0; ff->ff_lineno = s->lineno; } - else - done = 1; - } - else if (s->kind == Expr_kind && !found_docstring) { - expr_ty e = s->v.Expr.value; - if (e->kind != Str_kind) + else { done = 1; - else - found_docstring = 1; + } } - else + else { done = 1; + } } return 1; } PyFutureFeatures * -PyFuture_FromAST(mod_ty mod, const char *filename) +PyFuture_FromASTObject(mod_ty mod, PyObject *filename) { PyFutureFeatures *ff; @@ -135,3 +139,18 @@ PyFuture_FromAST(mod_ty mod, const char *filename) } return ff; } + + +PyFutureFeatures * +PyFuture_FromAST(mod_ty mod, const char *filename_str) +{ + PyFutureFeatures *ff; + PyObject *filename; + + filename = PyUnicode_DecodeFSDefault(filename_str); + if (filename == NULL) + return NULL; + ff = PyFuture_FromASTObject(mod, filename); + Py_DECREF(filename); + return ff; +} |