Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 1 | #include "Python.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2 | #include "Python-ast.h" |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 3 | #include "node.h" |
| 4 | #include "token.h" |
| 5 | #include "graminit.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6 | #include "code.h" |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 7 | #include "compile.h" |
| 8 | #include "symtable.h" |
| 9 | |
| 10 | #define UNDEFINED_FUTURE_FEATURE "future feature %.100s is not defined" |
| 11 | |
| 12 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 13 | future_check_features(PyFutureFeatures *ff, stmt_ty s, const char *filename) |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 14 | { |
| 15 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 16 | asdl_seq *names; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 17 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 18 | assert(s->kind == ImportFrom_kind); |
| 19 | |
| 20 | names = s->v.ImportFrom.names; |
| 21 | for (i = 0; i < asdl_seq_LEN(names); i++) { |
| 22 | alias_ty name = asdl_seq_GET(names, i); |
Neal Norwitz | b15ec09 | 2005-12-06 07:26:02 +0000 | [diff] [blame] | 23 | const char *feature = PyString_AsString(name->name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 24 | if (!feature) |
| 25 | return 0; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 26 | if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) { |
Jeremy Hylton | b857ba2 | 2001-08-10 21:41:33 +0000 | [diff] [blame] | 27 | continue; |
Guido van Rossum | b09f7ed | 2001-07-15 21:08:29 +0000 | [diff] [blame] | 28 | } else if (strcmp(feature, FUTURE_GENERATORS) == 0) { |
Tim Peters | 2bbdba3 | 2002-04-12 01:20:10 +0000 | [diff] [blame] | 29 | continue; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 30 | } else if (strcmp(feature, FUTURE_DIVISION) == 0) { |
Jeremy Hylton | b857ba2 | 2001-08-10 21:41:33 +0000 | [diff] [blame] | 31 | ff->ff_features |= CO_FUTURE_DIVISION; |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 32 | } else if (strcmp(feature, "braces") == 0) { |
| 33 | PyErr_SetString(PyExc_SyntaxError, |
| 34 | "not a chance"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 35 | PyErr_SyntaxLocation(filename, s->lineno); |
| 36 | return 0; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 37 | } else { |
| 38 | PyErr_Format(PyExc_SyntaxError, |
| 39 | UNDEFINED_FUTURE_FEATURE, feature); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 40 | PyErr_SyntaxLocation(filename, s->lineno); |
| 41 | return 0; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 42 | } |
| 43 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 44 | return 1; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Neal Norwitz | 6576bd8 | 2005-11-13 18:41:28 +0000 | [diff] [blame] | 47 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 48 | future_parse(PyFutureFeatures *ff, mod_ty mod, const char *filename) |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 49 | { |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 50 | int i, found_docstring = 0, done = 0, prev_line = 0; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 51 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 52 | static PyObject *future; |
| 53 | if (!future) { |
| 54 | future = PyString_InternFromString("__future__"); |
| 55 | if (!future) |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 56 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 57 | } |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 58 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 59 | if (!(mod->kind == Module_kind || mod->kind == Interactive_kind)) |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 60 | return 1; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 61 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 62 | /* A subsequent pass will detect future imports that don't |
| 63 | appear at the beginning of the file. There's one case, |
| 64 | however, that is easier to handl here: A series of imports |
| 65 | joined by semi-colons, where the first import is a future |
| 66 | statement but some subsequent import has the future form |
| 67 | but is preceded by a regular import. |
| 68 | */ |
| 69 | |
| 70 | |
| 71 | for (i = 0; i < asdl_seq_LEN(mod->v.Module.body); i++) { |
| 72 | stmt_ty s = asdl_seq_GET(mod->v.Module.body, i); |
| 73 | |
| 74 | if (done && s->lineno > prev_line) |
| 75 | return 1; |
| 76 | prev_line = s->lineno; |
| 77 | |
| 78 | /* The tests below will return from this function unless it is |
| 79 | still possible to find a future statement. The only things |
| 80 | that can precede a future statement are another future |
| 81 | statement and a doc string. |
| 82 | */ |
| 83 | |
| 84 | if (s->kind == ImportFrom_kind) { |
| 85 | if (s->v.ImportFrom.module == future) { |
| 86 | if (done) { |
| 87 | PyErr_SetString(PyExc_SyntaxError, |
| 88 | ERR_LATE_FUTURE); |
| 89 | PyErr_SyntaxLocation(filename, |
| 90 | s->lineno); |
| 91 | return 0; |
| 92 | } |
| 93 | if (!future_check_features(ff, s, filename)) |
| 94 | return 0; |
| 95 | ff->ff_lineno = s->lineno; |
| 96 | } |
| 97 | else |
| 98 | done = 1; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 99 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 100 | else if (s->kind == Expr_kind && !found_docstring) { |
| 101 | expr_ty e = s->v.Expr.value; |
| 102 | if (e->kind != Str_kind) |
| 103 | done = 1; |
| 104 | else |
| 105 | found_docstring = 1; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 106 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 107 | else |
| 108 | done = 1; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 109 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 110 | return 1; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 113 | |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 114 | PyFutureFeatures * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 115 | PyFuture_FromAST(mod_ty mod, const char *filename) |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 116 | { |
| 117 | PyFutureFeatures *ff; |
| 118 | |
| 119 | ff = (PyFutureFeatures *)PyMem_Malloc(sizeof(PyFutureFeatures)); |
| 120 | if (ff == NULL) |
| 121 | return NULL; |
Jeremy Hylton | b857ba2 | 2001-08-10 21:41:33 +0000 | [diff] [blame] | 122 | ff->ff_features = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 123 | ff->ff_lineno = -1; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 124 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 125 | if (!future_parse(ff, mod, filename)) { |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 126 | PyMem_Free((void *)ff); |
| 127 | return NULL; |
| 128 | } |
| 129 | return ff; |
| 130 | } |