Skip to content

Commit d915f15

Browse files
committed
python_version: cache for performance
This allows allows force_old to be cached
1 parent a6e4845 commit d915f15

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

+stdlib/python_version.m

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
11
%% PYTHON_VERSION get the Python version used by MATLAB
22
%
3-
%## Output
4-
%* 1x3 vector of major, minor, micro version e.g. Python 3.14.2 = [3, 14, 2]
3+
% uses persistent variable to cache the Python version
4+
%
5+
%%% Inputs
6+
% * force_old: (optional) boolean flag to force checking of Python on Matlab < R2022a
7+
%
8+
%%% Output
9+
% * v: 1x3 vector of major, minor, micro version e.g. Python 3.14.2 = [3, 14, 2]
10+
%
511
% we need to do at least one Python function call to handle cases
612
% where the environment has changed since pyenv() was set. For example
713
% HPC with "module load python3..."
814

9-
function v = python_version(force)
15+
function v = python_version(force_old)
1016
arguments
11-
force (1,1) logical = false
17+
force_old (1,1) logical = false
18+
end
19+
20+
persistent v_
21+
22+
if ~isempty(v_)
23+
v = v_;
24+
return
1225
end
1326

1427
v = [];
1528

16-
if isMATLABReleaseOlderThan('R2022a') && ~force
29+
% For MATLAB versions older than R2022a, skip Python version check unless force_old is true
30+
if isMATLABReleaseOlderThan('R2022a') && ~force_old
1731
return
1832
end
1933

@@ -22,4 +36,9 @@
2236

2337
v = pvt_get_python_version();
2438

39+
% cache the result
40+
if ~isempty(v)
41+
v_ = v;
42+
end
43+
2544
end

0 commit comments

Comments
 (0)