diff options
author | Guido van Rossum <guido@python.org> | 2006-02-28 21:57:43 +0000 |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2006-02-28 21:57:43 +0000 |
commit | 437a2e31793b86a0a8f2bf1d6f1a786a111540a3 (patch) | |
tree | 73cebb12fcf159603815a40127540b55cc7dfd24 /Modules/threadmodule.c | |
parent | fabb57675972d7648c02f6baffd54f64f82ea069 (diff) | |
download | cpython-437a2e31793b86a0a8f2bf1d6f1a786a111540a3.tar.gz |
Updates to the with-statement:
- New semantics for __exit__() -- it must re-raise the exception
if type is not None; the with-statement itself doesn't do this.
(See the updated PEP for motivation.)
- Added context managers to:
- file
- thread.LockType
- threading.{Lock,RLock,Condition,Semaphore,BoundedSemaphore}
- decimal.Context
- Added contextlib.py, which defines @contextmanager, nested(), closing().
- Unit tests all around; bot no docs yet.
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 fccdd62644..b0f77008df 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 */ }; |