summaryrefslogtreecommitdiffstats
path: root/Modules/threadmodule.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2006-02-28 21:57:43 (GMT)
committerGuido van Rossum <guido@python.org>2006-02-28 21:57:43 (GMT)
commit1a5e21e0334a6d4e1c756575023c7157fc9ee306 (patch)
treed2c1c9383b3c6d8194449ae756e663b0b0ac9e4e /Modules/threadmodule.c
parent87a8b4fee56b8204ee9f7b0ce2e5db0564e8f86e (diff)
downloadcpython-1a5e21e0334a6d4e1c756575023c7157fc9ee306.zip
cpython-1a5e21e0334a6d4e1c756575023c7157fc9ee306.tar.gz
cpython-1a5e21e0334a6d4e1c756575023c7157fc9ee306.tar.bz2
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.c36
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 */
};