changeset 267:c7a7d94a142f

fix off-by-one dict indexing error for assignment * @pyobject/subsasgn.m: Whitelist for subtracting 1 from indices.
author Colin Macdonald <cbm@m.fsf.org>
date Thu, 28 Jul 2016 00:15:19 -0700
parents b337b733d6f8
children 15666fdcbb45
files @pyobject/subsasgn.m
diffstat 1 files changed, 8 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/@pyobject/subsasgn.m	Thu Jul 28 00:12:03 2016 -0700
+++ b/@pyobject/subsasgn.m	Thu Jul 28 00:15:19 2016 -0700
@@ -39,9 +39,15 @@
     case "{}"
       ## XXX: doesn't support slices or anything like that yet
 
+      ## Subtract one from index: do this for lists, numpy arrays, etc
+      pyexec ("import collections")
+      pyexec ("import numpy")
+      x_is_list = pycall (pyeval (
+        "lambda (x): isinstance(x, (collections.Sequence, numpy.ndarray))"),
+        x);
       for i = 1:length (idx.subs)
         j = idx.subs{i};
-        if (isindex (j) && isnumeric (j))
+        if (x_is_list && isindex (j) && isnumeric (j))
           idx.subs{i} = cast (j, class (sizemax ())) - 1;
         endif
       endfor
@@ -95,7 +101,7 @@
 %! d{"a"} = 3;
 %! assert (d{"a"}, 3)
 
-%!xtest
+%!test
 %! % dict assignment, other keys (e.g., Issue #10).
 %! pyexec ("d = dict()")
 %! d = pyobject.fromPythonVarName ("d");