Skip to content

Commit 859d208

Browse files
committed
disable Python for Matlab < R2022a
Allow force for experiments, but Python interface doesn't get caught by try-catch < R2022a
1 parent b66044f commit 859d208

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function v = pvt_get_python_version()
2+
3+
% Matlab < R2022a has a bug in the JIT compiler that breaks try-catch
4+
% for any py.* command. We use a separate private function to workaround that.
5+
6+
v = [];
7+
8+
try
9+
pe = pyenv();
10+
vs = pe.Version;
11+
if strlength(vs) == 0, return, end
12+
13+
vi = py.sys.version_info;
14+
v = [double(vi.major), double(vi.minor), double(vi.micro)];
15+
16+
vv = strsplit(vs, '.');
17+
assert(str2double(vv{1}) == v(1), "stdlib:python_version:ValueError", "Python major version %s did not match pyenv %d", vv{1}, v(1))
18+
assert(str2double(vv{2}) == v(2), "stdlib:python_version:ValueError", "Python minor version %s did not match pyenv %d", vv{1}, v(1))
19+
catch e
20+
switch e.identifier
21+
case {'Octave:undefined-function', 'MATLAB:Python:PythonUnavailable'} % pass
22+
otherwise, rethrow(e)
23+
end
24+
end
25+
26+
end

+stdlib/python_version.m

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,20 @@
66
% where the environment has changed since pyenv() was set. For example
77
% HPC with "module load python3..."
88

9-
function v = python_version()
9+
function v = python_version(force)
10+
arguments
11+
force (1,1) logical = false
12+
end
1013

1114
v = [];
1215

13-
try
14-
pe = pyenv();
15-
vs = pe.Version;
16-
if strlength(vs) == 0, return, end
16+
if isMATLABReleaseOlderThan('R2022a') && ~force
17+
return
18+
end
1719

18-
vi = py.sys.version_info;
19-
v = [double(vi.major), double(vi.minor), double(vi.micro)];
20+
% we use a separate function because the JIT compiler in Matlab < R2022a
21+
% breaks try-catch for any py.* command
2022

21-
vv = strsplit(vs, '.');
22-
assert(str2double(vv{1}) == v(1), "stdlib:python_version:ValueError", "Python major version %s did not match pyenv %d", vv{1}, v(1))
23-
assert(str2double(vv{2}) == v(2), "stdlib:python_version:ValueError", "Python minor version %s did not match pyenv %d", vv{1}, v(1))
24-
catch e
25-
switch e.identifier
26-
case {'Octave:undefined-function', 'MATLAB:Python:PythonUnavailable'} % pass
27-
otherwise, rethrow(e)
28-
end
29-
end
23+
v = pvt_get_python_version();
3024

3125
end

0 commit comments

Comments
 (0)