diff options
author | Benjamin Peterson <benjamin@python.org> | 2009-06-28 03:18:59 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2009-06-28 03:18:59 (GMT) |
commit | 876b2f286b9af1a69846c84d1229014b2a885c97 (patch) | |
tree | 1934d1ce119603a4ae7a0a37c01957a3ca2d17a2 /Python/ceval.c | |
parent | d2397753ee3d20579aa60b5e1c037e9e20db7ccb (diff) | |
download | cpython-876b2f286b9af1a69846c84d1229014b2a885c97.zip cpython-876b2f286b9af1a69846c84d1229014b2a885c97.tar.gz cpython-876b2f286b9af1a69846c84d1229014b2a885c97.tar.bz2 |
Merged revisions 72912,72920,72940 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r72912 | benjamin.peterson | 2009-05-25 08:13:44 -0500 (Mon, 25 May 2009) | 5 lines
add a SETUP_WITH opcode
It speeds up the with statement and correctly looks up the special
methods involved.
........
r72920 | benjamin.peterson | 2009-05-25 15:12:57 -0500 (Mon, 25 May 2009) | 1 line
take into account the fact that SETUP_WITH pushes a finally block
........
r72940 | benjamin.peterson | 2009-05-26 07:49:59 -0500 (Tue, 26 May 2009) | 1 line
teach the peepholer about SETUP_WITH
........
Diffstat (limited to 'Python/ceval.c')
-rw-r--r-- | Python/ceval.c | 70 |
1 files changed, 63 insertions, 7 deletions
diff --git a/Python/ceval.c b/Python/ceval.c index b5b5c27..b689f3d 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -119,6 +119,7 @@ static int import_all_from(PyObject *, PyObject *); static void format_exc_check_arg(PyObject *, const char *, PyObject *); static PyObject * unicode_concatenate(PyObject *, PyObject *, PyFrameObject *, unsigned char *); +static PyObject * special_lookup(PyObject *, char *, PyObject **); #define NAME_ERROR_MSG \ "name '%.200s' is not defined" @@ -2455,6 +2456,33 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) STACK_LEVEL()); DISPATCH(); + TARGET(SETUP_WITH) + { + static PyObject *exit, *enter; + w = TOP(); + x = special_lookup(w, "__exit__", &exit); + if (!x) + break; + SET_TOP(x); + u = special_lookup(w, "__enter__", &enter); + Py_DECREF(w); + if (!u) { + x = NULL; + break; + } + x = PyObject_CallFunctionObjArgs(u, NULL); + Py_DECREF(u); + if (!x) + break; + /* Setup the finally block before pushing the result + of __enter__ on the stack. */ + PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, + STACK_LEVEL()); + + PUSH(x); + DISPATCH(); + } + TARGET(WITH_CLEANUP) { /* At the top of the stack are 1-3 values indicating @@ -2479,17 +2507,36 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) should still be resumed.) */ - PyObject *exit_func = POP(); + PyObject *exit_func; u = TOP(); if (u == Py_None) { + POP(); + exit_func = TOP(); + SET_TOP(u); v = w = Py_None; } else if (PyLong_Check(u)) { + POP(); + switch(PyLong_AsLong(u)) { + case WHY_RETURN: + case WHY_CONTINUE: + /* Retval in TOP. */ + exit_func = SECOND(); + SET_SECOND(TOP()); + SET_TOP(u); + break; + default: + exit_func = TOP(); + SET_TOP(u); + break; + } u = v = w = Py_None; } else { - v = SECOND(); + v = SECOND(); w = THIRD(); + exit_func = stack_pointer[-7]; + stack_pointer[-7] = NULL; } /* XXX Not the fastest way to call it... */ x = PyObject_CallFunctionObjArgs(exit_func, u, v, w, @@ -2509,11 +2556,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) else if (err > 0) { err = 0; /* There was an exception and a True return */ - STACKADJ(-2); - SET_TOP(PyLong_FromLong((long) WHY_SILENCED)); - Py_DECREF(u); - Py_DECREF(v); - Py_DECREF(w); + PUSH(PyLong_FromLong((long) WHY_SILENCED)); } PREDICT(END_FINALLY); break; @@ -3194,6 +3237,19 @@ fail: /* Jump here from prelude on failure */ } +static PyObject * +special_lookup(PyObject *o, char *meth, PyObject **cache) +{ + PyObject *res; + res = _PyObject_LookupSpecial(o, meth, cache); + if (res == NULL && !PyErr_Occurred()) { + PyErr_SetObject(PyExc_AttributeError, *cache); + return NULL; + } + return res; +} + + /* Logic for the raise statement (too complicated for inlining). This *consumes* a reference count to each of its arguments. */ static enum why_code |