From 135c35a8233ed8314030654d6570e7c96e1b8b04 Mon Sep 17 00:00:00 2001
From: Andreas Kloeckner <inform@tiker.net>
Date: Sun, 2 Jun 2013 16:48:22 -0400
Subject: [PATCH] Allow queues to be used as context managers.

---
 doc/source/misc.rst    |  1 +
 doc/source/runtime.rst |  9 +++++++++
 pyopencl/__init__.py   | 11 +++++++++++
 3 files changed, 21 insertions(+)

diff --git a/doc/source/misc.rst b/doc/source/misc.rst
index 79c63518..ba86c530 100644
--- a/doc/source/misc.rst
+++ b/doc/source/misc.rst
@@ -102,6 +102,7 @@ Version 2013.1
 * Add :meth:`pyopencl.array.Array.__getitem__`, supporting generic slicing.
   Note that many (most) operations on sliced arrays will fail for now.
   This will be fixed in a future release.
+* :class:`pyopencl.CommandQueue` may be used as a context manager (in a ``with`` statement)
 
 Version 2012.1
 --------------
diff --git a/doc/source/runtime.rst b/doc/source/runtime.rst
index cc28e561..f2b8fdbc 100644
--- a/doc/source/runtime.rst
+++ b/doc/source/runtime.rst
@@ -195,6 +195,15 @@ Command Queues and Events
     if *device* is None, one of the devices in *context* is chosen
     in an implementation-defined manner.
 
+    A :class:`CommandQueue` may be used as a context manager, like this::
+
+        with cl.CommandQueue(self.cl_context) as queue:
+            enqueue_stuff(queue, ...)
+
+    .. versionadded:: 2013.1
+
+        Context manager capability.
+
     .. attribute:: info
 
         Lower case versions of the :class:`command_queue_info` constants
diff --git a/pyopencl/__init__.py b/pyopencl/__init__.py
index c9ccf4ba..11e4fc5a 100644
--- a/pyopencl/__init__.py
+++ b/pyopencl/__init__.py
@@ -450,6 +450,17 @@ def _add_functionality():
 
     # }}}
 
+    # {{{ CommandQueue
+
+    def command_queue_enter(self):
+        return self
+
+    def command_queue_exit(self, exc_type, exc_val, exc_tb):
+        return self
+
+    CommandQueue.__enter__ = command_queue_enter
+    CommandQueue.__exit__ = command_queue_exit
+
     # {{{ _Program (the internal, non-caching version)
 
     def program_get_build_logs(self):
-- 
GitLab