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

Side by Side Diff: Python/symtable.c

Issue 10639: reindent.py converts newlines to platform default
Patch Set: Created 8 years, 8 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:
View unified diff | Download patch
« no previous file with comments | « Python/pythonrun.c ('k') | Python/thread.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #include "Python.h" 1 #include "Python.h"
2 #include "Python-ast.h" 2 #include "Python-ast.h"
3 #include "code.h" 3 #include "code.h"
4 #include "symtable.h" 4 #include "symtable.h"
5 #include "structmember.h" 5 #include "structmember.h"
6 6
7 /* error strings used for warnings */ 7 /* error strings used for warnings */
8 #define GLOBAL_AFTER_ASSIGN \ 8 #define GLOBAL_AFTER_ASSIGN \
9 "name '%.400s' is assigned to before global declaration" 9 "name '%.400s' is assigned to before global declaration"
10 10
(...skipping 881 matching lines...) Expand 10 before | Expand all | Expand 10 after
892 } 892 }
893 893
894 /* symtable_enter_block() gets a reference via ste_new. 894 /* symtable_enter_block() gets a reference via ste_new.
895 This reference is released when the block is exited, via the DECREF 895 This reference is released when the block is exited, via the DECREF
896 in symtable_exit_block(). 896 in symtable_exit_block().
897 */ 897 */
898 898
899 static int 899 static int
900 symtable_exit_block(struct symtable *st, void *ast) 900 symtable_exit_block(struct symtable *st, void *ast)
901 { 901 {
902 Py_ssize_t size; 902 Py_ssize_t end;
903 903
904 st->st_cur = NULL; 904 Py_CLEAR(st->st_cur);
905 size = PyList_GET_SIZE(st->st_stack); 905 end = PyList_GET_SIZE(st->st_stack) - 1;
906 if (size) { 906 if (end >= 0) {
907 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0) 907 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
908 end);
909 if (st->st_cur == NULL)
908 return 0; 910 return 0;
909 if (--size) 911 Py_INCREF(st->st_cur);
910 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1); 912 if (PySequence_DelItem(st->st_stack, end) < 0)
913 return 0;
911 } 914 }
912 return 1; 915 return 1;
913 } 916 }
914 917
915 static int 918 static int
916 symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block, 919 symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
917 void *ast, int lineno, int col_offset) 920 void *ast, int lineno, int col_offset)
918 { 921 {
919 PySTEntryObject *prev = NULL, *ste; 922 PySTEntryObject *prev = NULL;
920 923
921 ste = ste_new(st, name, block, ast, lineno, col_offset); 924 if (st->st_cur) {
922 if (ste == NULL) 925 prev = st->st_cur;
926 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
927 return 0;
928 }
929 Py_DECREF(st->st_cur);
930 }
931 st->st_cur = ste_new(st, name, block, ast, lineno, col_offset);
932 if (st->st_cur == NULL)
923 return 0; 933 return 0;
924 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
925 Py_DECREF(ste);
926 return 0;
927 }
928 prev = st->st_cur;
929 /* The entry is owned by the stack. Borrow it for st_cur. */
930 Py_DECREF(ste);
931 st->st_cur = ste;
932 if (block == ModuleBlock) 934 if (block == ModuleBlock)
933 st->st_global = st->st_cur->ste_symbols; 935 st->st_global = st->st_cur->ste_symbols;
934 if (prev) { 936 if (prev) {
935 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) { 937 if (PyList_Append(prev->ste_children,
938 (PyObject *)st->st_cur) < 0) {
936 return 0; 939 return 0;
937 } 940 }
938 } 941 }
939 return 1; 942 return 1;
940 } 943 }
941 944
942 static long 945 static long
943 symtable_lookup(struct symtable *st, PyObject *name) 946 symtable_lookup(struct symtable *st, PyObject *name)
944 { 947 {
945 PyObject *o; 948 PyObject *o;
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
1018 They use the ASDL name to synthesize the name of the C type and the visit 1021 They use the ASDL name to synthesize the name of the C type and the visit
1019 function. 1022 function.
1020 1023
1021 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is 1024 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1022 useful if the first node in the sequence requires special treatment. 1025 useful if the first node in the sequence requires special treatment.
1023 */ 1026 */
1024 1027
1025 #define VISIT(ST, TYPE, V) \ 1028 #define VISIT(ST, TYPE, V) \
1026 if (!symtable_visit_ ## TYPE((ST), (V))) \ 1029 if (!symtable_visit_ ## TYPE((ST), (V))) \
1027 return 0; 1030 return 0;
1031
1032 #define VISIT_IN_BLOCK(ST, TYPE, V, S) \
1033 if (!symtable_visit_ ## TYPE((ST), (V))) { \
1034 symtable_exit_block((ST), (S)); \
1035 return 0; \
1036 }
1028 1037
1029 #define VISIT_SEQ(ST, TYPE, SEQ) { \ 1038 #define VISIT_SEQ(ST, TYPE, SEQ) { \
1030 int i; \ 1039 int i; \
1031 asdl_seq *seq = (SEQ); /* avoid variable capture */ \ 1040 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1032 for (i = 0; i < asdl_seq_LEN(seq); i++) { \ 1041 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1033 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \ 1042 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1034 if (!symtable_visit_ ## TYPE((ST), elt)) \ 1043 if (!symtable_visit_ ## TYPE((ST), elt)) \
1035 return 0; \ 1044 return 0; \
1045 } \
1046 }
1047
1048 #define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
1049 int i; \
1050 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1051 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1052 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1053 if (!symtable_visit_ ## TYPE((ST), elt)) { \
1054 symtable_exit_block((ST), (S)); \
1055 return 0; \
1056 } \
1036 } \ 1057 } \
1037 } 1058 }
1038 1059
1039 #define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \ 1060 #define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
1040 int i; \ 1061 int i; \
1041 asdl_seq *seq = (SEQ); /* avoid variable capture */ \ 1062 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1042 for (i = (START); i < asdl_seq_LEN(seq); i++) { \ 1063 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1043 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \ 1064 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1044 if (!symtable_visit_ ## TYPE((ST), elt)) \ 1065 if (!symtable_visit_ ## TYPE((ST), elt)) \
1045 return 0; \ 1066 return 0; \
1067 } \
1068 }
1069
1070 #define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
1071 int i; \
1072 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1073 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1074 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1075 if (!symtable_visit_ ## TYPE((ST), elt)) { \
1076 symtable_exit_block((ST), (S)); \
1077 return 0; \
1078 } \
1046 } \ 1079 } \
1047 } 1080 }
1048 1081
1049 #define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \ 1082 #define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
1050 int i = 0; \ 1083 int i = 0; \
1051 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \ 1084 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
1052 for (i = 0; i < asdl_seq_LEN(seq); i++) { \ 1085 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1053 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \ 1086 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
1054 if (!elt) continue; /* can be NULL */ \ 1087 if (!elt) continue; /* can be NULL */ \
1055 if (!symtable_visit_expr((ST), elt)) \ 1088 if (!symtable_visit_expr((ST), elt)) \
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
1088 VISIT_KWONLYDEFAULTS(st, 1121 VISIT_KWONLYDEFAULTS(st,
1089 s->v.FunctionDef.args->kw_defaults); 1122 s->v.FunctionDef.args->kw_defaults);
1090 if (!symtable_visit_annotations(st, s)) 1123 if (!symtable_visit_annotations(st, s))
1091 return 0; 1124 return 0;
1092 if (s->v.FunctionDef.decorator_list) 1125 if (s->v.FunctionDef.decorator_list)
1093 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list); 1126 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1094 if (!symtable_enter_block(st, s->v.FunctionDef.name, 1127 if (!symtable_enter_block(st, s->v.FunctionDef.name,
1095 FunctionBlock, (void *)s, s->lineno, 1128 FunctionBlock, (void *)s, s->lineno,
1096 s->col_offset)) 1129 s->col_offset))
1097 return 0; 1130 return 0;
1098 VISIT(st, arguments, s->v.FunctionDef.args); 1131 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
1099 VISIT_SEQ(st, stmt, s->v.FunctionDef.body); 1132 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
1100 if (!symtable_exit_block(st, s)) 1133 if (!symtable_exit_block(st, s))
1101 return 0; 1134 return 0;
1102 break; 1135 break;
1103 case ClassDef_kind: { 1136 case ClassDef_kind: {
1104 PyObject *tmp; 1137 PyObject *tmp;
1105 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL)) 1138 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1106 return 0; 1139 return 0;
1107 VISIT_SEQ(st, expr, s->v.ClassDef.bases); 1140 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1108 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords); 1141 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1109 if (s->v.ClassDef.starargs) 1142 if (s->v.ClassDef.starargs)
1110 VISIT(st, expr, s->v.ClassDef.starargs); 1143 VISIT(st, expr, s->v.ClassDef.starargs);
1111 if (s->v.ClassDef.kwargs) 1144 if (s->v.ClassDef.kwargs)
1112 VISIT(st, expr, s->v.ClassDef.kwargs); 1145 VISIT(st, expr, s->v.ClassDef.kwargs);
1113 if (s->v.ClassDef.decorator_list) 1146 if (s->v.ClassDef.decorator_list)
1114 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list); 1147 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1115 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock, 1148 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
1116 (void *)s, s->lineno, s->col_offset)) 1149 (void *)s, s->lineno, s->col_offset))
1117 return 0; 1150 return 0;
1118 if (!symtable_add_def(st, __class__, DEF_LOCAL) || 1151 if (!symtable_add_def(st, __class__, DEF_LOCAL) ||
1119 !GET_IDENTIFIER(__locals__) || 1152 !GET_IDENTIFIER(__locals__) ||
1120 !symtable_add_def(st, __locals__, DEF_PARAM)) { 1153 !symtable_add_def(st, __locals__, DEF_PARAM)) {
1121 symtable_exit_block(st, s); 1154 symtable_exit_block(st, s);
1122 return 0; 1155 return 0;
1123 } 1156 }
1124 tmp = st->st_private; 1157 tmp = st->st_private;
1125 st->st_private = s->v.ClassDef.name; 1158 st->st_private = s->v.ClassDef.name;
1126 VISIT_SEQ(st, stmt, s->v.ClassDef.body); 1159 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
1127 st->st_private = tmp; 1160 st->st_private = tmp;
1128 if (!symtable_exit_block(st, s)) 1161 if (!symtable_exit_block(st, s))
1129 return 0; 1162 return 0;
1130 break; 1163 break;
1131 } 1164 }
1132 case Return_kind: 1165 case Return_kind:
1133 if (s->v.Return.value) { 1166 if (s->v.Return.value) {
1134 VISIT(st, expr, s->v.Return.value); 1167 VISIT(st, expr, s->v.Return.value);
1135 st->st_cur->ste_returns_value = 1; 1168 st->st_cur->ste_returns_value = 1;
1136 if (st->st_cur->ste_generator) { 1169 if (st->st_cur->ste_generator) {
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
1297 break; 1330 break;
1298 case Lambda_kind: { 1331 case Lambda_kind: {
1299 if (!GET_IDENTIFIER(lambda)) 1332 if (!GET_IDENTIFIER(lambda))
1300 return 0; 1333 return 0;
1301 if (e->v.Lambda.args->defaults) 1334 if (e->v.Lambda.args->defaults)
1302 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults); 1335 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1303 if (!symtable_enter_block(st, lambda, 1336 if (!symtable_enter_block(st, lambda,
1304 FunctionBlock, (void *)e, e->lineno, 1337 FunctionBlock, (void *)e, e->lineno,
1305 e->col_offset)) 1338 e->col_offset))
1306 return 0; 1339 return 0;
1307 VISIT(st, arguments, e->v.Lambda.args); 1340 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1308 VISIT(st, expr, e->v.Lambda.body); 1341 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
1309 if (!symtable_exit_block(st, (void *)e)) 1342 if (!symtable_exit_block(st, (void *)e))
1310 return 0; 1343 return 0;
1311 break; 1344 break;
1312 } 1345 }
1313 case IfExp_kind: 1346 case IfExp_kind:
1314 VISIT(st, expr, e->v.IfExp.test); 1347 VISIT(st, expr, e->v.IfExp.test);
1315 VISIT(st, expr, e->v.IfExp.body); 1348 VISIT(st, expr, e->v.IfExp.body);
1316 VISIT(st, expr, e->v.IfExp.orelse); 1349 VISIT(st, expr, e->v.IfExp.orelse);
1317 break; 1350 break;
1318 case Dict_kind: 1351 case Dict_kind:
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
1618 /* Outermost iter is received as an argument */ 1651 /* Outermost iter is received as an argument */
1619 if (!symtable_implicit_arg(st, 0)) { 1652 if (!symtable_implicit_arg(st, 0)) {
1620 symtable_exit_block(st, (void *)e); 1653 symtable_exit_block(st, (void *)e);
1621 return 0; 1654 return 0;
1622 } 1655 }
1623 /* Allocate temporary name if needed */ 1656 /* Allocate temporary name if needed */
1624 if (needs_tmp && !symtable_new_tmpname(st)) { 1657 if (needs_tmp && !symtable_new_tmpname(st)) {
1625 symtable_exit_block(st, (void *)e); 1658 symtable_exit_block(st, (void *)e);
1626 return 0; 1659 return 0;
1627 } 1660 }
1628 VISIT(st, expr, outermost->target); 1661 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1629 VISIT_SEQ(st, expr, outermost->ifs); 1662 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1630 VISIT_SEQ_TAIL(st, comprehension, generators, 1); 1663 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1664 generators, 1, (void*)e);
1631 if (value) 1665 if (value)
1632 VISIT(st, expr, value); 1666 VISIT_IN_BLOCK(st, expr, value, (void*)e);
1633 VISIT(st, expr, elt); 1667 VISIT_IN_BLOCK(st, expr, elt, (void*)e);
1634 return symtable_exit_block(st, (void *)e); 1668 return symtable_exit_block(st, (void *)e);
1635 } 1669 }
1636 1670
1637 static int 1671 static int
1638 symtable_visit_genexp(struct symtable *st, expr_ty e) 1672 symtable_visit_genexp(struct symtable *st, expr_ty e)
1639 { 1673 {
1640 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr), 1674 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1641 e->v.GeneratorExp.generators, 1675 e->v.GeneratorExp.generators,
1642 e->v.GeneratorExp.elt, NULL); 1676 e->v.GeneratorExp.elt, NULL);
1643 } 1677 }
(...skipping 15 matching lines...) Expand all
1659 } 1693 }
1660 1694
1661 static int 1695 static int
1662 symtable_visit_dictcomp(struct symtable *st, expr_ty e) 1696 symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1663 { 1697 {
1664 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp), 1698 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1665 e->v.DictComp.generators, 1699 e->v.DictComp.generators,
1666 e->v.DictComp.key, 1700 e->v.DictComp.key,
1667 e->v.DictComp.value); 1701 e->v.DictComp.value);
1668 } 1702 }
OLDNEW
« no previous file with comments | « Python/pythonrun.c ('k') | Python/thread.c » ('j') | no next file with comments »

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