From: Dag Sverre Seljebotn Date: Wed, 6 May 2009 18:26:30 +0000 (+0200) Subject: wraparound directive implemented X-Git-Tag: 0.11.2.rc1~10^2~32 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=65364b1bb8f3d49095e1de2ce9a2c4ed90507343;p=cython.git wraparound directive implemented --- diff --git a/Cython/Compiler/Buffer.py b/Cython/Compiler/Buffer.py index 7724fade..df019bee 100644 --- a/Cython/Compiler/Buffer.py +++ b/Cython/Compiler/Buffer.py @@ -330,7 +330,7 @@ def put_assign_to_buffer(lhs_cname, rhs_cname, buffer_aux, buffer_type, code.putln("}") # Release stack -def put_buffer_lookup_code(entry, index_signeds, index_cnames, options, pos, code): +def put_buffer_lookup_code(entry, index_signeds, index_cnames, directives, pos, code): """ Generates code to process indices and calculate an offset into a buffer. Returns a C string which gives a pointer which can be @@ -345,9 +345,9 @@ def put_buffer_lookup_code(entry, index_signeds, index_cnames, options, pos, cod """ bufaux = entry.buffer_aux bufstruct = bufaux.buffer_info_var.cname - negative_indices = entry.type.negative_indices + negative_indices = directives['wraparound'] and entry.type.negative_indices - if options['boundscheck']: + if directives['boundscheck']: # Check bounds and fix negative indices. # We allocate a temporary which is initialized to -1, meaning OK (!). # If an error occurs, the temp is set to the dimension index the diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 5b617224..99c447fb 100644 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -1958,7 +1958,7 @@ class IndexNode(ExprNode): return Buffer.put_buffer_lookup_code(entry=self.base.entry, index_signeds=[i.type.signed for i in self.indices], index_cnames=index_temps, - options=code.globalstate.directives, + directives=code.globalstate.directives, pos=self.pos, code=code) def put_nonecheck(self, code): diff --git a/Cython/Compiler/Options.py b/Cython/Compiler/Options.py index 06f70244..faaec3cd 100644 --- a/Cython/Compiler/Options.py +++ b/Cython/Compiler/Options.py @@ -54,7 +54,7 @@ c_line_in_traceback = 1 embed = False -# Declare pragmas +# Declare compiler directives option_defaults = { 'boundscheck' : True, 'nonecheck' : False, @@ -64,6 +64,7 @@ option_defaults = { 'cdivision': True, # Will be False in 0.12 'cdivision_warnings': False, 'always_allow_keywords': False, + 'wraparound' : True } # Override types possibilities above, if needed diff --git a/tests/run/bufaccess.pyx b/tests/run/bufaccess.pyx index eb4c10ba..db65dcac 100644 --- a/tests/run/bufaccess.pyx +++ b/tests/run/bufaccess.pyx @@ -478,6 +478,26 @@ def no_negative_indices(object[int, negative_indices=False] buf, int idx): """ return buf[idx] +@testcase +@cython.wraparound(False) +def wraparound_directive(object[int] buf, int pos_idx, int neg_idx): + """ + Again, the most interesting thing here is to inspect the C source. + + >>> A = IntMockBuffer(None, range(4)) + >>> wraparound_directive(A, 2, -1) + 5 + >>> wraparound_directive(A, -1, 2) + Traceback (most recent call last): + ... + IndexError: Out of bounds on buffer access (axis 0) + """ + cdef int byneg + with cython.wraparound(True): + byneg = buf[neg_idx] + return buf[pos_idx] + byneg + + # # Test which flags are passed. #