TestMemView.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from Cython.TestUtils import CythonTest
  2. import Cython.Compiler.Errors as Errors
  3. from Cython.Compiler.Nodes import *
  4. from Cython.Compiler.ParseTreeTransforms import *
  5. from Cython.Compiler.Buffer import *
  6. class TestMemviewParsing(CythonTest):
  7. def parse(self, s):
  8. return self.should_not_fail(lambda: self.fragment(s)).root
  9. def not_parseable(self, expected_error, s):
  10. e = self.should_fail(lambda: self.fragment(s), Errors.CompileError)
  11. self.assertEqual(expected_error, e.message_only)
  12. def test_default_1dim(self):
  13. self.parse(u"cdef int[:] x")
  14. self.parse(u"cdef short int[:] x")
  15. def test_default_ndim(self):
  16. self.parse(u"cdef int[:,:,:,:,:] x")
  17. self.parse(u"cdef unsigned long int[:,:,:,:,:] x")
  18. self.parse(u"cdef unsigned int[:,:,:,:,:] x")
  19. def test_zero_offset(self):
  20. self.parse(u"cdef long double[0:] x")
  21. self.parse(u"cdef int[0:] x")
  22. def test_zero_offset_ndim(self):
  23. self.parse(u"cdef int[0:,0:,0:,0:] x")
  24. def test_def_arg(self):
  25. self.parse(u"def foo(int[:,:] x): pass")
  26. def test_cdef_arg(self):
  27. self.parse(u"cdef foo(int[:,:] x): pass")
  28. def test_general_slice(self):
  29. self.parse(u'cdef float[::ptr, ::direct & contig, 0::full & strided] x')
  30. def test_non_slice_memview(self):
  31. self.not_parseable(u"An axis specification in memoryview declaration does not have a ':'.",
  32. u"cdef double[:foo, bar] x")
  33. self.not_parseable(u"An axis specification in memoryview declaration does not have a ':'.",
  34. u"cdef double[0:foo, bar] x")
  35. def test_basic(self):
  36. t = self.parse(u"cdef int[:] x")
  37. memv_node = t.stats[0].base_type
  38. self.assert_(isinstance(memv_node, MemoryViewSliceTypeNode))
  39. # we also test other similar declarations (buffers, anonymous C arrays)
  40. # since the parsing has to distinguish between them.
  41. def disable_test_no_buf_arg(self): # TODO
  42. self.not_parseable(u"Expected ']'",
  43. u"cdef extern foo(object[int, ndim=2])")
  44. def disable_test_parse_sizeof(self): # TODO
  45. self.parse(u"sizeof(int[NN])")
  46. self.parse(u"sizeof(int[])")
  47. self.parse(u"sizeof(int[][NN])")
  48. self.not_parseable(u"Expected an identifier or literal",
  49. u"sizeof(int[:NN])")
  50. self.not_parseable(u"Expected ']'",
  51. u"sizeof(foo[dtype=bar]")
  52. if __name__ == '__main__':
  53. import unittest
  54. unittest.main()