From c7d6af7292191336c955e64f4f4d809f94967d7b Mon Sep 17 00:00:00 2001 From: Dag Sverre Seljebotn Date: Sat, 16 Aug 2008 01:04:44 +0200 Subject: [PATCH] Buffers: ndim option must now be given as keyword argument --- Cython/Compiler/Buffer.py | 3 ++- tests/compile/c_directives.pyx | 8 ++++---- tests/run/bufaccess.pyx | 26 +++++++++++++------------- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/Cython/Compiler/Buffer.py b/Cython/Compiler/Buffer.py index 9d322f25..0a92964f 100644 --- a/Cython/Compiler/Buffer.py +++ b/Cython/Compiler/Buffer.py @@ -115,6 +115,7 @@ class IntroduceBufferAuxiliaryVars(CythonTransform): # buffer_options = ("dtype", "ndim", "mode") # ordered! buffer_defaults = {"ndim": 1, "mode": "full"} +buffer_positional_options_count = 1 # anything beyond this needs keyword argument ERR_BUF_OPTION_UNKNOWN = '"%s" is not a buffer option' ERR_BUF_TOO_MANY = 'Too many buffer options' @@ -140,7 +141,7 @@ def analyse_buffer_options(globalpos, env, posargs, dictargs, defaults=None, nee posargs, dictargs = Interpreter.interpret_compiletime_options(posargs, dictargs, type_env=env) - if len(posargs) > len(buffer_options): + if len(posargs) > buffer_positional_options_count: raise CompileError(posargs[-1][1], ERR_BUF_TOO_MANY) options = {} diff --git a/tests/compile/c_directives.pyx b/tests/compile/c_directives.pyx index 322bd43f..ccb22d08 100644 --- a/tests/compile/c_directives.pyx +++ b/tests/compile/c_directives.pyx @@ -7,21 +7,21 @@ print 3 cimport python_dict as asadf, python_exc, cython as cy -def e(object[int, 2] buf): +def e(object[int, ndim=2] buf): print buf[3, 2] # no bc @cy.boundscheck(False) -def f(object[int, 2] buf): +def f(object[int, ndim=2] buf): print buf[3, 2] # no bc @cy.boundscheck(True) -def g(object[int, 2] buf): +def g(object[int, ndim=2] buf): # The below line should have no meaning # boundscheck = False # even if the above line doesn't follow indentation. print buf[3, 2] # bc -def h(object[int, 2] buf): +def h(object[int, ndim=2] buf): print buf[3, 2] # no bc with cy.boundscheck(True): print buf[3,2] # bc diff --git a/tests/run/bufaccess.pyx b/tests/run/bufaccess.pyx index c6d9159b..29ed6903 100644 --- a/tests/run/bufaccess.pyx +++ b/tests/run/bufaccess.pyx @@ -41,13 +41,13 @@ def nousage(): """ The challenge here is just compilation. """ - cdef object[int, 2] buf + cdef object[int, ndim=2] buf def printbuf(): """ Just compilation. """ - cdef object[int, 2] buf + cdef object[int, ndim=2] buf print buf @testcase @@ -334,7 +334,7 @@ def explicitly_release_buffer(): # Getting items and index bounds checking # @testcase -def get_int_2d(object[int, 2] buf, int i, int j): +def get_int_2d(object[int, ndim=2] buf, int i, int j): """ >>> get_int_2d(C, 1, 1) acquired C @@ -368,7 +368,7 @@ def get_int_2d(object[int, 2] buf, int i, int j): return buf[i, j] @testcase -def get_int_2d_uintindex(object[int, 2] buf, unsigned int i, unsigned int j): +def get_int_2d_uintindex(object[int, ndim=2] buf, unsigned int i, unsigned int j): """ Unsigned indexing: >>> get_int_2d_uintindex(C, 0, 0) @@ -385,7 +385,7 @@ def get_int_2d_uintindex(object[int, 2] buf, unsigned int i, unsigned int j): return buf[i, j] @testcase -def set_int_2d(object[int, 2] buf, int i, int j, int value): +def set_int_2d(object[int, ndim=2] buf, int i, int j, int value): """ Uses get_int_2d to read back the value afterwards. For pure unit test, one should support reading in MockBuffer instead. @@ -470,7 +470,7 @@ def fmtst2(object[int] buf): """ @testcase -def ndim1(object[int, 2] buf): +def ndim1(object[int, ndim=2] buf): """ >>> ndim1(IntMockBuffer("A", range(3))) Traceback (most recent call last): @@ -492,7 +492,7 @@ def readonly(obj): >>> [str(x) for x in R.recieved_flags] # Works in both py2 and py3 ['FORMAT', 'INDIRECT', 'ND', 'STRIDES'] """ - cdef object[unsigned short int, 3] buf = obj + cdef object[unsigned short int, ndim=3] buf = obj print buf[2, 2, 1] @testcase @@ -505,11 +505,11 @@ def writable(obj): >>> [str(x) for x in R.recieved_flags] # Py2/3 ['FORMAT', 'INDIRECT', 'ND', 'STRIDES', 'WRITABLE'] """ - cdef object[unsigned short int, 3] buf = obj + cdef object[unsigned short int, ndim=3] buf = obj buf[2, 2, 1] = 23 @testcase -def strided(object[int, 1, 'strided'] buf): +def strided(object[int, ndim=1, mode='strided'] buf): """ >>> A = IntMockBuffer("A", range(4)) >>> strided(A) @@ -648,7 +648,7 @@ def printbuf_int_2d(o, shape): released A """ # should make shape builtin - cdef object[int, 2] buf + cdef object[int, ndim=2] buf buf = o cdef int i, j for i in range(shape[0]): @@ -1076,10 +1076,10 @@ def typedbuffer1(obj): ... TypeError: Cannot convert int to bufaccess.IntMockBuffer """ - cdef IntMockBuffer[int, 1] buf = obj + cdef IntMockBuffer[int, ndim=1] buf = obj @testcase -def typedbuffer2(IntMockBuffer[int, 1] obj): +def typedbuffer2(IntMockBuffer[int, ndim=1] obj): """ >>> typedbuffer2(IntMockBuffer("A", range(10))) acquired A @@ -1096,7 +1096,7 @@ def typedbuffer2(IntMockBuffer[int, 1] obj): # Test __cythonbufferdefaults__ # @testcase -def bufdefaults1(IntStridedMockBuffer[int, 1] buf): +def bufdefaults1(IntStridedMockBuffer[int, ndim=1] buf): """ For IntStridedMockBuffer, mode should be "strided" by defaults which should show -- 2.26.2