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