Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(246802)

Delta Between Two Patch Sets: Python/future.c

Issue 11619: On Windows, don't encode filenames in the import machinery
Left Patch Set: Created 8 years, 3 months ago
Right Patch Set: Created 5 years, 11 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Python/errors.c ('k') | Python/pythonrun.c » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 \
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 PyErr_SyntaxLocationObject(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, PyObject *filename) 59 future_parse(PyFutureFeatures *ff, mod_ty mod, PyObject *filename)
60 { 60 {
61 int i, found_docstring = 0, done = 0, prev_line = 0; 61 int i, done = 0, prev_line = 0;
62 62 stmt_ty first;
63 static PyObject *future;
64 if (!future) {
65 future = PyUnicode_InternFromString("__future__");
66 if (!future)
67 return 0;
68 }
69 63
70 if (!(mod->kind == Module_kind || mod->kind == Interactive_kind)) 64 if (!(mod->kind == Module_kind || mod->kind == Interactive_kind))
65 return 1;
66
67 if (asdl_seq_LEN(mod->v.Module.body) == 0)
71 return 1; 68 return 1;
72 69
73 /* A subsequent pass will detect future imports that don't 70 /* A subsequent pass will detect future imports that don't
74 appear at the beginning of the file. There's one case, 71 appear at the beginning of the file. There's one case,
75 however, that is easier to handle here: A series of imports 72 however, that is easier to handle here: A series of imports
76 joined by semi-colons, where the first import is a future 73 joined by semi-colons, where the first import is a future
77 statement but some subsequent import has the future form 74 statement but some subsequent import has the future form
78 but is preceded by a regular import. 75 but is preceded by a regular import.
79 */ 76 */
80 77
78 i = 0;
79 first = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i);
80 if (first->kind == Expr_kind && first->v.Expr.value->kind == Str_kind)
81 i++;
81 82
82 for (i = 0; i < asdl_seq_LEN(mod->v.Module.body); i++) { 83
84 for (; i < asdl_seq_LEN(mod->v.Module.body); i++) {
83 stmt_ty s = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i); 85 stmt_ty s = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i);
84 86
85 if (done && s->lineno > prev_line) 87 if (done && s->lineno > prev_line)
86 return 1; 88 return 1;
87 prev_line = s->lineno; 89 prev_line = s->lineno;
88 90
89 /* The tests below will return from this function unless it is 91 /* The tests below will return from this function unless it is
90 still possible to find a future statement. The only things 92 still possible to find a future statement. The only things
91 that can precede a future statement are another future 93 that can precede a future statement are another future
92 statement and a doc string. 94 statement and a doc string.
93 */ 95 */
94 96
95 if (s->kind == ImportFrom_kind) { 97 if (s->kind == ImportFrom_kind) {
96 if (s->v.ImportFrom.module == future) { 98 identifier modname = s->v.ImportFrom.module;
99 if (modname &&
100 !PyUnicode_CompareWithASCIIString(modname, "__future__")) {
97 if (done) { 101 if (done) {
98 PyErr_SetString(PyExc_SyntaxError, 102 PyErr_SetString(PyExc_SyntaxError,
99 ERR_LATE_FUTURE); 103 ERR_LATE_FUTURE);
100 PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offse t); 104 PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offse t);
101 return 0; 105 return 0;
102 } 106 }
103 if (!future_check_features(ff, s, filename)) 107 if (!future_check_features(ff, s, filename))
104 return 0; 108 return 0;
105 ff->ff_lineno = s->lineno; 109 ff->ff_lineno = s->lineno;
106 } 110 }
107 else 111 else {
108 done = 1; 112 done = 1;
113 }
109 } 114 }
110 else if (s->kind == Expr_kind && !found_docstring) { 115 else {
111 expr_ty e = s->v.Expr.value; 116 done = 1;
112 if (e->kind != Str_kind)
113 done = 1;
114 else
115 found_docstring = 1;
116 } 117 }
117 else
118 done = 1;
119 } 118 }
120 return 1; 119 return 1;
121 } 120 }
122 121
123 122
124 PyFutureFeatures * 123 PyFutureFeatures *
125 PyFuture_FromASTObject(mod_ty mod, PyObject *filename) 124 PyFuture_FromASTObject(mod_ty mod, PyObject *filename)
126 { 125 {
127 PyFutureFeatures *ff; 126 PyFutureFeatures *ff;
128 127
(...skipping 19 matching lines...) Expand all
148 PyFutureFeatures *ff; 147 PyFutureFeatures *ff;
149 PyObject *filename; 148 PyObject *filename;
150 149
151 filename = PyUnicode_DecodeFSDefault(filename_str); 150 filename = PyUnicode_DecodeFSDefault(filename_str);
152 if (filename == NULL) 151 if (filename == NULL)
153 return NULL; 152 return NULL;
154 ff = PyFuture_FromASTObject(mod, filename); 153 ff = PyFuture_FromASTObject(mod, filename);
155 Py_DECREF(filename); 154 Py_DECREF(filename);
156 return ff; 155 return ff;
157 } 156 }
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+