From 1dd318f61024a68801df6d785c11c4d40003a6b1 Mon Sep 17 00:00:00 2001 From: Karthikeyan564 Date: Wed, 31 Mar 2021 19:16:33 +0530 Subject: [PATCH 1/3] Add nested modules support --- CONTRIBUTORS.md | 1 + inst/@py/subsref.m | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index e8ccdd0..68baf91 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -8,6 +8,7 @@ - Mike Miller - Kai Torben Ohlhus - Abhinav Tripathi +- Karthikeyan R --- diff --git a/inst/@py/subsref.m b/inst/@py/subsref.m index 9316b67..b4c0b7e 100644 --- a/inst/@py/subsref.m +++ b/inst/@py/subsref.m @@ -43,20 +43,29 @@ function varargout = subsref (x, idx) error ("py: invalid indexing type"); endif + used_idx = 0; + if (type == "." && ((numel (idx) == 1) || (idx(2).type != "."))) try + used_idx =1; y = pyeval (subs); catch y = pycall ("__import__", subs); end_try_catch else - y = pycall ("__import__", subs); + for i = length(idx):-1:1 + try + y = pycall ("__import__", pyargs ("name",strjoin({idx(1:i).subs},"."),"fromlist","['']")); + used_idx = i; + break; + end + endfor endif - - if (numel (idx) > 1) - y = subsref (y, idx(2:end)); + + if (numel (idx) - used_idx > 0) + y = subsref (y, idx(used_idx + 1:end)); endif - + ## If the *last* indexing operation is ".name", and the object returned ## is a Python callable, then call it with no arguments to be compatible ## with how Octave functions are evaluated. -- GitLab From e2b77f46962b1a3807c4e738dc378cc1fd4803b1 Mon Sep 17 00:00:00 2001 From: Karthikeyan564 Date: Sun, 4 Apr 2021 03:28:57 +0530 Subject: [PATCH 2/3] tests: Add tests for nested module support --- inst/@pyobject/subsref.m | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/inst/@pyobject/subsref.m b/inst/@pyobject/subsref.m index 5b5ae11..a214ae3 100644 --- a/inst/@pyobject/subsref.m +++ b/inst/@pyobject/subsref.m @@ -292,3 +292,14 @@ endfunction %! % multiple return values: not enough outputs %! f = pyeval ("lambda: (1, 2, 3)"); %! [a, b] = f (); + +%!test +%! % support nested modules +%! a = py.curses.ascii.isblank('4'); +%! assert (a, false) + +%!test +%! % support nested modules (Chain) +%! dom3 = py.xml.dom.minidom.parseString("

data

"); +%! f = dom3.documentElement.tagName; +%! assert (char(f), 'p') \ No newline at end of file -- GitLab From ba5d35239c7d55b37945d518e6668bbb3e723393 Mon Sep 17 00:00:00 2001 From: Karthikeyan564 Date: Sun, 4 Apr 2021 03:55:17 +0530 Subject: [PATCH 3/3] Update NEWS.md --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 1f9faf5..70b6fcd 100644 --- a/NEWS.md +++ b/NEWS.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - New command `pythonic` to get information about the package. +- Support for nested modules. ### Changed - Use system Python 3 interpreter by default. -- GitLab