Skip to content

Commit ad7b909

Browse files
committed
add try-catch and warning feedback
1 parent efbb1df commit ad7b909

File tree

12 files changed

+73
-25
lines changed

12 files changed

+73
-25
lines changed

+stdlib/+python/create_symlink.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
py.os.symlink(target, link);
55
ok = true;
66
catch e
7-
warning(e.identifier, "%s", e.message)
7+
warning(e.identifier, "create_symlink(%s, %s) failed: %s", target, link, e.message);
88
ok = false;
99
end
1010

+stdlib/+python/device.m

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
function i = device(p)
22

3-
i = uint64(int64(py.os.stat(p).st_dev));
4-
% int64 first is for Matlab <= R2022a
3+
try
4+
i = uint64(int64(py.os.stat(p).st_dev));
5+
% int64 first is for Matlab <= R2022a
6+
catch e
7+
warning(e.identifier, "device(%s) failed: %s", p, e.message)
8+
i = [];
9+
end
510

611
end

+stdlib/+python/disk_available.m

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
function f = disk_available(d)
22

3-
di = py.shutil.disk_usage(d);
4-
5-
f = int64(di.free);
6-
% int64 first is for Matlab <= R2022a
3+
try
4+
di = py.shutil.disk_usage(d);
5+
f = int64(di.free);
6+
% int64 first is for Matlab <= R2022a
7+
catch e
8+
warning(e.identifier, "disk_available(%s) failed: %s", d, e.message);
9+
f = [];
10+
end
711

812
end

+stdlib/+python/disk_capacity.m

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
function f = disk_capacity(d)
22

3-
di = py.shutil.disk_usage(d);
4-
5-
f = int64(di.total);
6-
% int64 first is for Matlab <= R2022a
3+
try
4+
di = py.shutil.disk_usage(d);
5+
f = int64(di.total);
6+
% int64 first is for Matlab <= R2022a
7+
catch e
8+
warning(e.identifier, "disk_capacity(%s) failed: %s", d, e.message);
9+
f = [];
10+
end
711

812
end

+stdlib/+python/get_hostname.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
function n = get_hostname()
22

3-
n = py.socket.gethostname();
4-
3+
try
4+
n = char(py.socket.gethostname());
5+
catch e
6+
warning(e.identifier, "get_hostname failed: %s", e.message);
7+
n = '';
58
end

+stdlib/+python/is_char_device.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function y = is_char_device(p)
2+
3+
try
4+
y = py.pathlib.Path(p).is_char_device();
5+
catch e
6+
warning(e.identifier, "Python is_char_device failed: %s", e.message);
7+
y = false;
8+
end

+stdlib/+python/is_symlink.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
function y = is_symlink(p)
22

3-
y = py.pathlib.Path(p).is_symlink();
3+
try
4+
y = py.pathlib.Path(p).is_symlink();
5+
catch e
6+
warning(e.identifier, "Python is_symlink failed: %s", e.message);
7+
y = false;
8+
end
49

510
end

+stdlib/+python/read_symlink.m

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
function r = read_symlink(p)
22

33
% https://docs.python.org/3/library/pathlib.html#pathlib.Path.readlink
4-
r = string(py.os.readlink(p));
5-
if ispc() && startsWith(r, '\\?\')
6-
r = extractAfter(r, '\\?\');
4+
try
5+
r = string(py.os.readlink(p));
6+
if ispc() && startsWith(r, '\\?\')
7+
r = extractAfter(r, '\\?\');
8+
end
9+
catch e
10+
warning(e.identifier, "read_symlink(%s) failed: %s", p, e.message);
11+
r = string.empty;
712
end
813

914
end

+stdlib/+python/set_modtime.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
py.os.utime(p, py.tuple([s.st_atime, utc]));
66
ok = true;
77
catch e
8-
warning(e.identifier, "%s", e.message)
8+
warning(e.identifier, "set_modtime(%s, %s) failed: %s", p, utc, e.message);
99
ok = false;
1010
end
1111

+stdlib/+sys/read_symlink.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function r = read_symlink(p)
2+
3+
r = string.empty;
4+
5+
if isunix()
6+
cmd = sprintf('readlink -fn %s', p);
7+
else
8+
cmd = sprintf('pwsh -command "(Get-Item -Path %s).Target"', p);
9+
end
10+
11+
[s, m] = system(cmd);
12+
if s == 0
13+
r = strip(string(m));
14+
end
15+
16+
end

0 commit comments

Comments
 (0)