diff options
Diffstat (limited to 'Python/compile.c')
-rw-r--r-- | Python/compile.c | 64 |
1 files changed, 41 insertions, 23 deletions
diff --git a/Python/compile.c b/Python/compile.c index fde4591..0f5fd2d 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -373,10 +373,12 @@ dictbytype(PyObject *src, int scope_type, int flag, int offset) while (PyDict_Next(src, &pos, &k, &v)) { /* XXX this should probably be a macro in symtable.h */ + long vi; assert(PyInt_Check(v)); - scope = (PyInt_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK; + vi = PyInt_AS_LONG(v); + scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; - if (scope == scope_type || PyInt_AS_LONG(v) & flag) { + if (scope == scope_type || vi & flag) { PyObject *tuple, *item = PyInt_FromLong(i); if (item == NULL) { Py_DECREF(dest); @@ -1125,7 +1127,8 @@ compiler_body(struct compiler *c, asdl_seq *stmts) if (!asdl_seq_LEN(stmts)) return 1; st = (stmt_ty)asdl_seq_GET(stmts, 0); - if (compiler_isdocstring(st)) { + if (compiler_isdocstring(st) && Py_OptimizeFlag < 2) { + /* don't generate docstrings if -OO */ i = 1; VISIT(c, expr, st->v.Expr.value); if (!compiler_nameop(c, __doc__, Store)) @@ -1251,7 +1254,8 @@ compiler_make_closure(struct compiler *c, PyCodeObject *co, int args) else /* (reftype == FREE) */ arg = compiler_lookup_arg(c->u->u_freevars, name); if (arg == -1) { - printf("lookup %s in %s %d %d\n" + fprintf(stderr, + "lookup %s in %s %d %d\n" "freevars of %s: %s\n", PyObject_REPR(name), PyString_AS_STRING(c->u->u_name), @@ -1474,7 +1478,6 @@ compiler_function(struct compiler *c, stmt_ty s) static int compiler_class(struct compiler *c, stmt_ty s) { - static PyObject *build_class = NULL; static PyObject *locals = NULL; PyCodeObject *co; PyObject *str; @@ -1485,13 +1488,7 @@ compiler_class(struct compiler *c, stmt_ty s) if (!compiler_decorators(c, decos)) return 0; - /* initialize statics */ - if (build_class == NULL) { - build_class = PyUnicode_FromString("__build_class__"); - if (build_class == NULL) - return 0; - } if (locals == NULL) { locals = PyUnicode_FromString("__locals__"); if (locals == NULL) @@ -1501,14 +1498,16 @@ compiler_class(struct compiler *c, stmt_ty s) /* ultimately generate code for: <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>) where: - <func> is a function/closure created from the class body + <func> is a function/closure created from the class body; + it has a single argument (__locals__) where the dict + (or MutableSequence) representing the locals is passed <name> is the class name <bases> is the positional arguments and *varargs argument <keywords> is the keyword arguments and **kwds argument This borrows from compiler_call. */ - /* 0. Create a fake variable named __locals__ */ + /* 0. Create a fake argument named __locals__ */ ste = PySymtable_Lookup(c->c_st, s); if (ste == NULL) return 0; @@ -1528,11 +1527,11 @@ compiler_class(struct compiler *c, stmt_ty s) c->u->u_private = s->v.ClassDef.name; /* force it to have one mandatory argument */ c->u->u_argcount = 1; - /* load the first argument ... */ + /* load the first argument (__locals__) ... */ ADDOP_I(c, LOAD_FAST, 0); /* ... and store it into f_locals */ ADDOP_IN_SCOPE(c, STORE_LOCALS); - /* load __name__ ... */ + /* load (global) __name__ ... */ str = PyUnicode_InternFromString("__name__"); if (!str || !compiler_nameop(c, str, Load)) { Py_XDECREF(str); @@ -1553,8 +1552,24 @@ compiler_class(struct compiler *c, stmt_ty s) compiler_exit_scope(c); return 0; } - /* return None */ - ADDOP_O(c, LOAD_CONST, Py_None, consts); + /* return the (empty) __class__ cell */ + str = PyUnicode_InternFromString("__class__"); + if (str == NULL) { + compiler_exit_scope(c); + return 0; + } + i = compiler_lookup_arg(c->u->u_cellvars, str); + Py_DECREF(str); + if (i == -1) { + /* This happens when nobody references the cell */ + PyErr_Clear(); + /* Return None */ + ADDOP_O(c, LOAD_CONST, Py_None, consts); + } + else { + /* Return the cell where to store __class__ */ + ADDOP_I(c, LOAD_CLOSURE, i); + } ADDOP_IN_SCOPE(c, RETURN_VALUE); /* create the code object */ co = assemble(c, 1); @@ -2421,7 +2436,7 @@ compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) return compiler_error(c, "can not assign to __debug__"); } -mangled = _Py_Mangle(c->u->u_private, name); + mangled = _Py_Mangle(c->u->u_private, name); if (!mangled) return 0; @@ -2947,6 +2962,7 @@ compiler_visit_keyword(struct compiler *c, keyword_ty k) static int expr_constant(expr_ty e) { + char *id; switch (e->kind) { case Ellipsis_kind: return 1; @@ -2955,11 +2971,13 @@ expr_constant(expr_ty e) case Str_kind: return PyObject_IsTrue(e->v.Str.s); case Name_kind: - /* __debug__ is not assignable, so we can optimize - * it away in if and while statements */ - if (PyUnicode_CompareWithASCIIString(e->v.Name.id, - "__debug__") == 0) - return ! Py_OptimizeFlag; + /* optimize away names that can't be reassigned */ + id = _PyUnicode_AsDefaultEncodedString(e->v.Name.id, NULL); + if (strcmp(id, "True") == 0) return 1; + if (strcmp(id, "False") == 0) return 0; + if (strcmp(id, "None") == 0) return 0; + if (strcmp(id, "__debug__") == 0) + return ! Py_OptimizeFlag; /* fall through */ default: return -1; |