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