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
"""
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
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):
embed = False
-# Declare pragmas
+# Declare compiler directives
option_defaults = {
'boundscheck' : True,
'nonecheck' : False,
'cdivision': True, # Will be False in 0.12
'cdivision_warnings': False,
'always_allow_keywords': False,
+ 'wraparound' : True
}
# Override types possibilities above, if needed
"""
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.
#