提交 99079db1 authored 作者: nouiz's avatar nouiz

Merge pull request #1285 from lamblin/long_indexing

Allow Python long integers in indexing.
......@@ -4457,6 +4457,12 @@ class Subtensor(Op):
# See <http://projects.scipy.org/numpy/ticket/2235>.
elif isinstance(entry, (numpy.integer, int)):
return entry
# On Windows 64-bit, shapes are returned as Python long, as they can
# be bigger than what a Python int can hold.
# Shapes should always fit in a numpy.int64, and we support them better
elif isinstance(entry, long):
entry64 = numpy.int64(entry)
return entry64
else:
raise AdvancedIndexingError(Subtensor.e_indextype, entry)
......
......@@ -2998,6 +2998,20 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
self.assertTrue(tval.shape == ())
self.assertTrue(numpy.all(tval == 0))
def test_long(self):
n = self.shared(numpy.arange(12, dtype=self.dtype).reshape((4, 3)))
t = n[1L:4L:2L, 1L]
self.assertTrue(isinstance(t.owner.op, Subtensor))
tval = self.eval_output_and_check(t)
self.assertTrue(tval.shape == (2,))
self.assertTrue(numpy.all(tval == [4, 10]))
def test_long_too_big(self):
# Currently, we cast Python longs to int64 when used for indexing.
# This test checks that using a long that does not fit raises an error.
n = self.shared(numpy.arange(12, dtype=self.dtype).reshape((4, 3)))
self.assertRaises(Exception, lambda: n[:(2L ** 63)])
def test_newaxis(self):
"""
newaxis support comes from logic in the __getitem__ of TensorType
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论