diff options
Diffstat (limited to 'Modules/threadmodule.c')
-rw-r--r-- | Modules/threadmodule.c | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/Modules/threadmodule.c b/Modules/threadmodule.c index fccdd62..b0f7700 100644 --- a/Modules/threadmodule.c +++ b/Modules/threadmodule.c @@ -116,6 +116,36 @@ PyDoc_STRVAR(locked_doc, \n\ Return whether the lock is in the locked state."); +static PyObject * +lock_context(lockobject *self) +{ + Py_INCREF(self); + return (PyObject *)self; +} + +PyDoc_STRVAR(lock_exit_doc, +"__exit__(type, value, tb)\n\ +\n\ +Releases the lock; then re-raises the exception if type is not None."); + +static PyObject * +lock_exit(lockobject *self, PyObject *args) +{ + PyObject *type, *value, *tb, *result; + if (!PyArg_ParseTuple(args, "OOO:__exit__", &type, &value, &tb)) + return NULL; + result = lock_PyThread_release_lock(self); + if (result != NULL && type != Py_None) { + Py_DECREF(result); + result = NULL; + Py_INCREF(type); + Py_INCREF(value); + Py_INCREF(tb); + PyErr_Restore(type, value, tb); + } + return result; +} + static PyMethodDef lock_methods[] = { {"acquire_lock", (PyCFunction)lock_PyThread_acquire_lock, METH_VARARGS, acquire_doc}, @@ -129,6 +159,12 @@ static PyMethodDef lock_methods[] = { METH_NOARGS, locked_doc}, {"locked", (PyCFunction)lock_locked_lock, METH_NOARGS, locked_doc}, + {"__context__", (PyCFunction)lock_context, + METH_NOARGS, PyDoc_STR("__context__() -> self.")}, + {"__enter__", (PyCFunction)lock_PyThread_acquire_lock, + METH_VARARGS, acquire_doc}, + {"__exit__", (PyCFunction)lock_exit, + METH_VARARGS, lock_exit_doc}, {NULL, NULL} /* sentinel */ }; |