comparison @pyobject/subsref.m @ 288:f5282c87e5d5

Fix cell indexing for pyobject in subsref * @pyobject/subsref.m: Allow indexing pyobjects with cell type indexing with ranges and {:}
author Abhinav Tripathi <genuinelucifer@gmail.com>
date Wed, 03 Aug 2016 00:18:40 -0700
parents 24faaa3cf5e5
children f2b4576879a6
comparison
equal deleted inserted replaced
286:9e8faa439d1d 288:f5282c87e5d5
55 if (isindex (j) && isnumeric (j) && x_is_list) 55 if (isindex (j) && isnumeric (j) && x_is_list)
56 t.subs{i} = cast (j, class (sizemax ())) - 1; 56 t.subs{i} = cast (j, class (sizemax ())) - 1;
57 endif 57 endif
58 endfor 58 endfor
59 59
60 if (isscalar (t.subs)) 60 if (ischar (t.subs{1}) && strcmp (t.subs{1}, ":"))
61 is_map = pyeval ("lambda x: isinstance(x, collections.Mapping)");
62 if (pycall (is_map, x))
63 ind = ":";
64 else
65 ind = int32 ([1:length(x)] - 1);
66 endif
67 elseif (isscalar (t.subs))
61 ind = t.subs{1}; 68 ind = t.subs{1};
62 else 69 else
63 ## XXX: after #26, #27, I think its just: 70 ## XXX: after #26, #27, I think its just:
64 #ind = pycall ("tuple", t.subs); 71 #ind = pycall ("tuple", t.subs);
65 pyexec (["global _temp\n" ... 72 pyexec (["global _temp\n" ...
68 " _temp = x"]); 75 " _temp = x"]);
69 pycall ("pystoretemp", t.subs); 76 pycall ("pystoretemp", t.subs);
70 pyexec ("_temp = tuple(_temp[0])"); 77 pyexec ("_temp = tuple(_temp[0])");
71 ind = pyobject.fromPythonVarName ("_temp"); 78 ind = pyobject.fromPythonVarName ("_temp");
72 endif 79 endif
80
73 gi = pycall ("getattr", x, "__getitem__"); # x.__getitem__ 81 gi = pycall ("getattr", x, "__getitem__"); # x.__getitem__
74 r = pycall (gi, ind); 82 if (isnumeric (ind) && length (ind) > 1)
83 r = {};
84 for k = 1:length (ind)
85 r(end+1) = pycall (gi, ind(k));
86 endfor
87 else
88 r = pycall (gi, ind);
89 endif
75 90
76 otherwise 91 otherwise
77 t 92 t
78 error ("@pyobject/subsref: not implemented") 93 error ("@pyobject/subsref: not implemented")
79 endswitch 94 endswitch
90 elseif (nargout == 1) 105 elseif (nargout == 1)
91 varargout{1} = r; 106 varargout{1} = r;
92 elseif (nargout >= 2) 107 elseif (nargout >= 2)
93 assert (length (r) == nargout, ... 108 assert (length (r) == nargout, ...
94 "pyobject/subsref: number of outputs must match") 109 "pyobject/subsref: number of outputs must match")
95 [varargout{1:nargout}] = r{1:nargout}; 110 [varargout{1:nargout}] = subsref (r, struct ("type", "{}", "subs", {{1:nargout}}));
96 endif 111 endif
97 endfunction 112 endfunction
98 113
99 114
100 %!test 115 %!test