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

Delta Between Two Patch Sets: Python/ast.c

Issue 11682: PEP 380 reference implementation for 3.3
Left Patch Set: Created 8 years, 1 month ago
Right Patch Set: Created 7 years, 7 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
LEFTRIGHT
1 /* 1 /*
2 * This file includes functions to transform a concrete syntax tree (CST) to 2 * This file includes functions to transform a concrete syntax tree (CST) to
3 * an abstract syntax tree (AST). The main function is PyAST_FromNode(). 3 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
4 * 4 *
5 */ 5 */
6 #include "Python.h" 6 #include "Python.h"
7 #include "Python-ast.h" 7 #include "Python-ast.h"
8 #include "grammar.h"
9 #include "node.h" 8 #include "node.h"
10 #include "ast.h" 9 #include "ast.h"
11 #include "token.h" 10 #include "token.h"
11
12 #include <assert.h>
13
14 static int validate_stmts(asdl_seq *);
15 static int validate_exprs(asdl_seq *, expr_context_ty, int);
16 static int validate_nonempty_seq(asdl_seq *, const char *, const char *);
17 static int validate_stmt(stmt_ty);
18 static int validate_expr(expr_ty, expr_context_ty);
19
20 static int
21 validate_comprehension(asdl_seq *gens)
22 {
23 int i;
24 if (!asdl_seq_LEN(gens)) {
25 PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
26 return 0;
27 }
28 for (i = 0; i < asdl_seq_LEN(gens); i++) {
29 comprehension_ty comp = asdl_seq_GET(gens, i);
30 if (!validate_expr(comp->target, Store) ||
31 !validate_expr(comp->iter, Load) ||
32 !validate_exprs(comp->ifs, Load, 0))
33 return 0;
34 }
35 return 1;
36 }
37
38 static int
39 validate_slice(slice_ty slice)
40 {
41 switch (slice->kind) {
42 case Slice_kind:
43 return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Loa d)) &&
44 (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) &&
45 (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load));
46 case ExtSlice_kind: {
47 int i;
48 if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice"))
49 return 0;
50 for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++)
51 if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i)))
52 return 0;
53 return 1;
54 }
55 case Index_kind:
56 return validate_expr(slice->v.Index.value, Load);
57 default:
58 PyErr_SetString(PyExc_SystemError, "unknown slice node");
59 return 0;
60 }
61 }
62
63 static int
64 validate_keywords(asdl_seq *keywords)
65 {
66 int i;
67 for (i = 0; i < asdl_seq_LEN(keywords); i++)
68 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load) )
69 return 0;
70 return 1;
71 }
72
73 static int
74 validate_args(asdl_seq *args)
75 {
76 int i;
77 for (i = 0; i < asdl_seq_LEN(args); i++) {
78 arg_ty arg = asdl_seq_GET(args, i);
79 if (arg->annotation && !validate_expr(arg->annotation, Load))
80 return 0;
81 }
82 return 1;
83 }
84
85 static const char *
86 expr_context_name(expr_context_ty ctx)
87 {
88 switch (ctx) {
89 case Load:
90 return "Load";
91 case Store:
92 return "Store";
93 case Del:
94 return "Del";
95 case AugLoad:
96 return "AugLoad";
97 case AugStore:
98 return "AugStore";
99 case Param:
100 return "Param";
101 default:
102 assert(0);
103 return "(unknown)";
104 }
105 }
106
107 static int
108 validate_arguments(arguments_ty args)
109 {
110 if (!validate_args(args->args))
111 return 0;
112 if (args->varargannotation) {
113 if (!args->vararg) {
114 PyErr_SetString(PyExc_ValueError, "varargannotation but no vararg on arguments");
115 return 0;
116 }
117 if (!validate_expr(args->varargannotation, Load))
118 return 0;
119 }
120 if (!validate_args(args->kwonlyargs))
121 return 0;
122 if (args->kwargannotation) {
123 if (!args->kwarg) {
124 PyErr_SetString(PyExc_ValueError, "kwargannotation but no kwarg on a rguments");
125 return 0;
126 }
127 if (!validate_expr(args->kwargannotation, Load))
128 return 0;
129 }
130 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) {
131 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
132 return 0;
133 }
134 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
135 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
136 "kw_defaults on arguments");
137 return 0;
138 }
139 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_de faults, Load, 1);
140 }
141
142 static int
143 validate_expr(expr_ty exp, expr_context_ty ctx)
144 {
145 int check_ctx = 1;
146 expr_context_ty actual_ctx;
147
148 /* First check expression context. */
149 switch (exp->kind) {
150 case Attribute_kind:
151 actual_ctx = exp->v.Attribute.ctx;
152 break;
153 case Subscript_kind:
154 actual_ctx = exp->v.Subscript.ctx;
155 break;
156 case Starred_kind:
157 actual_ctx = exp->v.Starred.ctx;
158 break;
159 case Name_kind:
160 actual_ctx = exp->v.Name.ctx;
161 break;
162 case List_kind:
163 actual_ctx = exp->v.List.ctx;
164 break;
165 case Tuple_kind:
166 actual_ctx = exp->v.Tuple.ctx;
167 break;
168 default:
169 if (ctx != Load) {
170 PyErr_Format(PyExc_ValueError, "expression which can't be "
171 "assigned to in %s context", expr_context_name(ctx));
172 return 0;
173 }
174 check_ctx = 0;
175 }
176 if (check_ctx && actual_ctx != ctx) {
177 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
178 expr_context_name(ctx), expr_context_name(actual_ctx));
179 return 0;
180 }
181
182 /* Now validate expression. */
183 switch (exp->kind) {
184 case BoolOp_kind:
185 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
186 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
187 return 0;
188 }
189 return validate_exprs(exp->v.BoolOp.values, Load, 0);
190 case BinOp_kind:
191 return validate_expr(exp->v.BinOp.left, Load) &&
192 validate_expr(exp->v.BinOp.right, Load);
193 case UnaryOp_kind:
194 return validate_expr(exp->v.UnaryOp.operand, Load);
195 case Lambda_kind:
196 return validate_arguments(exp->v.Lambda.args) &&
197 validate_expr(exp->v.Lambda.body, Load);
198 case IfExp_kind:
199 return validate_expr(exp->v.IfExp.test, Load) &&
200 validate_expr(exp->v.IfExp.body, Load) &&
201 validate_expr(exp->v.IfExp.orelse, Load);
202 case Dict_kind:
203 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
204 PyErr_SetString(PyExc_ValueError,
205 "Dict doesn't have the same number of keys as values ");
206 return 0;
207 }
208 return validate_exprs(exp->v.Dict.keys, Load, 0) &&
209 validate_exprs(exp->v.Dict.values, Load, 0);
210 case Set_kind:
211 return validate_exprs(exp->v.Set.elts, Load, 0);
212 #define COMP(NAME) \
213 case NAME ## _kind: \
214 return validate_comprehension(exp->v.NAME.generators) && \
215 validate_expr(exp->v.NAME.elt, Load);
216 COMP(ListComp)
217 COMP(SetComp)
218 COMP(GeneratorExp)
219 #undef COMP
220 case DictComp_kind:
221 return validate_comprehension(exp->v.DictComp.generators) &&
222 validate_expr(exp->v.DictComp.key, Load) &&
223 validate_expr(exp->v.DictComp.value, Load);
224 case Yield_kind:
225 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
226 case Compare_kind:
227 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
228 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
229 return 0;
230 }
231 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
232 asdl_seq_LEN(exp->v.Compare.ops)) {
233 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
234 "of comparators and operands");
235 return 0;
236 }
237 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
238 validate_expr(exp->v.Compare.left, Load);
239 case Call_kind:
240 return validate_expr(exp->v.Call.func, Load) &&
241 validate_exprs(exp->v.Call.args, Load, 0) &&
242 validate_keywords(exp->v.Call.keywords) &&
243 (!exp->v.Call.starargs || validate_expr(exp->v.Call.starargs, Load)) &&
244 (!exp->v.Call.kwargs || validate_expr(exp->v.Call.kwargs, Load));
245 case Num_kind: {
246 PyObject *n = exp->v.Num.n;
247 if (!PyLong_CheckExact(n) && !PyFloat_CheckExact(n) &&
248 !PyComplex_CheckExact(n)) {
249 PyErr_SetString(PyExc_TypeError, "non-numeric type in Num");
250 return 0;
251 }
252 return 1;
253 }
254 case Str_kind: {
255 PyObject *s = exp->v.Str.s;
256 if (!PyUnicode_CheckExact(s)) {
257 PyErr_SetString(PyExc_TypeError, "non-string type in Str");
258 return 0;
259 }
260 return 1;
261 }
262 case Bytes_kind: {
263 PyObject *b = exp->v.Bytes.s;
264 if (!PyBytes_CheckExact(b)) {
265 PyErr_SetString(PyExc_TypeError, "non-bytes type in Bytes");
266 return 0;
267 }
268 return 1;
269 }
270 case Attribute_kind:
271 return validate_expr(exp->v.Attribute.value, Load);
272 case Subscript_kind:
273 return validate_slice(exp->v.Subscript.slice) &&
274 validate_expr(exp->v.Subscript.value, Load);
275 case Starred_kind:
276 return validate_expr(exp->v.Starred.value, ctx);
277 case List_kind:
278 return validate_exprs(exp->v.List.elts, ctx, 0);
279 case Tuple_kind:
280 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
281 /* These last cases don't have any checking. */
282 case Name_kind:
283 case Ellipsis_kind:
284 return 1;
285 default:
286 PyErr_SetString(PyExc_SystemError, "unexpected expression");
287 return 0;
288 }
289 }
290
291 static int
292 validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
293 {
294 if (asdl_seq_LEN(seq))
295 return 1;
296 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
297 return 0;
298 }
299
300 static int
301 validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
302 {
303 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "As sign") &&
304 validate_exprs(targets, ctx, 0);
305 }
306
307 static int
308 validate_body(asdl_seq *body, const char *owner)
309 {
310 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
311 }
312
313 static int
314 validate_stmt(stmt_ty stmt)
315 {
316 int i;
317 switch (stmt->kind) {
318 case FunctionDef_kind:
319 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
320 validate_arguments(stmt->v.FunctionDef.args) &&
321 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
322 (!stmt->v.FunctionDef.returns ||
323 validate_expr(stmt->v.FunctionDef.returns, Load));
324 case ClassDef_kind:
325 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
326 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
327 validate_keywords(stmt->v.ClassDef.keywords) &&
328 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0) &&
329 (!stmt->v.ClassDef.starargs || validate_expr(stmt->v.ClassDef.starar gs, Load)) &&
330 (!stmt->v.ClassDef.kwargs || validate_expr(stmt->v.ClassDef.kwargs, Load));
331 case Return_kind:
332 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load );
333 case Delete_kind:
334 return validate_assignlist(stmt->v.Delete.targets, Del);
335 case Assign_kind:
336 return validate_assignlist(stmt->v.Assign.targets, Store) &&
337 validate_expr(stmt->v.Assign.value, Load);
338 case AugAssign_kind:
339 return validate_expr(stmt->v.AugAssign.target, Store) &&
340 validate_expr(stmt->v.AugAssign.value, Load);
341 case For_kind:
342 return validate_expr(stmt->v.For.target, Store) &&
343 validate_expr(stmt->v.For.iter, Load) &&
344 validate_body(stmt->v.For.body, "For") &&
345 validate_stmts(stmt->v.For.orelse);
346 case While_kind:
347 return validate_expr(stmt->v.While.test, Load) &&
348 validate_body(stmt->v.While.body, "While") &&
349 validate_stmts(stmt->v.While.orelse);
350 case If_kind:
351 return validate_expr(stmt->v.If.test, Load) &&
352 validate_body(stmt->v.If.body, "If") &&
353 validate_stmts(stmt->v.If.orelse);
354 case With_kind:
355 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
356 return 0;
357 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
358 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
359 if (!validate_expr(item->context_expr, Load) ||
360 (item->optional_vars && !validate_expr(item->optional_vars, Stor e)))
361 return 0;
362 }
363 return validate_body(stmt->v.With.body, "With");
364 case Raise_kind:
365 if (stmt->v.Raise.exc) {
366 return validate_expr(stmt->v.Raise.exc, Load) &&
367 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load ));
368 }
369 if (stmt->v.Raise.cause) {
370 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception ");
371 return 0;
372 }
373 return 1;
374 case Try_kind:
375 if (!validate_body(stmt->v.Try.body, "Try"))
376 return 0;
377 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
378 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
379 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers n or finalbody");
380 return 0;
381 }
382 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
383 asdl_seq_LEN(stmt->v.Try.orelse)) {
384 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except hand lers");
385 return 0;
386 }
387 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
388 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
389 if ((handler->v.ExceptHandler.type &&
390 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
391 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
392 return 0;
393 }
394 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
395 validate_stmts(stmt->v.Try.finalbody)) &&
396 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
397 validate_stmts(stmt->v.Try.orelse));
398 case Assert_kind:
399 return validate_expr(stmt->v.Assert.test, Load) &&
400 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
401 case Import_kind:
402 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
403 case ImportFrom_kind:
404 if (stmt->v.ImportFrom.level < -1) {
405 PyErr_SetString(PyExc_ValueError, "ImportFrom level less than -1");
406 return 0;
407 }
408 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportF rom");
409 case Global_kind:
410 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
411 case Nonlocal_kind:
412 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal" );
413 case Expr_kind:
414 return validate_expr(stmt->v.Expr.value, Load);
415 case Pass_kind:
416 case Break_kind:
417 case Continue_kind:
418 return 1;
419 default:
420 PyErr_SetString(PyExc_SystemError, "unexpected statement");
421 return 0;
422 }
423 }
424
425 static int
426 validate_stmts(asdl_seq *seq)
427 {
428 int i;
429 for (i = 0; i < asdl_seq_LEN(seq); i++) {
430 stmt_ty stmt = asdl_seq_GET(seq, i);
431 if (stmt) {
432 if (!validate_stmt(stmt))
433 return 0;
434 }
435 else {
436 PyErr_SetString(PyExc_ValueError,
437 "None disallowed in statement list");
438 return 0;
439 }
440 }
441 return 1;
442 }
443
444 static int
445 validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
446 {
447 int i;
448 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
449 expr_ty expr = asdl_seq_GET(exprs, i);
450 if (expr) {
451 if (!validate_expr(expr, ctx))
452 return 0;
453 }
454 else if (!null_ok) {
455 PyErr_SetString(PyExc_ValueError,
456 "None disallowed in expression list");
457 return 0;
458 }
459
460 }
461 return 1;
462 }
463
464 int
465 PyAST_Validate(mod_ty mod)
466 {
467 int res = 0;
468
469 switch (mod->kind) {
470 case Module_kind:
471 res = validate_stmts(mod->v.Module.body);
472 break;
473 case Interactive_kind:
474 res = validate_stmts(mod->v.Interactive.body);
475 break;
476 case Expression_kind:
477 res = validate_expr(mod->v.Expression.body, Load);
478 break;
479 case Suite_kind:
480 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython com piler");
481 break;
482 default:
483 PyErr_SetString(PyExc_SystemError, "impossible module node");
484 res = 0;
485 break;
486 }
487 return res;
488 }
489
490 /* This is down here, so defines like "test" don't intefere with access AST abov e. */
491 #include "grammar.h"
12 #include "parsetok.h" 492 #include "parsetok.h"
13 #include "graminit.h" 493 #include "graminit.h"
14
15 #include <assert.h>
16 494
17 /* Data structure used internally */ 495 /* Data structure used internally */
18 struct compiling { 496 struct compiling {
19 char *c_encoding; /* source encoding */ 497 char *c_encoding; /* source encoding */
20 PyArena *c_arena; /* arena for allocating memeory */ 498 PyArena *c_arena; /* arena for allocating memeory */
21 const char *c_filename; /* filename */ 499 const char *c_filename; /* filename */
22 }; 500 };
23 501
24 static asdl_seq *seq_for_testlist(struct compiling *, const node *); 502 static asdl_seq *seq_for_testlist(struct compiling *, const node *);
25 static expr_ty ast_for_expr(struct compiling *, const node *); 503 static expr_ty ast_for_expr(struct compiling *, const node *);
(...skipping 16 matching lines...) Expand all
42 #define LINENO(n) ((n)->n_lineno) 520 #define LINENO(n) ((n)->n_lineno)
43 #endif 521 #endif
44 522
45 #define COMP_GENEXP 0 523 #define COMP_GENEXP 0
46 #define COMP_LISTCOMP 1 524 #define COMP_LISTCOMP 1
47 #define COMP_SETCOMP 2 525 #define COMP_SETCOMP 2
48 526
49 static identifier 527 static identifier
50 new_identifier(const char* n, PyArena *arena) 528 new_identifier(const char* n, PyArena *arena)
51 { 529 {
530 _Py_IDENTIFIER(normalize);
52 PyObject* id = PyUnicode_DecodeUTF8(n, strlen(n), NULL); 531 PyObject* id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
53 Py_UNICODE *u; 532 if (!id || PyUnicode_READY(id) == -1)
54 if (!id) 533 return NULL;
55 return NULL;
56 u = PyUnicode_AS_UNICODE(id);
57 /* Check whether there are non-ASCII characters in the 534 /* Check whether there are non-ASCII characters in the
58 identifier; if so, normalize to NFKC. */ 535 identifier; if so, normalize to NFKC. */
59 for (; *u; u++) { 536 if (PyUnicode_MAX_CHAR_VALUE((PyUnicodeObject *)id) >= 128) {
60 if (*u >= 128) { 537 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
61 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata"); 538 PyObject *id2;
62 PyObject *id2; 539 if (!m)
63 if (!m) 540 return NULL;
64 return NULL; 541 id2 = _PyObject_CallMethodId(m, &PyId_normalize, "sO", "NFKC", id);
65 id2 = PyObject_CallMethod(m, "normalize", "sO", "NFKC", id); 542 Py_DECREF(m);
66 Py_DECREF(m); 543 if (!id2)
67 if (!id2) 544 return NULL;
68 return NULL; 545 Py_DECREF(id);
69 Py_DECREF(id); 546 id = id2;
70 id = id2;
71 break;
72 }
73 } 547 }
74 PyUnicode_InternInPlace(&id); 548 PyUnicode_InternInPlace(&id);
75 PyArena_AddPyObject(arena, id); 549 PyArena_AddPyObject(arena, id);
76 return id; 550 return id;
77 } 551 }
78 552
79 #define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena) 553 #define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
80 554
81 /* This routine provides an invalid object for the syntax error. 555 /* This routine provides an invalid object for the syntax error.
82 The outermost routine must unpack this error and create the 556 The outermost routine must unpack this error and create the
(...skipping 1824 matching lines...) Expand 10 before | Expand all | Expand 10 after
1907 is_from = 1; 2381 is_from = 1;
1908 exp = ast_for_expr(c, en); 2382 exp = ast_for_expr(c, en);
1909 } 2383 }
1910 else 2384 else
1911 exp = ast_for_testlist(c, en); 2385 exp = ast_for_testlist(c, en);
1912 if (!exp) 2386 if (!exp)
1913 return NULL; 2387 return NULL;
1914 } 2388 }
1915 return Yield(is_from, exp, LINENO(n), n->n_col_offset, c->c_arena); 2389 return Yield(is_from, exp, LINENO(n), n->n_col_offset, c->c_arena);
1916 } 2390 }
1917 case yield_from: {
1918 expr_ty exp = ast_for_expr(c, CHILD(n, 2));
1919 return Yield(1, exp, LINENO(n), n->n_col_offset, c->c_arena);
1920 }
1921 case factor: 2391 case factor:
1922 if (NCH(n) == 1) { 2392 if (NCH(n) == 1) {
1923 n = CHILD(n, 0); 2393 n = CHILD(n, 0);
1924 goto loop; 2394 goto loop;
1925 } 2395 }
1926 return ast_for_factor(c, n); 2396 return ast_for_factor(c, n);
1927 case power: 2397 case power:
1928 return ast_for_power(c, n); 2398 return ast_for_power(c, n);
1929 default: 2399 default:
1930 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n)); 2400 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
1931 return NULL; 2401 return NULL;
1932 } 2402 }
1933 /* should never get here unless if error is set */ 2403 /* should never get here unless if error is set */
1934 return NULL; 2404 return NULL;
1935 } 2405 }
1936 2406
1937 static expr_ty 2407 static expr_ty
1938 ast_for_call(struct compiling *c, const node *n, expr_ty func) 2408 ast_for_call(struct compiling *c, const node *n, expr_ty func)
1939 { 2409 {
1940 /* 2410 /*
1941 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] 2411 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1942 | '**' test) 2412 | '**' test)
1943 argument: [test '='] (test | yield_from) [comp_for] # Really [keywo rd '='] test 2413 argument: [test '='] (test) [comp_for] # Really [keyword '='] test
1944 */ 2414 */
1945 2415
1946 int i, nargs, nkeywords, ngens; 2416 int i, nargs, nkeywords, ngens;
1947 asdl_seq *args; 2417 asdl_seq *args;
1948 asdl_seq *keywords; 2418 asdl_seq *keywords;
1949 expr_ty vararg = NULL, kwarg = NULL; 2419 expr_ty vararg = NULL, kwarg = NULL;
1950 2420
1951 REQ(n, arglist); 2421 REQ(n, arglist);
1952 2422
1953 nargs = 0; 2423 nargs = 0;
(...skipping 1236 matching lines...) Expand 10 before | Expand all | Expand 10 after
3190 else 3660 else
3191 { 3661 {
3192 dx = PyOS_string_to_double(s, NULL, NULL); 3662 dx = PyOS_string_to_double(s, NULL, NULL);
3193 if (dx == -1.0 && PyErr_Occurred()) 3663 if (dx == -1.0 && PyErr_Occurred())
3194 return NULL; 3664 return NULL;
3195 return PyFloat_FromDouble(dx); 3665 return PyFloat_FromDouble(dx);
3196 } 3666 }
3197 } 3667 }
3198 3668
3199 static PyObject * 3669 static PyObject *
3200 decode_utf8(struct compiling *c, const char **sPtr, const char *end, char* encod ing) 3670 decode_utf8(struct compiling *c, const char **sPtr, const char *end)
3201 { 3671 {
3202 PyObject *u, *v;
3203 char *s, *t; 3672 char *s, *t;
3204 t = s = (char *)*sPtr; 3673 t = s = (char *)*sPtr;
3205 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ 3674 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
3206 while (s < end && (*s & 0x80)) s++; 3675 while (s < end && (*s & 0x80)) s++;
3207 *sPtr = s; 3676 *sPtr = s;
3208 u = PyUnicode_DecodeUTF8(t, s - t, NULL); 3677 return PyUnicode_DecodeUTF8(t, s - t, NULL);
3209 if (u == NULL)
3210 return NULL;
3211 v = PyUnicode_AsEncodedString(u, encoding, NULL);
3212 Py_DECREF(u);
3213 return v;
3214 } 3678 }
3215 3679
3216 static PyObject * 3680 static PyObject *
3217 decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, cons t char *encoding) 3681 decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, cons t char *encoding)
3218 { 3682 {
3219 PyObject *v, *u; 3683 PyObject *v, *u;
3220 char *buf; 3684 char *buf;
3221 char *p; 3685 char *p;
3222 const char *end; 3686 const char *end;
3223 3687
(...skipping 13 matching lines...) Expand all
3237 while (s < end) { 3701 while (s < end) {
3238 if (*s == '\\') { 3702 if (*s == '\\') {
3239 *p++ = *s++; 3703 *p++ = *s++;
3240 if (*s & 0x80) { 3704 if (*s & 0x80) {
3241 strcpy(p, "u005c"); 3705 strcpy(p, "u005c");
3242 p += 5; 3706 p += 5;
3243 } 3707 }
3244 } 3708 }
3245 if (*s & 0x80) { /* XXX inefficient */ 3709 if (*s & 0x80) { /* XXX inefficient */
3246 PyObject *w; 3710 PyObject *w;
3247 char *r; 3711 int kind;
3248 Py_ssize_t rn, i; 3712 void *data;
3249 w = decode_utf8(c, &s, end, "utf-32-be"); 3713 Py_ssize_t len, i;
3714 w = decode_utf8(c, &s, end);
3250 if (w == NULL) { 3715 if (w == NULL) {
3251 Py_DECREF(u); 3716 Py_DECREF(u);
3252 return NULL; 3717 return NULL;
3253 } 3718 }
3254 r = PyBytes_AS_STRING(w); 3719 kind = PyUnicode_KIND(w);
3255 rn = Py_SIZE(w); 3720 data = PyUnicode_DATA(w);
3256 assert(rn % 4 == 0); 3721 len = PyUnicode_GET_LENGTH(w);
3257 for (i = 0; i < rn; i += 4) { 3722 for (i = 0; i < len; i++) {
3258 sprintf(p, "\\U%02x%02x%02x%02x", 3723 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
3259 r[i + 0] & 0xFF, 3724 sprintf(p, "\\U%08x", chr);
3260 r[i + 1] & 0xFF,
3261 r[i + 2] & 0xFF,
3262 r[i + 3] & 0xFF);
3263 p += 10; 3725 p += 10;
3264 } 3726 }
3265 /* Should be impossible to overflow */ 3727 /* Should be impossible to overflow */
3266 assert(p - buf <= Py_SIZE(u)); 3728 assert(p - buf <= Py_SIZE(u));
3267 Py_DECREF(w); 3729 Py_DECREF(w);
3268 } else { 3730 } else {
3269 *p++ = *s++; 3731 *p++ = *s++;
3270 } 3732 }
3271 } 3733 }
3272 len = p - buf; 3734 len = p - buf;
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
3398 goto onError; 3860 goto onError;
3399 } 3861 }
3400 } 3862 }
3401 } 3863 }
3402 return v; 3864 return v;
3403 3865
3404 onError: 3866 onError:
3405 Py_XDECREF(v); 3867 Py_XDECREF(v);
3406 return NULL; 3868 return NULL;
3407 } 3869 }
LEFTRIGHT

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