OLD | NEW |
1 #include "Python.h" | 1 #include "Python.h" |
2 #include "Python-ast.h" | 2 #include "Python-ast.h" |
3 #include "node.h" | 3 #include "node.h" |
4 #include "token.h" | 4 #include "token.h" |
5 #include "graminit.h" | 5 #include "graminit.h" |
6 #include "code.h" | 6 #include "code.h" |
7 #include "symtable.h" | 7 #include "symtable.h" |
8 | 8 |
9 #define UNDEFINED_FUTURE_FEATURE "future feature %.100s is not defined" | 9 #define UNDEFINED_FUTURE_FEATURE "future feature %.100s is not defined" |
10 #define ERR_LATE_FUTURE \ | 10 #define ERR_LATE_FUTURE \ |
11 "from __future__ imports must occur at the beginning of the file" | 11 "from __future__ imports must occur at the beginning of the file" |
12 | 12 |
13 static int | 13 static int |
14 future_check_features(PyFutureFeatures *ff, stmt_ty s, const char *filename) | 14 future_check_features(PyFutureFeatures *ff, stmt_ty s, PyObject *filename) |
15 { | 15 { |
16 int i; | 16 int i; |
17 asdl_seq *names; | 17 asdl_seq *names; |
18 | 18 |
19 assert(s->kind == ImportFrom_kind); | 19 assert(s->kind == ImportFrom_kind); |
20 | 20 |
21 names = s->v.ImportFrom.names; | 21 names = s->v.ImportFrom.names; |
22 for (i = 0; i < asdl_seq_LEN(names); i++) { | 22 for (i = 0; i < asdl_seq_LEN(names); i++) { |
23 alias_ty name = (alias_ty)asdl_seq_GET(names, i); | 23 alias_ty name = (alias_ty)asdl_seq_GET(names, i); |
24 const char *feature = _PyUnicode_AsString(name->name); | 24 const char *feature = _PyUnicode_AsString(name->name); |
(...skipping 11 matching lines...) Expand all Loading... |
36 continue; | 36 continue; |
37 } else if (strcmp(feature, FUTURE_PRINT_FUNCTION) == 0) { | 37 } else if (strcmp(feature, FUTURE_PRINT_FUNCTION) == 0) { |
38 continue; | 38 continue; |
39 } else if (strcmp(feature, FUTURE_UNICODE_LITERALS) == 0) { | 39 } else if (strcmp(feature, FUTURE_UNICODE_LITERALS) == 0) { |
40 continue; | 40 continue; |
41 } else if (strcmp(feature, FUTURE_BARRY_AS_BDFL) == 0) { | 41 } else if (strcmp(feature, FUTURE_BARRY_AS_BDFL) == 0) { |
42 ff->ff_features |= CO_FUTURE_BARRY_AS_BDFL; | 42 ff->ff_features |= CO_FUTURE_BARRY_AS_BDFL; |
43 } else if (strcmp(feature, "braces") == 0) { | 43 } else if (strcmp(feature, "braces") == 0) { |
44 PyErr_SetString(PyExc_SyntaxError, | 44 PyErr_SetString(PyExc_SyntaxError, |
45 "not a chance"); | 45 "not a chance"); |
46 PyErr_SyntaxLocationEx(filename, s->lineno, s->col_offset); | 46 PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset); |
47 return 0; | 47 return 0; |
48 } else { | 48 } else { |
49 PyErr_Format(PyExc_SyntaxError, | 49 PyErr_Format(PyExc_SyntaxError, |
50 UNDEFINED_FUTURE_FEATURE, feature); | 50 UNDEFINED_FUTURE_FEATURE, feature); |
51 PyErr_SyntaxLocationEx(filename, s->lineno, s->col_offset); | 51 PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset); |
52 return 0; | 52 return 0; |
53 } | 53 } |
54 } | 54 } |
55 return 1; | 55 return 1; |
56 } | 56 } |
57 | 57 |
58 static int | 58 static int |
59 future_parse(PyFutureFeatures *ff, mod_ty mod, const char *filename) | 59 future_parse(PyFutureFeatures *ff, mod_ty mod, PyObject *filename) |
60 { | 60 { |
61 int i, done = 0, prev_line = 0; | 61 int i, done = 0, prev_line = 0; |
62 stmt_ty first; | 62 stmt_ty first; |
63 | 63 |
64 if (!(mod->kind == Module_kind || mod->kind == Interactive_kind)) | 64 if (!(mod->kind == Module_kind || mod->kind == Interactive_kind)) |
65 return 1; | 65 return 1; |
66 | 66 |
67 if (asdl_seq_LEN(mod->v.Module.body) == 0) | 67 if (asdl_seq_LEN(mod->v.Module.body) == 0) |
68 return 1; | 68 return 1; |
69 | 69 |
(...skipping 24 matching lines...) Expand all Loading... |
94 statement and a doc string. | 94 statement and a doc string. |
95 */ | 95 */ |
96 | 96 |
97 if (s->kind == ImportFrom_kind) { | 97 if (s->kind == ImportFrom_kind) { |
98 identifier modname = s->v.ImportFrom.module; | 98 identifier modname = s->v.ImportFrom.module; |
99 if (modname && | 99 if (modname && |
100 !PyUnicode_CompareWithASCIIString(modname, "__future__")) { | 100 !PyUnicode_CompareWithASCIIString(modname, "__future__")) { |
101 if (done) { | 101 if (done) { |
102 PyErr_SetString(PyExc_SyntaxError, | 102 PyErr_SetString(PyExc_SyntaxError, |
103 ERR_LATE_FUTURE); | 103 ERR_LATE_FUTURE); |
104 PyErr_SyntaxLocationEx(filename, s->lineno, s->col_offset); | 104 PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offse
t); |
105 return 0; | 105 return 0; |
106 } | 106 } |
107 if (!future_check_features(ff, s, filename)) | 107 if (!future_check_features(ff, s, filename)) |
108 return 0; | 108 return 0; |
109 ff->ff_lineno = s->lineno; | 109 ff->ff_lineno = s->lineno; |
110 } | 110 } |
111 else { | 111 else { |
112 done = 1; | 112 done = 1; |
113 } | 113 } |
114 } | 114 } |
115 else { | 115 else { |
116 done = 1; | 116 done = 1; |
117 } | 117 } |
118 } | 118 } |
119 return 1; | 119 return 1; |
120 } | 120 } |
121 | 121 |
122 | 122 |
123 PyFutureFeatures * | 123 PyFutureFeatures * |
124 PyFuture_FromAST(mod_ty mod, const char *filename) | 124 PyFuture_FromASTObject(mod_ty mod, PyObject *filename) |
125 { | 125 { |
126 PyFutureFeatures *ff; | 126 PyFutureFeatures *ff; |
127 | 127 |
128 ff = (PyFutureFeatures *)PyObject_Malloc(sizeof(PyFutureFeatures)); | 128 ff = (PyFutureFeatures *)PyObject_Malloc(sizeof(PyFutureFeatures)); |
129 if (ff == NULL) { | 129 if (ff == NULL) { |
130 PyErr_NoMemory(); | 130 PyErr_NoMemory(); |
131 return NULL; | 131 return NULL; |
132 } | 132 } |
133 ff->ff_features = 0; | 133 ff->ff_features = 0; |
134 ff->ff_lineno = -1; | 134 ff->ff_lineno = -1; |
135 | 135 |
136 if (!future_parse(ff, mod, filename)) { | 136 if (!future_parse(ff, mod, filename)) { |
137 PyObject_Free(ff); | 137 PyObject_Free(ff); |
138 return NULL; | 138 return NULL; |
139 } | 139 } |
140 return ff; | 140 return ff; |
141 } | 141 } |
| 142 |
| 143 |
| 144 PyFutureFeatures * |
| 145 PyFuture_FromAST(mod_ty mod, const char *filename_str) |
| 146 { |
| 147 PyFutureFeatures *ff; |
| 148 PyObject *filename; |
| 149 |
| 150 filename = PyUnicode_DecodeFSDefault(filename_str); |
| 151 if (filename == NULL) |
| 152 return NULL; |
| 153 ff = PyFuture_FromASTObject(mod, filename); |
| 154 Py_DECREF(filename); |
| 155 return ff; |
| 156 } |
OLD | NEW |