From 2bb0d720892b5c751f3096fe8d9496612dd15cf3 Mon Sep 17 00:00:00 2001
From: Andreas Kloeckner <inform@tiker.net>
Date: Thu, 29 May 2014 12:33:39 -0500
Subject: [PATCH] Rename pyopencl.ipython to pyopencl.ipython_ext to convince
 2to3 that 'from IPython import' is not package-local

---
 doc/misc.rst                |  2 +-
 examples/ipython-demo.ipynb | 23 ++++++++++++++++-------
 pyopencl/ipython.py         |  3 +++
 pyopencl/ipython_ext.py     | 37 +++++++++++++++++++++++++++++++++++++
 4 files changed, 57 insertions(+), 8 deletions(-)
 create mode 100644 pyopencl/ipython_ext.py

diff --git a/doc/misc.rst b/doc/misc.rst
index f1fc8cc8..3fda67b1 100644
--- a/doc/misc.rst
+++ b/doc/misc.rst
@@ -31,7 +31,7 @@ PyOpenCL comes with IPython integration, which lets you seamlessly integrate
 PyOpenCL kernels into your IPython notebooks. Simply load the PyOpenCL 
 IPython extension using::
 
-    %load_ext pyopencl.ipython
+    %load_ext pyopencl.ipython_ext
 
 and then use the ``%%cl_kernel`` 'cell-magic' command. See `this notebook
 <http://nbviewer.ipython.org/urls/raw.githubusercontent.com/pyopencl/pyopencl/master/examples/ipython-demo.ipynb>`_
diff --git a/examples/ipython-demo.ipynb b/examples/ipython-demo.ipynb
index 1635dcc3..b0e8159c 100644
--- a/examples/ipython-demo.ipynb
+++ b/examples/ipython-demo.ipynb
@@ -1,7 +1,7 @@
 {
  "metadata": {
   "name": "",
-  "signature": "sha256:85c637b863a4bbbd3fb91eca8682d36d9874a53a6db35b18f1c53bb53b3c6bdc"
+  "signature": "sha256:81f3deed7cdc26b0fc756b3ee1eb6e8f9b1be96304ddfc6ff484d223c2b8a942"
  },
  "nbformat": 3,
  "nbformat_minor": 0,
@@ -19,8 +19,17 @@
      ],
      "language": "python",
      "metadata": {},
-     "outputs": [],
-     "prompt_number": 2
+     "outputs": [
+      {
+       "output_type": "stream",
+       "stream": "stderr",
+       "text": [
+        "/usr/lib/python2.7/pkgutil.py:186: ImportWarning: Not importing directory '/usr/lib/python2.7/dist-packages/enthought': missing __init__.py\n",
+        "  file, filename, etc = imp.find_module(subname, path)\n"
+       ]
+      }
+     ],
+     "prompt_number": 1
     },
     {
      "cell_type": "markdown",
@@ -33,7 +42,7 @@
      "cell_type": "code",
      "collapsed": false,
      "input": [
-      "%load_ext pyopencl.ipython"
+      "%load_ext pyopencl.ipython_ext"
      ],
      "language": "python",
      "metadata": {},
@@ -62,8 +71,8 @@
        "stream": "stdout",
        "text": [
         "Choose platform:\n",
-        "[0] <pyopencl.Platform 'AMD Accelerated Parallel Processing' at 0x7f244be8e500>\n",
-        "[1] <pyopencl.Platform 'Intel(R) OpenCL' at 0x3adcef0>\n"
+        "[0] <pyopencl.Platform 'AMD Accelerated Parallel Processing' at 0x7fc14f1b0080>\n",
+        "[1] <pyopencl.Platform 'Intel(R) OpenCL' at 0x32aed00>\n"
        ]
       },
       {
@@ -162,7 +171,7 @@
        "output_type": "pyout",
        "prompt_number": 8,
        "text": [
-        "<pyopencl._cl.Event at 0x39dac20>"
+        "<pyopencl._cl.Event at 0x7fc14f3fdf30>"
        ]
       }
      ],
diff --git a/pyopencl/ipython.py b/pyopencl/ipython.py
index 309a6830..ca5d3cea 100644
--- a/pyopencl/ipython.py
+++ b/pyopencl/ipython.py
@@ -4,6 +4,9 @@ from IPython.core.magic import (magics_class, Magics, cell_magic)
 
 import pyopencl as cl
 
+from warnings import warn
+warn("pyopencl.ipython is deprecated. Use pyopencl.ipython_ext instead.")
+
 
 @magics_class
 class PyOpenCLMagics(Magics):
diff --git a/pyopencl/ipython_ext.py b/pyopencl/ipython_ext.py
new file mode 100644
index 00000000..309a6830
--- /dev/null
+++ b/pyopencl/ipython_ext.py
@@ -0,0 +1,37 @@
+from __future__ import division
+
+from IPython.core.magic import (magics_class, Magics, cell_magic)
+
+import pyopencl as cl
+
+
+@magics_class
+class PyOpenCLMagics(Magics):
+    @cell_magic
+    def cl_kernel(self, line, cell):
+        try:
+            ctx = self.shell.user_ns["cl_ctx"]
+        except KeyError:
+            ctx = None
+
+        if not isinstance(ctx, cl.Context):
+            ctx = None
+
+        if ctx is None:
+            try:
+                ctx = self.shell.user_ns["ctx"]
+            except KeyError:
+                ctx = None
+
+        if ctx is None:
+            raise RuntimeError("unable to locate cl context, which must be "
+                    "present in namespace as 'cl_ctx' or 'ctx'")
+
+        prg = cl.Program(ctx, cell.encode("utf8")).build()
+
+        for knl in prg.all_kernels():
+            self.shell.user_ns[knl.function_name] = knl
+
+
+def load_ipython_extension(ip):
+    ip.register_magics(PyOpenCLMagics)
-- 
GitLab