Skip to content

Commit

Permalink
Rename pyopencl.ipython to pyopencl.ipython_ext to convince 2to3 that…
Browse files Browse the repository at this point in the history
… 'from IPython import' is not package-local
  • Loading branch information
inducer committed May 29, 2014
1 parent ad1b26f commit 2bb0d72
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
2 changes: 1 addition & 1 deletion doc/misc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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>`_
Expand Down
23 changes: 16 additions & 7 deletions examples/ipython-demo.ipynb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:85c637b863a4bbbd3fb91eca8682d36d9874a53a6db35b18f1c53bb53b3c6bdc"
"signature": "sha256:81f3deed7cdc26b0fc756b3ee1eb6e8f9b1be96304ddfc6ff484d223c2b8a942"
},
"nbformat": 3,
"nbformat_minor": 0,
Expand All @@ -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",
Expand All @@ -33,7 +42,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
"%load_ext pyopencl.ipython"
"%load_ext pyopencl.ipython_ext"
],
"language": "python",
"metadata": {},
Expand Down Expand Up @@ -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"
]
},
{
Expand Down Expand Up @@ -162,7 +171,7 @@
"output_type": "pyout",
"prompt_number": 8,
"text": [
"<pyopencl._cl.Event at 0x39dac20>"
"<pyopencl._cl.Event at 0x7fc14f3fdf30>"
]
}
],
Expand Down
3 changes: 3 additions & 0 deletions pyopencl/ipython.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

import pyopencl as cl

from warnings import warn
warn("pyopencl.ipython is deprecated. Use pyopencl.ipython_ext instead.")


@magics_class
class PyOpenCLMagics(Magics):
Expand Down
37 changes: 37 additions & 0 deletions pyopencl/ipython_ext.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 2bb0d72

Please sign in to comment.