Indeed, this is a subtle issue
Cython thinks that F is a sequence because Family does support the
sequence protocol (this is lost somewhere in the macro
__Pyx_GetItemInt). As a consequence, Cython translates a negative index
"-i" into "len(sequence) - i". Which is indeed wrong in the above
case... Cython should have called PyObject_GetItem.
sage: F = Family({-1:0,-2:0,-3:0})
sage: foo(-1)
sage: foo(F, -1)
KeyError: 2
Though, I am not sure who is to blame.
Vincent
On 04/08/2017 10:31, Vincent Delecroix wrote:
On 04/08/2017 10:27, Nils Bruin wrote:
On Thursday, August 3, 2017 at 7:58:50 PM UTC+2, Travis Scrimshaw wrote:
Hey all,
I think I have come across a possible Cython bug, but I am not
sure. I
am using 8.1beta0. Here is as small of a working example as I can
currently
get.
sage: %%cython
....: def foo(F, int i):
....: return F[i]
....: def foo2(F, int i):
....: return F._dictionary[i]
....:
sage: F = Family({-1:0})
sage: foo2(F, int(-1)) # This works
0
sage: foo(F, int(-1))
KeyError: 0
sage: F._dictionary
{-1: 0}
sage: foo(F, int(2)) # Expected failure, but the input matches
---------------------------------------------------------------------------
KeyError Traceback (most recent call
last
)
...
KeyError: 2
It seems to have to do with a __getitem__. I do not understand what
precisely the situation is that makes this fail; when I try to go to
smaller examples, it works.
Indication of what might be happening: with your example upstairs I get
sage: foo(F,-2)
0
sage: foo(F,-3)
KeyError: -2
sage: foo(F,3)
KeyError: 3
sage: foo(F,2**31)
OverflowError: value too large to convert to int
sage: foo(F,2**31-1)
KeyError: 2147483647
sage: foo(F,-2**31)
KeyError: -2147483647
so it looks like with "foo" something goes wrong with the conversion of
signed ints. The boundary cases perhaps give an indication where this
goes
wrong. I'd think this is indeed a bug.
Note that it is perfectly fine with dictionaries
sage: d = {-1: 0}
sage: foo(d, -1)
0
sage: foo(d, -2)
KeyError: -2
sage: foo(d, 3)
KeyError: 3
and I confirm
sage: F = Family(d)
sage: foo(F, -2) # a nice workaround ;-)
0
sage: foo(F, -3)
KeyError: -2
--
You received this message because you are subscribed to the Google Groups
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.