Skip to content

Commit bfd8441

Browse files
committed
Check for complete statements. Requires Py>=3.4.
1 parent a9dfd2b commit bfd8441

File tree

3 files changed

+48
-24
lines changed

3 files changed

+48
-24
lines changed

README.rst

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
|Python3x|
2+
3+
.. |Python3x| image:: https://img.shields.io/badge/python-3.x-blue.svg
4+
15
A Jupyter/IPython kernel for Matlab
26
===================================
37

4-
This requires `Jupyter Notebook <http://jupyter.readthedocs.org/en/latest/install.html>`_,
5-
and the `Matlab engine for Python <https://www.mathworks.com/help/matlab/matlab-engine-for-python.html>`_.
8+
This requires `Jupyter Notebook <http://jupyter.readthedocs.org/en/latest/install.html>`_
9+
with Python 3.4+, and the
10+
`Matlab engine for Python <https://www.mathworks.com/help/matlab/matlab-engine-for-python.html>`_.
611

712
To install::
813

matlab_kernel/kernel.py

+34-20
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
from functools import partial
33
from io import StringIO
44
import os
5-
import shutil
5+
from pathlib import Path
66
import sys
7-
import tempfile
7+
from tempfile import TemporaryDirectory
88

99
from IPython.display import Image
1010
from metakernel import MetaKernel
@@ -78,24 +78,25 @@ def do_execute_direct(self, code):
7878
if settings["backend"] == "inline":
7979
nfig = len(self._matlab.get(0., "children"))
8080
if nfig:
81-
tmpdir = tempfile.mkdtemp()
82-
try:
83-
self._matlab.eval(
84-
"arrayfun("
85-
"@(h, i) print(h, sprintf('{}/%i', i), '-d{}', '-r{}'),"
86-
"get(0, 'children'), (1:{})')"
87-
.format(tmpdir, settings["format"], settings["resolution"], nfig),
88-
nargout=0)
89-
self._matlab.eval(
90-
"arrayfun(@(h) close(h), get(0, 'children'))",
91-
nargout=0)
92-
for fname in sorted(os.listdir(tmpdir)):
93-
self.Display(Image(
94-
filename="{}/{}".format(tmpdir, fname)))
95-
except Exception as exc:
96-
self.Error(exc)
97-
finally:
98-
shutil.rmtree(tmpdir)
81+
with TemporaryDirectory() as tmpdir:
82+
try:
83+
self._matlab.eval(
84+
"arrayfun("
85+
"@(h, i) print(h, sprintf('{}/%i', i), '-d{}', '-r{}'),"
86+
"get(0, 'children'), (1:{})')".format(
87+
tmpdir,
88+
settings["format"],
89+
settings["resolution"],
90+
nfig),
91+
nargout=0)
92+
self._matlab.eval(
93+
"arrayfun(@(h) close(h), get(0, 'children'))",
94+
nargout=0)
95+
for fname in sorted(os.listdir(tmpdir)):
96+
self.Display(Image(
97+
filename="{}/{}".format(tmpdir, fname)))
98+
except Exception as exc:
99+
self.Error(exc)
99100

100101
def get_kernel_help_on(self, info, level=0, none_on_fail=False):
101102
name = info.get("help_obj", "")
@@ -146,6 +147,19 @@ def get_completions(self, info):
146147

147148
return compls
148149

150+
def do_is_complete(self, code):
151+
if self.parse_code(code)["magic"]:
152+
return {"status": "complete"}
153+
with TemporaryDirectory() as tmpdir:
154+
Path(tmpdir, "test_complete.m").write_text(code)
155+
self._matlab.eval(
156+
"try, pcode {} -inplace; catch, end".format(tmpdir),
157+
nargout=0)
158+
if Path(tmpdir, "test_complete.p").exists():
159+
return {"status": "complete"}
160+
else:
161+
return {"status": "incomplete"}
162+
149163
def handle_plot_settings(self):
150164
raw = self.plot_settings
151165
settings = self._validated_plot_settings

setup.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
import sys
2+
13
from setuptools import setup, find_packages
24
import versioneer
35

46

57
if __name__ == "__main__":
8+
if sys.version_info < (3, 4):
9+
raise ImportError("matlab_kernel requires Python>=3.4")
10+
611
setup(name="matlab_kernel",
712
author="Steven Silvester, Antony Lee",
813
version=versioneer.get_version(),
@@ -12,8 +17,8 @@
1217
long_description=open("README.rst").read(),
1318
classifiers=["Framework :: IPython",
1419
"License :: OSI Approved :: BSD License",
15-
"Programming Language :: Python :: 2",
16-
"Programming Language :: Python :: 3",
20+
"Programming Language :: Python :: 3.4",
21+
"Programming Language :: Python :: 3.5",
1722
"Topic :: System :: Shells",],
1823
packages=find_packages(include=["matlab_kernel", "matlab_kernel.*"]),
1924
install_requires=["metakernel>=0.13.1",

0 commit comments

Comments
 (0)