8000 Add support for Windows and Python 2.7 · Calysto/matlab_kernel@eb550b0 · GitHub
[go: up one dir, main page]

Skip to content

Commit eb550b0

Browse files
committed
Add support for Windows and Python 2.7
1 parent 555ce44 commit eb550b0

File tree

3 files changed

+50
-22
lines changed

3 files changed

+50
-22
lines changed

README.rst

Lines changed: 0 additions & 4 deletions
< 10000 /div>
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
|Python3x|
2-
3-
.. |Python3x| image:: https://img.shields.io/badge/python-3.x-blue.svg
41

52
A Jupyter/IPython kernel for Matlab
63
===================================
74

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

128
To install::

matlab_kernel/kernel.py

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,29 @@
33
import os
44
from pathlib import Path
55
import sys
6-
from tempfile import TemporaryDirectory
6+
try:
7+
from tempfile import TemporaryDirectory
8+
except ImportError:
9+
from backports import tempfile
710

811
from IPython.display import Image
912
from metakernel import MetaKernel
1013

11-
import matlab.engine
12-
from matlab.engine import MatlabExecutionError
13-
1414
from . import __version__
15-
from .wurlitzer import Wurlitzer
15+
16+
try:
17+
from .wurlitzer import Wurlitzer
18+
except ImportError:
19+
Wurlitzer = None
20+
21+
try:
22+
import matlab.engine
23+
from matlab.engine import MatlabExecutionError
24+
except ImportError:
25+
raise ImportError("""
26+
Matlab engine not installed:
27+
See https://www.mathworks.com/help/matlab/matlab-engine-for-python.htm
28+
""")
1629

1730

1831
class _PseudoStream:
@@ -67,13 +80,10 @@ def do_execute_direct(self, code):
6780
self._matlab.get(0., "defaultfigureposition")[0][2:])
6881
self.handle_plot_settings()
6982

70-
try:
71-
with Wurlitzer(_PseudoStream(partial(self.Print, end="")),
72-
_PseudoStream(partial(self.Error, end=""))):
73-
future = self._matlab.eval(code, nargout=0, async=True)
74-
future.result()
75-
except (SyntaxError, MatlabExecutionError, KeyboardInterrupt):
76-
pass
83+
if Wurlitzer:
84+
self._execute_async(code)
85+
else:
86+
self._execute_sync(code)
7787

7888
settings = self._validated_plot_settings
7989
if settings["backend"] == "inline":
@@ -216,6 +226,27 @@ def do_shutdown(self, restart):
216226
self._matlab.exit()
217227
return super(MatlabKernel, self).do_shutdown(restart)
218228

229+
def _execute_async(self, code):
230+
try:
231+
with Wurlitzer(_PseudoStream(partial(self.Print, end="")),
232+
_PseudoStream(partial(self.Error, end=""))):
233+
future = self._matlab.eval(code, nargout=0, async=True)
234+
future.result()
235+
except (SyntaxError, MatlabExecutionError, KeyboardInterrupt):
236+
pass
237+
238+
def _execute_sync(self, code):
239+
out = StringIO()
240+
err = StringIO()
241+
try:
242+
self._matlab.eval(code, nargout=0, stdout=out, stderr=err)
243+
except (SyntaxError, MatlabExecutionError) as exc:
244+
stdout = exc.args[0]
245+
self.Error(stdout)
246+
return
247+
stdout = out.getvalue()
248+
self.Print(stdout)
249+
219250

220251
if __name__ == '__main__':
221252
try:

setup.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
import sys
21

32
from setuptools import setup, find_packages
43
import versioneer
54

65

76
if __name__ == "__main__":
8-
if sys.version_info < (3, 4):
9-
raise ImportError("matlab_kernel requires Python>=3.4")
10-
117
setup(name="matlab_kernel",
128
author="Steven Silvester, Antony Lee",
139
version=versioneer.get_version(),
@@ -19,7 +15,12 @@
1915
"License :: OSI Approved :: BSD License",
2016
"Programming Language :: Python :: 3.4",
2117
"Programming Language :: Python :: 3.5",
22-
"Topic :: System :: Shells",],
18+
"Topic :: System :: Shells"],
2319
packages=find_packages(include=["matlab_kernel", "matlab_kernel.*"]),
24-
install_requires=["metakernel>=0.13.1"],
20+
requires=["metakernel (>0.18.0)", "jupyter_client (>=4.4.0)",
21+
"pathlib", 'ipython (>=4.0.0)'],
22+
install_requires=["metakernel>=0.18.0", "jupyter_client >=4.4.0",
23+
"ipython>=4.0.0",
24+
"pathlib;python_version<'3.4'",
25+
"backports.tempfile;python_version<'3.0'"]
2526
)

0 commit comments

Comments
 (0)
0