mirror of
https://github.com/python/cpython.git
synced 2025-07-19 01:05:26 +00:00
Close #14857: fix regression in references to PEP 3135 implicit __class__ closure variable. Reopens issue #12370, but also updates unittest.mock to workaround that issue
This commit is contained in:
parent
5c6eba3a93
commit
0b43bcf528
7 changed files with 45 additions and 17 deletions
|
@ -1676,7 +1676,7 @@ compiler_class(struct compiler *c, stmt_ty s)
|
|||
return 0;
|
||||
}
|
||||
/* return the (empty) __class__ cell */
|
||||
str = PyUnicode_InternFromString("@__class__");
|
||||
str = PyUnicode_InternFromString("__class__");
|
||||
if (str == NULL) {
|
||||
compiler_exit_scope(c);
|
||||
return 0;
|
||||
|
|
|
@ -106,6 +106,7 @@ typedef unsigned short mode_t;
|
|||
Python 3.3a0 3200 (__qualname__ added)
|
||||
3210 (added size modulo 2**32 to the pyc header)
|
||||
Python 3.3a1 3220 (changed PEP 380 implementation)
|
||||
Python 3.3a4 3230 (revert changes to implicit __class__ closure)
|
||||
*/
|
||||
|
||||
/* MAGIC must change whenever the bytecode emitted by the compiler may no
|
||||
|
@ -118,7 +119,7 @@ typedef unsigned short mode_t;
|
|||
#define STRIFY(name) QUOTE(name)
|
||||
#define MAJOR STRIFY(PY_MAJOR_VERSION)
|
||||
#define MINOR STRIFY(PY_MINOR_VERSION)
|
||||
#define MAGIC (3220 | ((long)'\r'<<16) | ((long)'\n'<<24))
|
||||
#define MAGIC (3230 | ((long)'\r'<<16) | ((long)'\n'<<24))
|
||||
#define TAG "cpython-" MAJOR MINOR;
|
||||
#define CACHEDIR "__pycache__"
|
||||
/* Current magic word and string tag as globals. */
|
||||
|
|
|
@ -221,17 +221,10 @@ symtable_new(void)
|
|||
struct symtable *
|
||||
PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
|
||||
{
|
||||
struct symtable *st;
|
||||
struct symtable *st = symtable_new();
|
||||
asdl_seq *seq;
|
||||
int i;
|
||||
|
||||
if (__class__ == NULL) {
|
||||
__class__ = PyUnicode_InternFromString("@__class__");
|
||||
if (__class__ == NULL)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
st = symtable_new();
|
||||
if (st == NULL)
|
||||
return st;
|
||||
st->st_filename = filename;
|
||||
|
@ -747,6 +740,8 @@ analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
|
|||
}
|
||||
else {
|
||||
/* Special-case __class__ */
|
||||
if (!GET_IDENTIFIER(__class__))
|
||||
goto error;
|
||||
assert(PySet_Contains(local, __class__) == 1);
|
||||
if (PySet_Add(newbound, __class__) < 0)
|
||||
goto error;
|
||||
|
@ -784,7 +779,7 @@ analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
|
|||
NULL))
|
||||
goto error;
|
||||
else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree,
|
||||
"@__class__"))
|
||||
"__class__"))
|
||||
goto error;
|
||||
/* Records the results of the analysis in the symbol table entry */
|
||||
if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
|
||||
|
@ -1111,7 +1106,8 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
|
|||
if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
|
||||
(void *)s, s->lineno, s->col_offset))
|
||||
return 0;
|
||||
if (!symtable_add_def(st, __class__, DEF_LOCAL) ||
|
||||
if (!GET_IDENTIFIER(__class__) ||
|
||||
!symtable_add_def(st, __class__, DEF_LOCAL) ||
|
||||
!GET_IDENTIFIER(__locals__) ||
|
||||
!symtable_add_def(st, __locals__, DEF_PARAM)) {
|
||||
symtable_exit_block(st, s);
|
||||
|
@ -1376,7 +1372,8 @@ symtable_visit_expr(struct symtable *st, expr_ty e)
|
|||
if (e->v.Name.ctx == Load &&
|
||||
st->st_cur->ste_type == FunctionBlock &&
|
||||
!PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
|
||||
if (!symtable_add_def(st, __class__, USE))
|
||||
if (!GET_IDENTIFIER(__class__) ||
|
||||
!symtable_add_def(st, __class__, USE))
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue