Skip to content

Commit fa84448

Browse files
committed
has_python, python_version much more robuse
1 parent 55431f5 commit fa84448

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

+stdlib/has_python.m

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44

55
function y = has_python()
66

7-
try
8-
pe = pyenv();
9-
y = strlength(pe.Version) > 0;
10-
catch
11-
y = false;
12-
end
7+
y = ~isempty(stdlib.python_version);
138

149
end

+stdlib/python_version.m

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
%% PYTHON_VERSION get the Python version used by MATLAB
2+
%
3+
%## Output
4+
%* 1x3 vector of major, minor, micro version e.g. Python 3.14.2 = [3, 14, 2]
5+
% we need to do at least one Python function call to handle cases
6+
% where the environment has changed since pyenv() was set. For example
7+
% HPC with "module load python3..."
28

39
function v = python_version()
410

11+
v = [];
12+
513
try
614
pe = pyenv();
7-
v = pe.Version;
8-
catch
9-
v = "";
15+
vs = pe.Version;
16+
vi = py.sys.version_info;
17+
v = [double(vi.major), double(vi.minor), double(vi.micro)];
18+
19+
vv = strsplit(vs, '.');
20+
assert(str2double(vv{1}) == v(1), "stdlib:python_version:ValueError", "Python major version %s did not match pyenv %d", vv{1}, v(1))
21+
assert(str2double(vv{2}) == v(2), "stdlib:python_version:ValueError", "Python minor version %s did not match pyenv %d", vv{1}, v(1))
22+
catch e
23+
switch e.identifier
24+
case {'Octave:undefined-function', 'MATLAB:Python:PythonUnavailable'} % pass
25+
otherwise, rethrow(e)
26+
end
1027
end
1128

1229
end

test/TestSys.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function test_dotnet_version(tc)
2626
function test_has_python(tc)
2727
tc.assumeTrue(stdlib.has_python())
2828
v = stdlib.python_version();
29-
tc.verifyTrue(stdlib.version_atleast(v, "3.8"), "expected Python >= 3.8")
29+
tc.verifyTrue(all(v >= [3, 8, 0]), "expected Python >= 3.8")
3030
end
3131

3232
function test_os_version(tc)

test/test_nomex.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
function test_nomex()
2-
import matlab.unittest.TestSuite
32
import matlab.unittest.selectors.HasTag
43

54
cwd = fileparts(mfilename('fullpath'));
65

76
root = fileparts(cwd);
87
addpath(root)
98

10-
suite = TestSuite.fromFolder(cwd);
9+
if isMATLABReleaseOlderThan('R2022b')
10+
suite = testsuite(cwd);
11+
else
12+
suite = testsuite(cwd, InvalidFileFoundAction="error");
13+
end
1114

1215
sel = ~HasTag("exe") & ~HasTag("mex");
1316

0 commit comments

Comments
 (0)