Changeset 391 for python/trunk/Python/ast.c
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Python/ast.c
r2 r391 32 32 static expr_ty ast_for_testlist(struct compiling *, const node *); 33 33 static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *); 34 static expr_ty ast_for_testlist_ gexp(struct compiling *, const node *);34 static expr_ty ast_for_testlist_comp(struct compiling *, const node *); 35 35 36 36 /* Note different signature for ast_for_call */ … … 44 44 #define LINENO(n) ((n)->n_lineno) 45 45 #endif 46 47 #define COMP_GENEXP 0 48 #define COMP_SETCOMP 1 46 49 47 50 static identifier … … 132 135 { 133 136 if (!strcmp(x, "None")) 134 return ast_error(n, "assignment to None"); 135 if (Py_Py3kWarningFlag && !(strcmp(x, "True") && strcmp(x, "False")) && 136 !ast_warn(c, n, "assignment to True or False is forbidden in 3.x")) 137 return 0; 137 return ast_error(n, "cannot assign to None"); 138 if (!strcmp(x, "__debug__")) 139 return ast_error(n, "cannot assign to __debug__"); 140 if (Py_Py3kWarningFlag) { 141 if (!(strcmp(x, "True") && strcmp(x, "False")) && 142 !ast_warn(c, n, "assignment to True or False is forbidden in 3.x")) 143 return 0; 144 if (!strcmp(x, "nonlocal") && 145 !ast_warn(c, n, "nonlocal is a keyword in 3.x")) 146 return 0; 147 } 138 148 return 1; 139 149 } … … 191 201 char buf[128]; 192 202 193 sprintf(buf, "Non-statement found: %d %d \n",203 sprintf(buf, "Non-statement found: %d %d", 194 204 TYPE(n), NCH(n)); 195 205 Py_FatalError(buf); … … 262 272 expr_ty testlist_ast; 263 273 264 /* XXX Why not gen_for here? */274 /* XXX Why not comp_for here? */ 265 275 testlist_ast = ast_for_testlist(&c, CHILD(n, 0)); 266 276 if (!testlist_ast) … … 396 406 break; 397 407 case Tuple_kind: 398 if (asdl_seq_LEN(e->v.Tuple.elts) == 0) 399 return ast_error(n, "can't assign to ()"); 400 e->v.Tuple.ctx = ctx; 401 s = e->v.Tuple.elts; 408 if (asdl_seq_LEN(e->v.Tuple.elts)) { 409 e->v.Tuple.ctx = ctx; 410 s = e->v.Tuple.elts; 411 } 412 else { 413 expr_name = "()"; 414 } 402 415 break; 403 416 case Lambda_kind: … … 421 434 expr_name = "list comprehension"; 422 435 break; 436 case SetComp_kind: 437 expr_name = "set comprehension"; 438 break; 439 case DictComp_kind: 440 expr_name = "dict comprehension"; 441 break; 423 442 case Dict_kind: 443 case Set_kind: 424 444 case Num_kind: 425 445 case Str_kind: … … 436 456 break; 437 457 default: 438 PyErr_Format(PyExc_SystemError, 439 "unexpected expression in assignment %d (line %d)", 458 PyErr_Format(PyExc_SystemError, 459 "unexpected expression in assignment %d (line %d)", 440 460 e->kind, e->lineno); 441 461 return 0; … … 452 472 453 473 /* If the LHS is a list or tuple, we need to set the assignment 454 context for all the contained elements. 474 context for all the contained elements. 455 475 */ 456 476 if (s) { … … 564 584 assert(TYPE(n) == testlist || 565 585 TYPE(n) == listmaker || 566 TYPE(n) == testlist_ gexp ||586 TYPE(n) == testlist_comp || 567 587 TYPE(n) == testlist_safe || 568 588 TYPE(n) == testlist1); … … 669 689 args = (n_args ? asdl_seq_new(n_args, c->c_arena) : NULL); 670 690 if (!args && n_args) 671 return NULL; /* Don't need to goto error; no objects allocated */691 return NULL; 672 692 defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL); 673 693 if (!defaults && n_defaults) 674 return NULL; /* Don't need to goto error; no objects allocated */694 return NULL; 675 695 676 696 /* fpdef: NAME | '(' fplist ')' … … 692 712 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2)); 693 713 if (!expression) 694 goto error;714 return NULL; 695 715 assert(defaults != NULL); 696 716 asdl_seq_SET(defaults, j++, expression); … … 703 723 if (parenthesized && !complex_args) { 704 724 ast_error(n, "parenthesized arg with default"); 705 goto error;725 return NULL; 706 726 } 707 ast_error(n, 727 ast_error(n, 708 728 "non-default argument follows default argument"); 709 goto error;729 return NULL; 710 730 } 711 731 if (NCH(ch) == 3) { … … 716 736 if (Py_Py3kWarningFlag && !ast_warn(c, ch, 717 737 "tuple parameter unpacking has been removed in 3.x")) 718 goto error;738 return NULL; 719 739 complex_args = 1; 720 740 asdl_seq_SET(args, k++, compiler_complex_args(c, ch)); 721 741 if (!asdl_seq_GET(args, k-1)) 722 goto error;742 return NULL; 723 743 } else { 724 744 /* def foo((x)): setup for checking NAME below. */ … … 735 755 expr_ty name; 736 756 if (!forbidden_check(c, n, STR(CHILD(ch, 0)))) 737 goto error;757 return NULL; 738 758 id = NEW_IDENTIFIER(CHILD(ch, 0)); 739 759 if (!id) 740 goto error;760 return NULL; 741 761 name = Name(id, Param, LINENO(ch), ch->n_col_offset, 742 762 c->c_arena); 743 763 if (!name) 744 goto error;764 return NULL; 745 765 asdl_seq_SET(args, k++, name); 746 766 747 767 } 748 768 i += 2; /* the name and the comma */ … … 750 770 !ast_warn(c, ch, "parenthesized argument names " 751 771 "are invalid in 3.x")) 752 goto error;772 return NULL; 753 773 754 774 break; … … 756 776 case STAR: 757 777 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1)))) 758 goto error;778 return NULL; 759 779 vararg = NEW_IDENTIFIER(CHILD(n, i+1)); 760 780 if (!vararg) 761 goto error;781 return NULL; 762 782 i += 3; 763 783 break; 764 784 case DOUBLESTAR: 765 785 if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1)))) 766 goto error;786 return NULL; 767 787 kwarg = NEW_IDENTIFIER(CHILD(n, i+1)); 768 788 if (!kwarg) 769 goto error;789 return NULL; 770 790 i += 3; 771 791 break; … … 774 794 "unexpected node in varargslist: %d @ %d", 775 795 TYPE(ch), i); 776 goto error;796 return NULL; 777 797 } 778 798 } 779 799 780 800 return arguments(args, vararg, kwarg, defaults, c->c_arena); 781 782 error:783 Py_XDECREF(vararg);784 Py_XDECREF(kwarg);785 return NULL;786 801 } 787 802 … … 824 839 expr_ty d = NULL; 825 840 expr_ty name_expr; 826 841 827 842 REQ(n, decorator); 828 843 REQ(CHILD(n, 0), AT); 829 844 REQ(RCHILD(n, -1), NEWLINE); 830 845 831 846 name_expr = ast_for_dotted_name(c, CHILD(n, 1)); 832 847 if (!name_expr) 833 848 return NULL; 834 849 835 850 if (NCH(n) == 3) { /* No arguments */ 836 851 d = name_expr; … … 860 875 expr_ty d; 861 876 int i; 862 877 863 878 REQ(n, decorators); 864 879 decorator_seq = asdl_seq_new(NCH(n), c->c_arena); 865 880 if (!decorator_seq) 866 881 return NULL; 867 882 868 883 for (i = 0; i < NCH(n); i++) { 869 884 d = ast_for_decorator(c, CHILD(n, i)); 870 871 872 885 if (!d) 886 return NULL; 887 asdl_seq_SET(decorator_seq, i, d); 873 888 } 874 889 return decorator_seq; … … 916 931 917 932 assert(TYPE(CHILD(n, 1)) == funcdef || 918 933 TYPE(CHILD(n, 1)) == classdef); 919 934 920 935 if (TYPE(CHILD(n, 1)) == funcdef) { … … 962 977 ast_for_ifexpr(struct compiling *c, const node *n) 963 978 { 964 /* test: or_test 'if' or_test 'else' test */ 979 /* test: or_test 'if' or_test 'else' test */ 965 980 expr_ty expression, body, orelse; 966 981 … … 1053 1068 testlist_safe: test [(',' test)+ [',']] 1054 1069 */ 1055 expr_ty elt ;1070 expr_ty elt, first; 1056 1071 asdl_seq *listcomps; 1057 1072 int i, n_fors; … … 1079 1094 expr_ty expression; 1080 1095 node *for_ch; 1081 1096 1082 1097 REQ(ch, list_for); 1083 1098 1084 1099 for_ch = CHILD(ch, 1); 1085 1100 t = ast_for_exprlist(c, for_ch, Store); … … 1089 1104 if (!expression) 1090 1105 return NULL; 1091 1106 1092 1107 /* Check the # of children rather than the length of t, since 1093 1108 [x for x, in ... ] has 1 element in t, but still requires a Tuple. 1094 1109 */ 1110 first = (expr_ty)asdl_seq_GET(t, 0); 1095 1111 if (NCH(for_ch) == 1) 1096 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL, 1097 c->c_arena); 1112 lc = comprehension(first, expression, NULL, c->c_arena); 1098 1113 else 1099 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,1114 lc = comprehension(Tuple(t, Store, first->lineno, first->col_offset, 1100 1115 c->c_arena), 1101 1116 expression, NULL, c->c_arena); … … 1121 1136 ch = CHILD(ch, 0); 1122 1137 REQ(ch, list_if); 1123 1138 1124 1139 list_for_expr = ast_for_expr(c, CHILD(ch, 1)); 1125 1140 if (!list_for_expr) 1126 1141 return NULL; 1127 1142 1128 1143 asdl_seq_SET(ifs, j, list_for_expr); 1129 1144 if (NCH(ch) == 3) … … 1141 1156 } 1142 1157 1143 /* Count the number of 'for' loops in a generator expression. 1144 1145 Helper for ast_for_genexp(). 1158 /* 1159 Count the number of 'for' loops in a comprehension. 1160 1161 Helper for ast_for_comprehension(). 1146 1162 */ 1147 1163 1148 1164 static int 1149 count_ gen_fors(struct compiling *c, const node *n)1165 count_comp_fors(struct compiling *c, const node *n) 1150 1166 { 1151 1167 int n_fors = 0; 1152 node *ch = CHILD(n, 1); 1153 1154 count_gen_for: 1168 1169 count_comp_for: 1155 1170 n_fors++; 1156 REQ( ch, gen_for);1157 if (NCH( ch) == 5)1158 ch = CHILD(ch, 4);1171 REQ(n, comp_for); 1172 if (NCH(n) == 5) 1173 n = CHILD(n, 4); 1159 1174 else 1160 1175 return n_fors; 1161 count_gen_iter:1162 REQ( ch, gen_iter);1163 ch = CHILD(ch, 0);1164 if (TYPE( ch) == gen_for)1165 goto count_ gen_for;1166 else if (TYPE( ch) == gen_if) {1167 if (NCH( ch) == 3) {1168 ch = CHILD(ch, 2);1169 goto count_ gen_iter;1176 count_comp_iter: 1177 REQ(n, comp_iter); 1178 n = CHILD(n, 0); 1179 if (TYPE(n) == comp_for) 1180 goto count_comp_for; 1181 else if (TYPE(n) == comp_if) { 1182 if (NCH(n) == 3) { 1183 n = CHILD(n, 2); 1184 goto count_comp_iter; 1170 1185 } 1171 1186 else 1172 1187 return n_fors; 1173 1188 } 1174 1189 1175 1190 /* Should never be reached */ 1176 1191 PyErr_SetString(PyExc_SystemError, 1177 "logic error in count_ gen_fors");1192 "logic error in count_comp_fors"); 1178 1193 return -1; 1179 1194 } 1180 1195 1181 /* Count the number of 'if' statements in a generator expression.1182 1183 Helper for ast_for_ genexp().1196 /* Count the number of 'if' statements in a comprehension. 1197 1198 Helper for ast_for_comprehension(). 1184 1199 */ 1185 1200 1186 1201 static int 1187 count_ gen_ifs(struct compiling *c, const node *n)1202 count_comp_ifs(struct compiling *c, const node *n) 1188 1203 { 1189 1204 int n_ifs = 0; 1190 1205 1191 1206 while (1) { 1192 REQ(n, gen_iter);1193 if (TYPE(CHILD(n, 0)) == gen_for)1207 REQ(n, comp_iter); 1208 if (TYPE(CHILD(n, 0)) == comp_for) 1194 1209 return n_ifs; 1195 1210 n = CHILD(n, 0); 1196 REQ(n, gen_if);1211 REQ(n, comp_if); 1197 1212 n_ifs++; 1198 1213 if (NCH(n) == 2) … … 1202 1217 } 1203 1218 1204 /* TODO(jhylton): Combine with list comprehension code? */ 1219 static asdl_seq * 1220 ast_for_comprehension(struct compiling *c, const node *n) 1221 { 1222 int i, n_fors; 1223 asdl_seq *comps; 1224 1225 n_fors = count_comp_fors(c, n); 1226 if (n_fors == -1) 1227 return NULL; 1228 1229 comps = asdl_seq_new(n_fors, c->c_arena); 1230 if (!comps) 1231 return NULL; 1232 1233 for (i = 0; i < n_fors; i++) { 1234 comprehension_ty comp; 1235 asdl_seq *t; 1236 expr_ty expression, first; 1237 node *for_ch; 1238 1239 REQ(n, comp_for); 1240 1241 for_ch = CHILD(n, 1); 1242 t = ast_for_exprlist(c, for_ch, Store); 1243 if (!t) 1244 return NULL; 1245 expression = ast_for_expr(c, CHILD(n, 3)); 1246 if (!expression) 1247 return NULL; 1248 1249 /* Check the # of children rather than the length of t, since 1250 (x for x, in ...) has 1 element in t, but still requires a Tuple. */ 1251 first = (expr_ty)asdl_seq_GET(t, 0); 1252 if (NCH(for_ch) == 1) 1253 comp = comprehension(first, expression, NULL, c->c_arena); 1254 else 1255 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset, 1256 c->c_arena), 1257 expression, NULL, c->c_arena); 1258 if (!comp) 1259 return NULL; 1260 1261 if (NCH(n) == 5) { 1262 int j, n_ifs; 1263 asdl_seq *ifs; 1264 1265 n = CHILD(n, 4); 1266 n_ifs = count_comp_ifs(c, n); 1267 if (n_ifs == -1) 1268 return NULL; 1269 1270 ifs = asdl_seq_new(n_ifs, c->c_arena); 1271 if (!ifs) 1272 return NULL; 1273 1274 for (j = 0; j < n_ifs; j++) { 1275 REQ(n, comp_iter); 1276 n = CHILD(n, 0); 1277 REQ(n, comp_if); 1278 1279 expression = ast_for_expr(c, CHILD(n, 1)); 1280 if (!expression) 1281 return NULL; 1282 asdl_seq_SET(ifs, j, expression); 1283 if (NCH(n) == 3) 1284 n = CHILD(n, 2); 1285 } 1286 /* on exit, must guarantee that n is a comp_for */ 1287 if (TYPE(n) == comp_iter) 1288 n = CHILD(n, 0); 1289 comp->ifs = ifs; 1290 } 1291 asdl_seq_SET(comps, i, comp); 1292 } 1293 return comps; 1294 } 1295 1296 static expr_ty 1297 ast_for_itercomp(struct compiling *c, const node *n, int type) 1298 { 1299 expr_ty elt; 1300 asdl_seq *comps; 1301 1302 assert(NCH(n) > 1); 1303 1304 elt = ast_for_expr(c, CHILD(n, 0)); 1305 if (!elt) 1306 return NULL; 1307 1308 comps = ast_for_comprehension(c, CHILD(n, 1)); 1309 if (!comps) 1310 return NULL; 1311 1312 if (type == COMP_GENEXP) 1313 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); 1314 else if (type == COMP_SETCOMP) 1315 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); 1316 else 1317 /* Should never happen */ 1318 return NULL; 1319 } 1320 1321 static expr_ty 1322 ast_for_dictcomp(struct compiling *c, const node *n) 1323 { 1324 expr_ty key, value; 1325 asdl_seq *comps; 1326 1327 assert(NCH(n) > 3); 1328 REQ(CHILD(n, 1), COLON); 1329 1330 key = ast_for_expr(c, CHILD(n, 0)); 1331 if (!key) 1332 return NULL; 1333 1334 value = ast_for_expr(c, CHILD(n, 2)); 1335 if (!value) 1336 return NULL; 1337 1338 comps = ast_for_comprehension(c, CHILD(n, 3)); 1339 if (!comps) 1340 return NULL; 1341 1342 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena); 1343 } 1344 1205 1345 static expr_ty 1206 1346 ast_for_genexp(struct compiling *c, const node *n) 1207 1347 { 1208 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) 1209 argument: [test '='] test [gen_for] # Really [keyword '='] test */ 1210 expr_ty elt; 1211 asdl_seq *genexps; 1212 int i, n_fors; 1213 node *ch; 1214 1215 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument)); 1216 assert(NCH(n) > 1); 1217 1218 elt = ast_for_expr(c, CHILD(n, 0)); 1219 if (!elt) 1220 return NULL; 1221 1222 n_fors = count_gen_fors(c, n); 1223 if (n_fors == -1) 1224 return NULL; 1225 1226 genexps = asdl_seq_new(n_fors, c->c_arena); 1227 if (!genexps) 1228 return NULL; 1229 1230 ch = CHILD(n, 1); 1231 for (i = 0; i < n_fors; i++) { 1232 comprehension_ty ge; 1233 asdl_seq *t; 1234 expr_ty expression; 1235 node *for_ch; 1236 1237 REQ(ch, gen_for); 1238 1239 for_ch = CHILD(ch, 1); 1240 t = ast_for_exprlist(c, for_ch, Store); 1241 if (!t) 1242 return NULL; 1243 expression = ast_for_expr(c, CHILD(ch, 3)); 1244 if (!expression) 1245 return NULL; 1246 1247 /* Check the # of children rather than the length of t, since 1248 (x for x, in ...) has 1 element in t, but still requires a Tuple. */ 1249 if (NCH(for_ch) == 1) 1250 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, 1251 NULL, c->c_arena); 1252 else 1253 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset, 1254 c->c_arena), 1255 expression, NULL, c->c_arena); 1256 1257 if (!ge) 1258 return NULL; 1259 1260 if (NCH(ch) == 5) { 1261 int j, n_ifs; 1262 asdl_seq *ifs; 1263 1264 ch = CHILD(ch, 4); 1265 n_ifs = count_gen_ifs(c, ch); 1266 if (n_ifs == -1) 1267 return NULL; 1268 1269 ifs = asdl_seq_new(n_ifs, c->c_arena); 1270 if (!ifs) 1271 return NULL; 1272 1273 for (j = 0; j < n_ifs; j++) { 1274 REQ(ch, gen_iter); 1275 ch = CHILD(ch, 0); 1276 REQ(ch, gen_if); 1277 1278 expression = ast_for_expr(c, CHILD(ch, 1)); 1279 if (!expression) 1280 return NULL; 1281 asdl_seq_SET(ifs, j, expression); 1282 if (NCH(ch) == 3) 1283 ch = CHILD(ch, 2); 1284 } 1285 /* on exit, must guarantee that ch is a gen_for */ 1286 if (TYPE(ch) == gen_iter) 1287 ch = CHILD(ch, 0); 1288 ge->ifs = ifs; 1289 } 1290 asdl_seq_SET(genexps, i, ge); 1291 } 1292 1293 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena); 1348 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument)); 1349 return ast_for_itercomp(c, n, COMP_GENEXP); 1350 } 1351 1352 static expr_ty 1353 ast_for_setcomp(struct compiling *c, const node *n) 1354 { 1355 assert(TYPE(n) == (dictorsetmaker)); 1356 return ast_for_itercomp(c, n, COMP_SETCOMP); 1294 1357 } 1295 1358 … … 1297 1360 ast_for_atom(struct compiling *c, const node *n) 1298 1361 { 1299 /* atom: '(' [yield_expr|testlist_ gexp] ')' | '[' [listmaker] ']'1362 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [listmaker] ']' 1300 1363 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+ 1301 1364 */ 1302 1365 node *ch = CHILD(n, 0); 1303 1366 1304 1367 switch (TYPE(ch)) { 1305 1368 case NAME: { … … 1349 1412 case LPAR: /* some parenthesized expressions */ 1350 1413 ch = CHILD(n, 1); 1351 1414 1352 1415 if (TYPE(ch) == RPAR) 1353 1416 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena); 1354 1417 1355 1418 if (TYPE(ch) == yield_expr) 1356 1419 return ast_for_expr(c, ch); 1357 1358 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for)) 1359 return ast_for_genexp(c, ch); 1360 1361 return ast_for_testlist_gexp(c, ch); 1420 1421 return ast_for_testlist_comp(c, ch); 1362 1422 case LSQB: /* list (or list comprehension) */ 1363 1423 ch = CHILD(n, 1); 1364 1424 1365 1425 if (TYPE(ch) == RSQB) 1366 1426 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena); 1367 1427 1368 1428 REQ(ch, listmaker); 1369 1429 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { … … 1377 1437 return ast_for_listcomp(c, ch); 1378 1438 case LBRACE: { 1379 /* dictmaker: test ':' test (',' test ':' test)* [','] */ 1439 /* dictorsetmaker: 1440 * (test ':' test (comp_for | (',' test ':' test)* [','])) | 1441 * (test (comp_for | (',' test)* [','])) 1442 */ 1380 1443 int i, size; 1381 1444 asdl_seq *keys, *values; 1382 1445 1383 1446 ch = CHILD(n, 1); 1384 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */ 1385 keys = asdl_seq_new(size, c->c_arena); 1386 if (!keys) 1387 return NULL; 1388 1389 values = asdl_seq_new(size, c->c_arena); 1390 if (!values) 1391 return NULL; 1392 1393 for (i = 0; i < NCH(ch); i += 4) { 1394 expr_ty expression; 1395 1396 expression = ast_for_expr(c, CHILD(ch, i)); 1397 if (!expression) 1398 return NULL; 1399 1400 asdl_seq_SET(keys, i / 4, expression); 1401 1402 expression = ast_for_expr(c, CHILD(ch, i + 2)); 1403 if (!expression) 1404 return NULL; 1405 1406 asdl_seq_SET(values, i / 4, expression); 1407 } 1408 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena); 1447 if (TYPE(ch) == RBRACE) { 1448 /* it's an empty dict */ 1449 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena); 1450 } else if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { 1451 /* it's a simple set */ 1452 asdl_seq *elts; 1453 size = (NCH(ch) + 1) / 2; /* +1 in case no trailing comma */ 1454 elts = asdl_seq_new(size, c->c_arena); 1455 if (!elts) 1456 return NULL; 1457 for (i = 0; i < NCH(ch); i += 2) { 1458 expr_ty expression; 1459 expression = ast_for_expr(c, CHILD(ch, i)); 1460 if (!expression) 1461 return NULL; 1462 asdl_seq_SET(elts, i / 2, expression); 1463 } 1464 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena); 1465 } else if (TYPE(CHILD(ch, 1)) == comp_for) { 1466 /* it's a set comprehension */ 1467 return ast_for_setcomp(c, ch); 1468 } else if (NCH(ch) > 3 && TYPE(CHILD(ch, 3)) == comp_for) { 1469 return ast_for_dictcomp(c, ch); 1470 } else { 1471 /* it's a dict */ 1472 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */ 1473 keys = asdl_seq_new(size, c->c_arena); 1474 if (!keys) 1475 return NULL; 1476 1477 values = asdl_seq_new(size, c->c_arena); 1478 if (!values) 1479 return NULL; 1480 1481 for (i = 0; i < NCH(ch); i += 4) { 1482 expr_ty expression; 1483 1484 expression = ast_for_expr(c, CHILD(ch, i)); 1485 if (!expression) 1486 return NULL; 1487 1488 asdl_seq_SET(keys, i / 4, expression); 1489 1490 expression = ast_for_expr(c, CHILD(ch, i + 2)); 1491 if (!expression) 1492 return NULL; 1493 1494 asdl_seq_SET(values, i / 4, expression); 1495 } 1496 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena); 1497 } 1409 1498 } 1410 1499 case BACKQUOTE: { /* repr */ … … 1444 1533 /* 'step' variable hold no significance in terms of being used over 1445 1534 other vars */ 1446 step = ast_for_expr(c, ch); 1535 step = ast_for_expr(c, ch); 1447 1536 if (!step) 1448 1537 return NULL; 1449 1538 1450 1539 return Index(step, c->c_arena); 1451 1540 } … … 1481 1570 if (TYPE(ch) == sliceop) { 1482 1571 if (NCH(ch) == 1) { 1483 /* No expression, so step is None */ 1572 /* 1573 This is an extended slice (ie "x[::]") with no expression in the 1574 step field. We set this literally to "None" in order to 1575 disambiguate it from x[:]. (The interpreter might have to call 1576 __getslice__ for x[:], but it must call __getitem__ for x[::].) 1577 */ 1578 identifier none = new_identifier("None", c->c_arena); 1579 if (!none) 1580 return NULL; 1484 1581 ch = CHILD(ch, 0); 1485 step = Name(new_identifier("None", c->c_arena), Load, 1486 LINENO(ch), ch->n_col_offset, c->c_arena); 1582 step = Name(none, Load, LINENO(ch), ch->n_col_offset, c->c_arena); 1487 1583 if (!step) 1488 1584 return NULL; … … 1504 1600 { 1505 1601 /* Must account for a sequence of expressions. 1506 How should A op B op C by represented? 1602 How should A op B op C by represented? 1507 1603 BinOp(BinOp(A, op, B), op, C). 1508 1604 */ … … 1542 1638 return NULL; 1543 1639 1544 tmp_result = BinOp(result, newoperator, tmp, 1640 tmp_result = BinOp(result, newoperator, tmp, 1545 1641 LINENO(next_oper), next_oper->n_col_offset, 1546 1642 c->c_arena); 1547 if (!tmp_result) 1643 if (!tmp_result) 1548 1644 return NULL; 1549 1645 result = tmp_result; … … 1555 1651 ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr) 1556 1652 { 1557 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME 1653 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME 1558 1654 subscriptlist: subscript (',' subscript)* [','] 1559 1655 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] … … 1586 1682 } 1587 1683 else { 1588 /* The grammar is ambiguous here. The ambiguity is resolved 1684 /* The grammar is ambiguous here. The ambiguity is resolved 1589 1685 by treating the sequence as a tuple literal if there are 1590 1686 no slice features. … … 1649 1745 TYPE((patom = CHILD(ppower, 0))) == atom && 1650 1746 TYPE((pnum = CHILD(patom, 0))) == NUMBER) { 1747 PyObject *pynum; 1651 1748 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2); 1652 1749 if (s == NULL) … … 1654 1751 s[0] = '-'; 1655 1752 strcpy(s + 1, STR(pnum)); 1656 PyObject_FREE(STR(pnum)); 1657 STR(pnum) = s; 1658 return ast_for_atom(c, patom); 1753 pynum = parsenumber(c, s); 1754 PyObject_FREE(s); 1755 if (!pynum) 1756 return NULL; 1757 1758 PyArena_AddPyObject(c->c_arena, pynum); 1759 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena); 1659 1760 } 1660 1761 … … 1723 1824 /* handle the full range of simple expressions 1724 1825 test: or_test ['if' or_test 'else' test] | lambdef 1725 or_test: and_test ('or' and_test)* 1826 or_test: and_test ('or' and_test)* 1726 1827 and_test: not_test ('and' not_test)* 1727 1828 not_test: 'not' not_test | comparison … … 1740 1841 [ x for x in lambda: 0, lambda: 1 ] 1741 1842 (which would be ambiguous without these extra rules) 1742 1843 1743 1844 old_test: or_test | old_lambdef 1744 1845 old_lambdef: 'lambda' [vararglist] ':' old_test … … 1820 1921 return NULL; 1821 1922 } 1822 1923 1823 1924 asdl_seq_SET(ops, i / 2, newoperator); 1824 1925 asdl_seq_SET(cmps, i / 2, expression); … … 1828 1929 return NULL; 1829 1930 } 1830 1931 1831 1932 return Compare(expression, ops, cmps, LINENO(n), 1832 1933 n->n_col_offset, c->c_arena); … … 1880 1981 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] 1881 1982 | '**' test) 1882 argument: [test '='] test [ gen_for] # Really [keyword '='] test1983 argument: [test '='] test [comp_for] # Really [keyword '='] test 1883 1984 */ 1884 1985 … … 1898 1999 if (NCH(ch) == 1) 1899 2000 nargs++; 1900 else if (TYPE(CHILD(ch, 1)) == gen_for)2001 else if (TYPE(CHILD(ch, 1)) == comp_for) 1901 2002 ngens++; 1902 2003 else … … 1942 2043 return NULL; 1943 2044 asdl_seq_SET(args, nargs++, e); 1944 } 1945 else if (TYPE(CHILD(ch, 1)) == gen_for) {2045 } 2046 else if (TYPE(CHILD(ch, 1)) == comp_for) { 1946 2047 e = ast_for_genexp(c, ch); 1947 2048 if (!e) … … 1955 2056 char *tmp; 1956 2057 1957 /* CHILD(ch, 0) is test, but must be an identifier? */ 2058 /* CHILD(ch, 0) is test, but must be an identifier? */ 1958 2059 e = ast_for_expr(c, CHILD(ch, 0)); 1959 2060 if (!e) … … 2013 2114 ast_for_testlist(struct compiling *c, const node* n) 2014 2115 { 2015 /* testlist_ gexp: test (',' test)* [','] */2116 /* testlist_comp: test (',' test)* [','] */ 2016 2117 /* testlist: test (',' test)* [','] */ 2017 2118 /* testlist_safe: test (',' test)+ [','] */ 2018 2119 /* testlist1: test (',' test)* */ 2019 2120 assert(NCH(n) > 0); 2020 if (TYPE(n) == testlist_ gexp) {2121 if (TYPE(n) == testlist_comp) { 2021 2122 if (NCH(n) > 1) 2022 assert(TYPE(CHILD(n, 1)) != gen_for);2123 assert(TYPE(CHILD(n, 1)) != comp_for); 2023 2124 } 2024 2125 else { … … 2038 2139 2039 2140 static expr_ty 2040 ast_for_testlist_ gexp(struct compiling *c, const node* n)2041 { 2042 /* testlist_ gexp: test ( gen_for | (',' test)* [','] ) */2043 /* argument: test [ gen_for ] */2044 assert(TYPE(n) == testlist_ gexp || TYPE(n) == argument);2045 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)2141 ast_for_testlist_comp(struct compiling *c, const node* n) 2142 { 2143 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */ 2144 /* argument: test [ comp_for ] */ 2145 assert(TYPE(n) == testlist_comp || TYPE(n) == argument); 2146 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == comp_for) 2046 2147 return ast_for_genexp(c, n); 2047 2148 return ast_for_testlist(c, n); … … 2074 2175 { 2075 2176 REQ(n, expr_stmt); 2076 /* expr_stmt: testlist (augassign (yield_expr|testlist) 2177 /* expr_stmt: testlist (augassign (yield_expr|testlist) 2077 2178 | ('=' (yield_expr|testlist))*) 2078 2179 testlist: test (',' test)* [','] … … 2097 2198 if (!expr1) 2098 2199 return NULL; 2099 /* TODO(nas): Remove duplicated error checks (set_context does it) */ 2200 if(!set_context(c, expr1, Store, ch)) 2201 return NULL; 2202 /* set_context checks that most expressions are not the left side. 2203 Augmented assignments can only have a name, a subscript, or an 2204 attribute on the left, though, so we have to explicitly check for 2205 those. */ 2100 2206 switch (expr1->kind) { 2101 case GeneratorExp_kind: 2102 ast_error(ch, "augmented assignment to generator " 2103 "expression not possible"); 2104 return NULL; 2105 case Yield_kind: 2106 ast_error(ch, "augmented assignment to yield " 2107 "expression not possible"); 2108 return NULL; 2109 case Name_kind: { 2110 const char *var_name = PyBytes_AS_STRING(expr1->v.Name.id); 2111 if ((var_name[0] == 'N' || var_name[0] == 'T' || var_name[0] == 'F') && 2112 !forbidden_check(c, ch, var_name)) 2113 return NULL; 2114 break; 2115 } 2207 case Name_kind: 2116 2208 case Attribute_kind: 2117 2209 case Subscript_kind: 2118 2210 break; 2119 2211 default: 2120 ast_error(ch, "illegal expression for augmented " 2121 "assignment"); 2122 return NULL; 2123 } 2124 if(!set_context(c, expr1, Store, ch)) 2125 return NULL; 2212 ast_error(ch, "illegal expression for augmented assignment"); 2213 return NULL; 2214 } 2126 2215 2127 2216 ch = CHILD(n, 2); … … 2159 2248 } 2160 2249 e = ast_for_testlist(c, ch); 2250 if (!e) 2251 return NULL; 2161 2252 2162 2253 /* set context to assign */ 2163 if (!e)2164 return NULL;2165 2166 2254 if (!set_context(c, e, Store, CHILD(n, i))) 2167 2255 return NULL; … … 2188 2276 */ 2189 2277 expr_ty dest = NULL, expression; 2190 asdl_seq *seq ;2278 asdl_seq *seq = NULL; 2191 2279 bool nl; 2192 int i, j, start = 1;2280 int i, j, values_count, start = 1; 2193 2281 2194 2282 REQ(n, print_stmt); … … 2199 2287 start = 4; 2200 2288 } 2201 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena); 2202 if (!seq) 2203 return NULL; 2204 for (i = start, j = 0; i < NCH(n); i += 2, ++j) { 2205 expression = ast_for_expr(c, CHILD(n, i)); 2206 if (!expression) 2207 return NULL; 2208 asdl_seq_SET(seq, j, expression); 2289 values_count = (NCH(n) + 1 - start) / 2; 2290 if (values_count) { 2291 seq = asdl_seq_new(values_count, c->c_arena); 2292 if (!seq) 2293 return NULL; 2294 for (i = start, j = 0; i < NCH(n); i += 2, ++j) { 2295 expression = ast_for_expr(c, CHILD(n, i)); 2296 if (!expression) 2297 return NULL; 2298 asdl_seq_SET(seq, j, expression); 2299 } 2209 2300 } 2210 2301 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true; … … 2239 2330 { 2240 2331 asdl_seq *expr_list; 2241 2332 2242 2333 /* del_stmt: 'del' exprlist */ 2243 2334 REQ(n, del_stmt); … … 2323 2414 if (!expr3) 2324 2415 return NULL; 2325 2416 2326 2417 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, 2327 2418 c->c_arena); … … 2338 2429 2339 2430 static alias_ty 2340 alias_for_import_name(struct compiling *c, const node *n )2431 alias_for_import_name(struct compiling *c, const node *n, int store) 2341 2432 { 2342 2433 /* … … 2349 2440 loop: 2350 2441 switch (TYPE(n)) { 2351 case import_as_name: 2442 case import_as_name: { 2443 node *name_node = CHILD(n, 0); 2352 2444 str = NULL; 2353 2445 if (NCH(n) == 3) { 2354 str = NEW_IDENTIFIER(CHILD(n, 2)); 2446 node *str_node = CHILD(n, 2); 2447 if (store && !forbidden_check(c, str_node, STR(str_node))) 2448 return NULL; 2449 str = NEW_IDENTIFIER(str_node); 2355 2450 if (!str) 2356 2451 return NULL; 2357 2452 } 2358 name = NEW_IDENTIFIER(CHILD(n, 0)); 2453 else { 2454 if (!forbidden_check(c, name_node, STR(name_node))) 2455 return NULL; 2456 } 2457 name = NEW_IDENTIFIER(name_node); 2359 2458 if (!name) 2360 2459 return NULL; 2361 2460 return alias(name, str, c->c_arena); 2461 } 2362 2462 case dotted_as_name: 2363 2463 if (NCH(n) == 1) { … … 2366 2466 } 2367 2467 else { 2368 alias_ty a = alias_for_import_name(c, CHILD(n, 0)); 2468 node *asname_node = CHILD(n, 2); 2469 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0); 2369 2470 if (!a) 2370 2471 return NULL; 2371 2472 assert(!a->asname); 2372 a->asname = NEW_IDENTIFIER(CHILD(n, 2)); 2473 if (!forbidden_check(c, asname_node, STR(asname_node))) 2474 return NULL; 2475 a->asname = NEW_IDENTIFIER(asname_node); 2373 2476 if (!a->asname) 2374 2477 return NULL; … … 2378 2481 case dotted_name: 2379 2482 if (NCH(n) == 1) { 2380 name = NEW_IDENTIFIER(CHILD(n, 0)); 2483 node *name_node = CHILD(n, 0); 2484 if (store && !forbidden_check(c, name_node, STR(name_node))) 2485 return NULL; 2486 name = NEW_IDENTIFIER(name_node); 2381 2487 if (!name) 2382 2488 return NULL; … … 2452 2558 return NULL; 2453 2559 for (i = 0; i < NCH(n); i += 2) { 2454 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i) );2560 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1); 2455 2561 if (!import_alias) 2456 2562 return NULL; … … 2463 2569 int idx, ndots = 0; 2464 2570 alias_ty mod = NULL; 2465 identifier modname ;2466 2571 identifier modname = NULL; 2572 2467 2573 /* Count the number of dots (for relative imports) and check for the 2468 2574 optional module name */ 2469 2575 for (idx = 1; idx < NCH(n); idx++) { 2470 2576 if (TYPE(CHILD(n, idx)) == dotted_name) { 2471 mod = alias_for_import_name(c, CHILD(n, idx)); 2577 mod = alias_for_import_name(c, CHILD(n, idx), 0); 2578 if (!mod) 2579 return NULL; 2472 2580 idx++; 2473 2581 break; … … 2510 2618 /* handle "from ... import *" special b/c there's no children */ 2511 2619 if (TYPE(n) == STAR) { 2512 alias_ty import_alias = alias_for_import_name(c, n );2620 alias_ty import_alias = alias_for_import_name(c, n, 1); 2513 2621 if (!import_alias) 2514 2622 return NULL; … … 2517 2625 else { 2518 2626 for (i = 0; i < NCH(n); i += 2) { 2519 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i) );2627 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1); 2520 2628 if (!import_alias) 2521 2629 return NULL; … … 2525 2633 if (mod != NULL) 2526 2634 modname = mod->name; 2527 else2528 modname = new_identifier("", c->c_arena);2529 2635 return ImportFrom(modname, aliases, ndots, lineno, col_offset, 2530 2636 c->c_arena); … … 2610 2716 if (!expr2) 2611 2717 return NULL; 2612 2718 2613 2719 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena); 2614 2720 } … … 2637 2743 n = CHILD(n, 0); 2638 2744 /* simple_stmt always ends with a NEWLINE, 2639 and may have a trailing SEMI 2745 and may have a trailing SEMI 2640 2746 */ 2641 2747 end = NCH(n) - 1; … … 2702 2808 if (!expression) 2703 2809 return NULL; 2704 suite_seq = ast_for_suite(c, CHILD(n, 3)); 2810 suite_seq = ast_for_suite(c, CHILD(n, 3)); 2705 2811 if (!suite_seq) 2706 2812 return NULL; 2707 2813 2708 2814 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, 2709 2815 c->c_arena); … … 2763 2869 return NULL; 2764 2870 2765 asdl_seq_SET(orelse, 0, 2766 If(expression, suite_seq, suite_seq2, 2871 asdl_seq_SET(orelse, 0, 2872 If(expression, suite_seq, suite_seq2, 2767 2873 LINENO(CHILD(n, NCH(n) - 6)), 2768 2874 CHILD(n, NCH(n) - 6)->n_col_offset, … … 2785 2891 2786 2892 asdl_seq_SET(newobj, 0, 2787 If(expression, suite_seq, orelse, 2893 If(expression, suite_seq, orelse, 2788 2894 LINENO(CHILD(n, off)), 2789 2895 CHILD(n, off)->n_col_offset, c->c_arena)); … … 2853 2959 asdl_seq *_target, *seq = NULL, *suite_seq; 2854 2960 expr_ty expression; 2855 expr_ty target ;2961 expr_ty target, first; 2856 2962 const node *node_target; 2857 2963 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */ … … 2870 2976 /* Check the # of children rather than the length of _target, since 2871 2977 for x, in ... has 1 element in _target, but still requires a Tuple. */ 2978 first = (expr_ty)asdl_seq_GET(_target, 0); 2872 2979 if (NCH(node_target) == 1) 2873 target = (expr_ty)asdl_seq_GET(_target, 0);2980 target = first; 2874 2981 else 2875 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);2982 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena); 2876 2983 2877 2984 expression = ast_for_testlist(c, CHILD(n, 3)); … … 2983 3090 return NULL; 2984 3091 } 2985 3092 2986 3093 if (n_except > 0) { 2987 3094 int i; … … 3018 3125 } 3019 3126 3020 static expr_ty 3021 ast_for_with_var(struct compiling *c, const node *n) 3022 { 3023 REQ(n, with_var); 3024 return ast_for_expr(c, CHILD(n, 1)); 3025 } 3026 3027 /* with_stmt: 'with' test [ with_var ] ':' suite */ 3127 /* with_item: test ['as' expr] */ 3128 static stmt_ty 3129 ast_for_with_item(struct compiling *c, const node *n, asdl_seq *content) 3130 { 3131 expr_ty context_expr, optional_vars = NULL; 3132 3133 REQ(n, with_item); 3134 context_expr = ast_for_expr(c, CHILD(n, 0)); 3135 if (!context_expr) 3136 return NULL; 3137 if (NCH(n) == 3) { 3138 optional_vars = ast_for_expr(c, CHILD(n, 2)); 3139 3140 if (!optional_vars) { 3141 return NULL; 3142 } 3143 if (!set_context(c, optional_vars, Store, n)) { 3144 return NULL; 3145 } 3146 } 3147 3148 return With(context_expr, optional_vars, content, LINENO(n), 3149 n->n_col_offset, c->c_arena); 3150 } 3151 3152 /* with_stmt: 'with' with_item (',' with_item)* ':' suite */ 3028 3153 static stmt_ty 3029 3154 ast_for_with_stmt(struct compiling *c, const node *n) 3030 3155 { 3031 expr_ty context_expr, optional_vars = NULL; 3032 int suite_index = 3; /* skip 'with', test, and ':' */ 3033 asdl_seq *suite_seq; 3034 3035 assert(TYPE(n) == with_stmt); 3036 context_expr = ast_for_expr(c, CHILD(n, 1)); 3037 if (!context_expr) 3038 return NULL; 3039 if (TYPE(CHILD(n, 2)) == with_var) { 3040 optional_vars = ast_for_with_var(c, CHILD(n, 2)); 3041 3042 if (!optional_vars) { 3043 return NULL; 3044 } 3045 if (!set_context(c, optional_vars, Store, n)) { 3046 return NULL; 3047 } 3048 suite_index = 4; 3049 } 3050 3051 suite_seq = ast_for_suite(c, CHILD(n, suite_index)); 3052 if (!suite_seq) { 3053 return NULL; 3054 } 3055 return With(context_expr, optional_vars, suite_seq, LINENO(n), 3056 n->n_col_offset, c->c_arena); 3156 int i; 3157 stmt_ty ret; 3158 asdl_seq *inner; 3159 3160 REQ(n, with_stmt); 3161 3162 /* process the with items inside-out */ 3163 i = NCH(n) - 1; 3164 /* the suite of the innermost with item is the suite of the with stmt */ 3165 inner = ast_for_suite(c, CHILD(n, i)); 3166 if (!inner) 3167 return NULL; 3168 3169 for (;;) { 3170 i -= 2; 3171 ret = ast_for_with_item(c, CHILD(n, i), inner); 3172 if (!ret) 3173 return NULL; 3174 /* was this the last item? */ 3175 if (i == 1) 3176 break; 3177 /* if not, wrap the result so far in a new sequence */ 3178 inner = asdl_seq_new(1, c->c_arena); 3179 if (!inner) 3180 return NULL; 3181 asdl_seq_SET(inner, 0, ret); 3182 } 3183 3184 return ret; 3057 3185 } 3058 3186 … … 3063 3191 PyObject *classname; 3064 3192 asdl_seq *bases, *s; 3065 3193 3066 3194 REQ(n, classdef); 3067 3195 … … 3118 3246 } 3119 3247 if (TYPE(n) == small_stmt) { 3120 REQ(n, small_stmt);3121 3248 n = CHILD(n, 0); 3122 3249 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt … … 3171 3298 case classdef: 3172 3299 return ast_for_classdef(c, ch, NULL); 3173 3174 3300 case decorated: 3301 return ast_for_decorated(c, ch); 3175 3302 default: 3176 3303 PyErr_Format(PyExc_SystemError, … … 3211 3338 if (imflag) { 3212 3339 complex.real = 0.; 3213 PyFPE_START_PROTECT("atof", return 0)3214 complex.imag = PyOS_ascii_atof(s);3215 PyFPE_END_PROTECT(complex)3340 complex.imag = PyOS_string_to_double(s, (char **)&end, NULL); 3341 if (complex.imag == -1.0 && PyErr_Occurred()) 3342 return NULL; 3216 3343 return PyComplex_FromCComplex(complex); 3217 3344 } … … 3219 3346 #endif 3220 3347 { 3221 PyFPE_START_PROTECT("atof", return 0)3222 dx = PyOS_ascii_atof(s);3223 PyFPE_END_PROTECT(dx)3348 dx = PyOS_string_to_double(s, NULL, NULL); 3349 if (dx == -1.0 && PyErr_Occurred()) 3350 return NULL; 3224 3351 return PyFloat_FromDouble(dx); 3225 3352 } … … 3252 3379 decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding) 3253 3380 { 3254 PyObject *v, *u; 3381 PyObject *v; 3382 PyObject *u = NULL; 3255 3383 char *buf; 3256 3384 char *p; 3257 3385 const char *end; 3258 if (encoding == NULL) { 3259 buf = (char *)s; 3260 u = NULL; 3261 } else if (strcmp(encoding, "iso-8859-1") == 0) { 3262 buf = (char *)s; 3263 u = NULL; 3264 } else { 3386 if (encoding != NULL && strcmp(encoding, "iso-8859-1")) { 3265 3387 /* check for integer overflow */ 3266 3388 if (len > PY_SIZE_MAX / 6) 3267 3389 return NULL; 3268 3269 3390 /* "<C3><A4>" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5 3391 "\À" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */ 3270 3392 u = PyString_FromStringAndSize((char *)NULL, len * 6); 3271 3393 if (u == NULL) … … 3297 3419 r[i + 0] & 0xFF, 3298 3420 r[i + 1] & 0xFF, 3299 3300 3421 r[i + 2] & 0xFF, 3422 r[i + 3] & 0xFF); 3301 3423 p += 10; 3302 3424 } … … 3352 3474 len = strlen(s); 3353 3475 if (len > INT_MAX) { 3354 PyErr_SetString(PyExc_OverflowError, 3476 PyErr_SetString(PyExc_OverflowError, 3355 3477 "string to parse is too long"); 3356 3478 return NULL;
Note:
See TracChangeset
for help on using the changeset viewer.