Skip to content

Commit a677739

Browse files
committed
has_python(enable) parameter
This is one-point of truth/action for clarity
1 parent 97ea983 commit a677739

File tree

6 files changed

+25
-43
lines changed

6 files changed

+25
-43
lines changed

+stdlib/+python/has_psutil.m

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
%% PYTHON.HAS_PSUTIL is Python psutil module availble
22

3-
function y = has_psutil(force_old)
4-
if nargin < 1
5-
force_old = false;
6-
end
7-
8-
y = false;
3+
function y = has_psutil()
94

10-
% For MATLAB versions older than R2022a, skip Python version check unless force_old is true
11-
if stdlib.matlabOlderThan('R2022a') && ~force_old
12-
return
5+
try
6+
py.psutil.version_info();
7+
y = true;
8+
catch
9+
y = false;
1310
end
1411

15-
y = pvt_psutil();
16-
% this is to avoid Matlab < R2022a JIT but with Python in general
17-
1812
end

+stdlib/+python/private/pvt_psutil.m

Lines changed: 0 additions & 10 deletions
This file was deleted.

+stdlib/Backend.m

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,20 @@
109109

110110
switch functionName
111111
case {'filesystem_type', 'is_removable', 'ram_total', 'ram_free', 'uptime'}
112-
if ~stdlib.python.has_psutil(); continue, end
112+
if ~stdlib.has_python() || ~stdlib.python.has_psutil()
113+
continue
114+
end
113115
case {'cpu_load', 'get_owner', 'get_process_priority', 'get_uid'}
114116
if ispc(), continue, end
115117
case 'is_admin'
116118
if ispc() && stdlib.matlabOlderThan('R2024a'), continue, end
117119
case 'is_dev_drive'
118-
pyv = stdlib.python_version();
119-
if any(pyv(1:2) < [3, 12]), continue, end
120+
if stdlib.has_python()
121+
pyv = stdlib.python_version();
122+
if isempty(pyv) || any(pyv(1:2) < [3, 12])
123+
continue
124+
end
125+
end
120126
end
121127
case 'native'
122128

+stdlib/has_python.m

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22
%
33
% https://www.mathworks.com/support/requirements/python-compatibility.html
44

5-
function y = has_python()
5+
function y = has_python(enable_check)
66

7-
try %#ok<TRYNC>
8-
r = matlabRelease();
9-
if ispc && r.Release == "R2026a" && r.Stage == "prerelease"
10-
% avoid python due to bug
11-
y = false;
12-
return
13-
end
7+
y = false;
8+
9+
if nargin == 0
10+
enable_check = ~stdlib.matlabOlderThan('R2022a');
1411
end
1512

16-
y = ~isempty(stdlib.python_version());
13+
if enable_check
14+
y = ~isempty(stdlib.python_version());
15+
end
1716

1817
end
1918

+stdlib/matlabOlderThan.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
%% MATLABOLDERTHAN compare Matlab release name only e.g. R2025a
22
% works for Matlab >= R2016b
33
%
4-
% our simpler checks is about 200x faster than isMATLABReleaseOlderThan(release)
4+
% our implementation is about 200x faster than isMATLABReleaseOlderThan(release)
55

66
function isOlder = matlabOlderThan(release)
77
assert(nargin == 1, 'Specify Matlab release like ''R2025a''')

+stdlib/python_version.m

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
% where the environment has changed since pyenv() was set. For example
1515
% HPC with 'module load python3...'
1616

17-
function [v, msg] = python_version(force_old)
18-
if nargin < 1
19-
force_old = false;
20-
end
17+
function [v, msg] = python_version()
2118

2219
persistent stdlib_py_version pyv_cached
2320

@@ -36,10 +33,6 @@
3633

3734
v = [];
3835

39-
if stdlib.matlabOlderThan('R2022a') && ~force_old
40-
return
41-
end
42-
4336
% glitchy Python load can error on sys.version_info
4437
% if pyenv() hasn't ever been configured, may get uncatchable error
4538
% bad lexical cast: source type value could not be interpreted as target

0 commit comments

Comments
 (0)