diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index e8ccdd08d0566137ba77ed7540337deed6606ff4..68baf911190cd8a1a1384c0f689cc6087b3e710d 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -8,6 +8,7 @@ - Mike Miller - Kai Torben Ohlhus - Abhinav Tripathi +- Karthikeyan R --- diff --git a/NEWS.md b/NEWS.md index 1f9faf5bd344ad73525cfec12d940c6356884293..70b6fcdffa1e671cac04a6de1410cde0418c6e9d 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. diff --git a/inst/@py/subsref.m b/inst/@py/subsref.m index 9316b670a9ef3a8ca853bd696adc6b7fb002a284..b4c0b7e1ea88565c64c01c71e80d856b408ae887 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. diff --git a/inst/@pyobject/subsref.m b/inst/@pyobject/subsref.m index 5b5ae11798df32ef60c7c23f7610a7e5cb7f577d..a214ae3ca10be44d608a1c9331d74fea4803a031 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