summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
Diffstat (limited to 'Objects')
-rw-r--r--Objects/frameobject.c24
-rw-r--r--Objects/funcobject.c33
2 files changed, 54 insertions, 3 deletions
diff --git a/Objects/frameobject.c b/Objects/frameobject.c
index f541f1e..0f6372b 100644
--- a/Objects/frameobject.c
+++ b/Objects/frameobject.c
@@ -79,6 +79,7 @@ frame_dealloc(PyFrameObject *f)
Py_XDECREF(f->f_builtins);
Py_XDECREF(f->f_globals);
Py_XDECREF(f->f_locals);
+ Py_XDECREF(f->f_closure);
Py_XDECREF(f->f_trace);
Py_XDECREF(f->f_exc_type);
Py_XDECREF(f->f_exc_value);
@@ -106,14 +107,14 @@ PyTypeObject PyFrame_Type = {
};
PyFrameObject *
-PyFrame_New(PyThreadState *tstate, PyCodeObject *code,
- PyObject *globals, PyObject *locals)
+PyFrame_New(PyThreadState *tstate, PyCodeObject *code, PyObject *globals,
+ PyObject *locals, PyObject *closure)
{
PyFrameObject *back = tstate->frame;
static PyObject *builtin_object;
PyFrameObject *f;
PyObject *builtins;
- int extras;
+ int extras, ncells;
if (builtin_object == NULL) {
builtin_object = PyString_InternFromString("__builtins__");
@@ -128,6 +129,7 @@ PyFrame_New(PyThreadState *tstate, PyCodeObject *code,
return NULL;
}
extras = code->co_stacksize + code->co_nlocals;
+ ncells = PyTuple_GET_SIZE(code->co_cellvars);
if (back == NULL || back->f_globals != globals) {
builtins = PyDict_GetItem(globals, builtin_object);
if (builtins != NULL && PyModule_Check(builtins))
@@ -197,6 +199,22 @@ PyFrame_New(PyThreadState *tstate, PyCodeObject *code,
locals = globals;
Py_INCREF(locals);
}
+ if (closure || ncells) {
+ int i, size;
+ size = ncells;
+ if (closure)
+ size += PyTuple_GET_SIZE(closure);
+ f->f_closure = PyTuple_New(size);
+ for (i = 0; i < ncells; ++i)
+ PyTuple_SET_ITEM(f->f_closure, i, PyCell_New(NULL));
+ for (i = ncells; i < size; ++i) {
+ PyObject *o = PyTuple_GET_ITEM(closure, i - ncells);
+ Py_INCREF(o);
+ PyTuple_SET_ITEM(f->f_closure, i, o);
+ }
+ }
+ else
+ f->f_closure = NULL;
f->f_locals = locals;
f->f_trace = NULL;
f->f_exc_type = f->f_exc_value = f->f_exc_traceback = NULL;
diff --git a/Objects/funcobject.c b/Objects/funcobject.c
index c45f318..b7e25aa 100644
--- a/Objects/funcobject.c
+++ b/Objects/funcobject.c
@@ -20,6 +20,7 @@ PyFunction_New(PyObject *code, PyObject *globals)
op->func_name = ((PyCodeObject *)code)->co_name;
Py_INCREF(op->func_name);
op->func_defaults = NULL; /* No default arguments */
+ op->func_closure = NULL;
consts = ((PyCodeObject *)code)->co_consts;
if (PyTuple_Size(consts) >= 1) {
doc = PyTuple_GetItem(consts, 0);
@@ -89,6 +90,37 @@ PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
return 0;
}
+PyObject *
+PyFunction_GetClosure(PyObject *op)
+{
+ if (!PyFunction_Check(op)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ return ((PyFunctionObject *) op) -> func_closure;
+}
+
+int
+PyFunction_SetClosure(PyObject *op, PyObject *closure)
+{
+ if (!PyFunction_Check(op)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ if (closure == Py_None)
+ closure = NULL;
+ else if (PyTuple_Check(closure)) {
+ Py_XINCREF(closure);
+ }
+ else {
+ PyErr_SetString(PyExc_SystemError, "non-tuple closure");
+ return -1;
+ }
+ Py_XDECREF(((PyFunctionObject *) op) -> func_closure);
+ ((PyFunctionObject *) op) -> func_closure = closure;
+ return 0;
+}
+
/* Methods */
#define OFF(x) offsetof(PyFunctionObject, x)
@@ -98,6 +130,7 @@ static struct memberlist func_memberlist[] = {
{"func_globals", T_OBJECT, OFF(func_globals), READONLY},
{"func_name", T_OBJECT, OFF(func_name), READONLY},
{"__name__", T_OBJECT, OFF(func_name), READONLY},
+ {"func_closure", T_OBJECT, OFF(func_closure)},
{"func_defaults", T_OBJECT, OFF(func_defaults)},
{"func_doc", T_OBJECT, OFF(func_doc)},
{"__doc__", T_OBJECT, OFF(func_doc)},