Skip to content

Commit 3fc87eb

Browse files
committed
python: workaround private functions
these allow use with Matlab < R2022a where Python isn't available or isn't working due to Matlab JIT bugs in < R2022a
1 parent d915f15 commit 3fc87eb

16 files changed

+81
-38
lines changed

+stdlib/create_symlink.m

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,7 @@
2727
% see example/Filesystem.java for this working in plain Java.
2828
% see example/javaCreateSymbolicLink.m for a non-working attempt in Matlab.
2929
if stdlib.has_python()
30-
try
31-
py.os.symlink(target, link);
32-
ok = true;
33-
catch e
34-
warning(e.identifier, "%s", e.message)
35-
end
30+
ok = py_create_symlink(target, link);
3631
elseif stdlib.dotnet_api() >= 6
3732
% https://learn.microsoft.com/en-us/dotnet/api/system.io.file.createsymboliclink
3833
try

+stdlib/disk_available.m

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@
1313
if ~stdlib.exists(d), return, end
1414

1515
if stdlib.has_python()
16-
di = py.shutil.disk_usage(d);
17-
f = uint64(int64(di.free)); % int64 first is for Matlab <= R2022a
16+
f = py_disk_available(d);
1817
elseif stdlib.has_dotnet()
1918
f = System.IO.DriveInfo(stdlib.absolute(d)).AvailableFreeSpace();
2019
% https://learn.microsoft.com/en-us/dotnet/api/system.io.driveinfo.availablefreespace
2120
elseif stdlib.has_java()
2221
f = javaObject("java.io.File", d).getUsableSpace();
23-
f = uint64(f);
22+
end
23+
24+
f = uint64(f);
25+
2426
end
2527

2628
%!assert (disk_available('.') > 0)

+stdlib/disk_capacity.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
if ~stdlib.exists(d), return, end
1212

1313
if stdlib.has_python()
14-
di = py.shutil.disk_usage(d);
15-
f = uint64(int64(di.total)); % int64 first is for Matlab <= R2022a
14+
f = py_disk_capacity(d);
1615
elseif stdlib.has_dotnet()
1716
f = System.IO.DriveInfo(stdlib.absolute(d)).TotalSize();
1817
% https://learn.microsoft.com/en-us/dotnet/api/system.io.driveinfo.totalsize
1918
elseif stdlib.has_java()
2019
f = javaObject("java.io.File", d).getTotalSpace();
21-
f = uint64(f);
2220
end
2321

22+
f = uint64(f);
23+
2424
end
2525

2626
%!assert (disk_capacity('.') > 0)

+stdlib/get_owner.m

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212

1313

1414
if ~ispc() && stdlib.has_python()
15-
16-
n = string(py.str(py.pathlib.Path(p).owner()));
17-
15+
n = py_get_owner(p);
1816
elseif stdlib.has_java()
1917
% https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/nio/file/Files.html#getOwner(java.nio.file.Path,java.nio.file.LinkOption...)
2018
% https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/nio/file/LinkOption.html

+stdlib/hard_link_count.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
c = [];
1414

1515
if stdlib.has_python()
16-
c = uint64(int64(py.os.stat(p).st_nlink)); % int64 first is for Matlab <= R2022a
16+
c = py_hard_link_count(p);
1717
elseif stdlib.isoctave()
1818
[s, err] = stat(p);
1919
if err == 0

+stdlib/is_admin.m

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,9 @@
33
function y = is_admin()
44

55

6-
if stdlib.has_python()
7-
if isunix()
8-
y = py.os.getuid() == 0;
9-
return
10-
elseif ~isMATLABReleaseOlderThan('R2024a')
11-
shell32 = py.ctypes.WinDLL('shell32');
12-
13-
% this is a key step vs. simply py.ctypes.windll.shell32.IsUserAnAdmin()
14-
f = py.getattr(shell32, 'IsUserAnAdmin');
15-
16-
y = logical(f());
17-
return
18-
end
19-
end
20-
21-
if stdlib.isoctave()
6+
if (isunix() || ~isMATLABReleaseOlderThan('R2024a')) && stdlib.has_python()
7+
y = py_is_admin();
8+
elseif stdlib.isoctave()
229
y = getuid() == 0;
2310
elseif ispc() && stdlib.has_dotnet()
2411
% com.sun.security.auth.module.NTSystem().getGroupIDs();

+stdlib/is_symlink.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
elseif stdlib.has_java()
1616
ok = java.nio.file.Files.isSymbolicLink(javaPathObject(stdlib.absolute(p)));
1717
elseif stdlib.has_python()
18-
ok = py.pathlib.Path(p).is_symlink();
18+
ok = py_is_symlink(p);
1919
else
2020
ok = logical.empty;
2121
end

+stdlib/private/py_create_symlink.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function ok = py_create_symlink(target, link)
2+
3+
try
4+
py.os.symlink(target, link);
5+
ok = true;
6+
catch e
7+
warning(e.identifier, "%s", e.message)
8+
ok = false;
9+
end
10+
11+
end

+stdlib/private/py_disk_available.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function f = py_disk_available(d)
2+
3+
di = py.shutil.disk_usage(d);
4+
5+
f = int64(di.free);
6+
% int64 first is for Matlab <= R2022a
7+
8+
end

+stdlib/private/py_disk_capacity.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function f = py_disk_capacity(d)
2+
3+
di = py.shutil.disk_usage(d);
4+
5+
f = int64(di.total);
6+
% int64 first is for Matlab <= R2022a
7+
8+
end

0 commit comments

Comments
 (0)