Mercurial > cpython-lru_cache-weakref
comparison Python/symtable.c @ 70889:2d62ee4e7d98
use a invalid name for the __class__ closure for super() (closes #12370)
This prevents the assignment of __class__ in the class body from breaking
super. (Although a determined person could do locals()["@__class__"] = 4)
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sun, 19 Jun 2011 19:42:22 -0500 |
| parents | e0e663132363 |
| children | c8192197d23d |
comparison
equal
deleted
inserted
replaced
| 70881:5dd75cdd445a | 70889:2d62ee4e7d98 |
|---|---|
| 223 } | 223 } |
| 224 | 224 |
| 225 struct symtable * | 225 struct symtable * |
| 226 PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future) | 226 PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future) |
| 227 { | 227 { |
| 228 struct symtable *st = symtable_new(); | 228 struct symtable *st; |
| 229 asdl_seq *seq; | 229 asdl_seq *seq; |
| 230 int i; | 230 int i; |
| 231 | 231 |
| 232 if (__class__ == NULL) { | |
| 233 __class__ = PyUnicode_InternFromString("@__class__"); | |
| 234 if (__class__ == NULL) | |
| 235 return NULL; | |
| 236 } | |
| 237 | |
| 238 st = symtable_new(); | |
| 232 if (st == NULL) | 239 if (st == NULL) |
| 233 return st; | 240 return st; |
| 234 st->st_filename = filename; | 241 st->st_filename = filename; |
| 235 st->st_future = future; | 242 st->st_future = future; |
| 236 /* Make the initial symbol information gathering pass */ | 243 /* Make the initial symbol information gathering pass */ |
| 742 goto error; | 749 goto error; |
| 743 Py_DECREF(temp); | 750 Py_DECREF(temp); |
| 744 } | 751 } |
| 745 else { | 752 else { |
| 746 /* Special-case __class__ */ | 753 /* Special-case __class__ */ |
| 747 if (!GET_IDENTIFIER(__class__)) | |
| 748 goto error; | |
| 749 assert(PySet_Contains(local, __class__) == 1); | 754 assert(PySet_Contains(local, __class__) == 1); |
| 750 if (PySet_Add(newbound, __class__) < 0) | 755 if (PySet_Add(newbound, __class__) < 0) |
| 751 goto error; | 756 goto error; |
| 752 } | 757 } |
| 753 | 758 |
| 781 /* Check if any local variables must be converted to cell variables */ | 786 /* Check if any local variables must be converted to cell variables */ |
| 782 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree, | 787 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree, |
| 783 NULL)) | 788 NULL)) |
| 784 goto error; | 789 goto error; |
| 785 else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree, | 790 else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree, |
| 786 "__class__")) | 791 "@__class__")) |
| 787 goto error; | 792 goto error; |
| 788 /* Records the results of the analysis in the symbol table entry */ | 793 /* Records the results of the analysis in the symbol table entry */ |
| 789 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree, | 794 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree, |
| 790 ste->ste_type == ClassBlock)) | 795 ste->ste_type == ClassBlock)) |
| 791 goto error; | 796 goto error; |
| 1141 if (s->v.ClassDef.decorator_list) | 1146 if (s->v.ClassDef.decorator_list) |
| 1142 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list); | 1147 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list); |
| 1143 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock, | 1148 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock, |
| 1144 (void *)s, s->lineno, s->col_offset)) | 1149 (void *)s, s->lineno, s->col_offset)) |
| 1145 return 0; | 1150 return 0; |
| 1146 if (!GET_IDENTIFIER(__class__) || | 1151 if (!symtable_add_def(st, __class__, DEF_LOCAL) || |
| 1147 !symtable_add_def(st, __class__, DEF_LOCAL) || | |
| 1148 !GET_IDENTIFIER(__locals__) || | 1152 !GET_IDENTIFIER(__locals__) || |
| 1149 !symtable_add_def(st, __locals__, DEF_PARAM)) { | 1153 !symtable_add_def(st, __locals__, DEF_PARAM)) { |
| 1150 symtable_exit_block(st, s); | 1154 symtable_exit_block(st, s); |
| 1151 return 0; | 1155 return 0; |
| 1152 } | 1156 } |
| 1415 return 0; | 1419 return 0; |
| 1416 /* Special-case super: it counts as a use of __class__ */ | 1420 /* Special-case super: it counts as a use of __class__ */ |
| 1417 if (e->v.Name.ctx == Load && | 1421 if (e->v.Name.ctx == Load && |
| 1418 st->st_cur->ste_type == FunctionBlock && | 1422 st->st_cur->ste_type == FunctionBlock && |
| 1419 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) { | 1423 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) { |
| 1420 if (!GET_IDENTIFIER(__class__) || | 1424 if (!symtable_add_def(st, __class__, USE)) |
| 1421 !symtable_add_def(st, __class__, USE)) | |
| 1422 return 0; | 1425 return 0; |
| 1423 } | 1426 } |
| 1424 break; | 1427 break; |
| 1425 /* child nodes of List and Tuple will have expr_context set */ | 1428 /* child nodes of List and Tuple will have expr_context set */ |
| 1426 case List_kind: | 1429 case List_kind: |
