merge
authorDag Sverre Seljebotn <dagss@student.matnat.uio.no>
Thu, 25 Sep 2008 09:40:17 +0000 (11:40 +0200)
committerDag Sverre Seljebotn <dagss@student.matnat.uio.no>
Thu, 25 Sep 2008 09:40:17 +0000 (11:40 +0200)
1  2 
Cython/Compiler/Buffer.py
Cython/Compiler/Code.py
Cython/Compiler/ExprNodes.py
Cython/Compiler/PyrexTypes.py
Cython/Includes/numpy.pxd
tests/run/bufaccess.pyx
tests/run/numpy_test.pyx

index 10c7e086cd7dbf4eda5fb7c7442a106632299765,aa6d09b02e57e1710499b11fbfc8085cf94015cf..9676756944d09c69230c7fc5c913a97c30e86dd9
@@@ -381,14 -415,16 +415,16 @@@ def put_buffer_lookup_code(entry, index
          for i, s in zip(index_cnames, bufaux.stridevars):
              params.append(i)
              params.append(s.cname)
-         funcname = "__Pyx_BufPtrStrided%dd" % nd
-         funcgen = buf_lookup_strided_code
          
      # Make sure the utility code is available
 -    code.globalstate.use_generated_code(funcgen, name=funcname, nd=nd)
 +    code.globalstate.use_code_from(funcgen, name=funcname, nd=nd)
  
-     ptrcode = "%s(%s.buf, %s)" % (funcname, bufstruct, ", ".join(params))
-     return entry.type.buffer_ptr_type.cast_code(ptrcode)
+     ptr_type = entry.type.buffer_ptr_type
+     ptrcode = "%s(%s, %s.buf, %s)" % (funcname,
+                                       ptr_type.declaration_code(""),
+                                       bufstruct,
+                                       ", ".join(params))
+     return ptrcode
  
  
  def use_empty_bufstruct_code(env, max_ndim):
@@@ -557,19 -576,14 +619,19 @@@ def get_getbuffer_code(dtype, code)
      """
  
      name = "__Pyx_GetBuffer_%s" % mangle_dtype_name(dtype)
 -    if not code.globalstate.has_utility_code(name):
 +    if not code.globalstate.has_code(name):
          code.globalstate.use_utility_code(acquire_utility_code)
 -        itemchecker = get_ts_check_item(dtype, code)
 +        typestringchecker = "__Pyx_CheckTypestring_%s" % mangle_dtype_name(dtype)
 +        code.globalstate.use_code_from(create_typestringchecker,
 +                                       typestringchecker,
 +                                       dtype=dtype)
 +
 +        dtype_name = str(dtype)
+         dtype_cname = dtype.declaration_code("")
          utilcode = [dedent("""
-         static int %s(PyObject* obj, Py_buffer* buf, int flags, int nd); /*proto*/
+         static int %s(PyObject* obj, Py_buffer* buf, int flags, int nd, int cast); /*proto*/
          """) % name, dedent("""
-         static int %(name)s(PyObject* obj, Py_buffer* buf, int flags, int nd) {
+         static int %(name)s(PyObject* obj, Py_buffer* buf, int flags, int nd, int cast) {
            const char* ts;
            if (obj == Py_None) {
              __Pyx_ZeroBuffer(buf);
              __Pyx_BufferNdimError(buf, nd);
              goto fail;
            }
-           ts = buf->format;
-           ts = %(typestringchecker)s(ts); if (!ts) goto fail;
-           ts = __Pyx_ConsumeWhitespace(ts);
-           if (*ts != 0) {
-             PyErr_Format(PyExc_ValueError,
-               "Buffer format string specifies more data than '%(dtype_name)s' can hold (expected end, got '%%s')", ts);
-             goto fail;
+           if (!cast) {
+             ts = buf->format;
+             ts = __Pyx_ConsumeWhitespace(ts);
+             if (!ts) goto fail;
 -            ts = %(itemchecker)s(ts);
++            ts = %(typestringchecker)s(ts);
+             if (!ts) goto fail;
+             ts = __Pyx_ConsumeWhitespace(ts);
+             if (!ts) goto fail;
+             if (*ts != 0) {
+               PyErr_Format(PyExc_ValueError,
 -                "Expected non-struct buffer data type (expected end, got '%%s')", ts);
++                "Buffer format string specifies more data than '%(dtype_name)s' can hold (expected end, got '%%s')", ts);
+               goto fail;
+             }
+           } else {
+             if (buf->itemsize != sizeof(%(dtype_cname)s)) {
+               PyErr_SetString(PyExc_ValueError,
+                 "Attempted cast of buffer to datatype of different size.");
+               goto fail;
+             }
            }
            if (buf->suboffsets == NULL) buf->suboffsets = __Pyx_minusones;
            return 0;
index 2b69f484965a2b43f65b39374f2df332d28aaad6,6fe06d88d82f61586dd755195d4140c3a6a3a3dd..bc57c53b4d079dcf51de83d3cbce4491c89f6a71
@@@ -168,7 -174,7 +174,8 @@@ class GlobalState(object)
          self.used_utility_code = set()
          self.declared_cnames = {}
          self.pystring_table_needed = False
 +        self.in_utility_code_generation = False
+         self.emit_linenums = emit_linenums
  
      def initwriters(self, rootwriter):
          self.utilprotowriter = rootwriter.new_writer()
index f8237beea37e5a9101ee940e2b874c7dca97c619,cd9a5a46151688c19d29ab713fbf9a38de7c282e..ddf5f6182a1043b282b71cb5e829232643103903
@@@ -1460,7 -1463,7 +1464,7 @@@ class IndexNode(ExprNode)
      
      def is_lvalue(self):
          return 1
--    
++
      def calculate_result_code(self):
          if self.is_buffer_access:
              return "<not used>"
  
      def generate_result_code(self, code):
          if self.is_buffer_access:
++            # buffer_pointer_code is returned by result()
              ptrcode = self.buffer_lookup_code(code)
              code.putln("%s = *%s;" % (
-                 self.result_code,
+                 self.result(),
                  self.buffer_type.buffer_ptr_type.cast_code(ptrcode)))
              # Must incref the value we pulled out.
              if self.buffer_type.dtype.is_pyobject:
Simple merge
Simple merge
Simple merge
index 33336ab93c4cde8e0f94ae5917e0c1a19512548c,e070d3ce5f74f312b845c86ab18d042914ab4949..934e33d4e7e821c2fabf4e6cb1e2607e48094566
@@@ -151,20 -184,17 +182,29 @@@ def put_range_long_1d(np.ndarray[long] 
          arr[i] = value
          value += 1
  
+ def test_c_contig(np.ndarray[int, ndim=2, mode='c'] arr):
+     cdef int i, j
+     for i in range(arr.shape[0]):
+         print " ".join([str(arr[i, j]) for j in range(arr.shape[1])])
+ def test_f_contig(np.ndarray[int, ndim=2, mode='fortran'] arr):
+     cdef int i, j
+     for i in range(arr.shape[0]):
+         print " ".join([str(arr[i, j]) for j in range(arr.shape[1])])
  
 -# Exhaustive dtype tests -- increments element [1] by 1 for all dtypes
 +cdef struct cfloat:
 +    float real
 +    float imag
 +
 +cdef struct cdouble:
 +    double real
 +    double imag
 +
 +cdef struct clongdouble:
 +    long double real
 +    long double imag
 +
 +# Exhaustive dtype tests -- increments element [1] by 1 (or 1+1j) for all dtypes
  def inc1_byte(np.ndarray[char] arr):                    arr[1] += 1
  def inc1_ubyte(np.ndarray[unsigned char] arr):          arr[1] += 1
  def inc1_short(np.ndarray[short] arr):                  arr[1] += 1
@@@ -216,11 -229,18 +256,21 @@@ def inc1_float64_t(np.ndarray[np.float6
  
      
  def test_dtype(dtype, inc1):
 -    a = np.array([0, 10], dtype=dtype)
 -    inc1(a)
 -    if a[1] != 11: print "failed!"
 -
 +    if dtype in ('F', 'D', 'G'):
 +        a = np.array([0, 10+10j], dtype=dtype)
 +        inc1(a)
 +        if a[1] != (11 + 11j): print "failed!", a[1]
 +    else:
 +        a = np.array([0, 10], dtype=dtype)
 +        inc1(a)
 +        if a[1] != 11: print "failed!"
+ def test_good_cast():
+     # Check that a signed int can round-trip through casted unsigned int access
+     cdef np.ndarray[unsigned int, cast=True] arr = np.array([-100], dtype='i')
+     cdef unsigned int data = arr[0]
+     return -100 == <int>data
+ def test_bad_cast():
+     # This should raise an exception
+     cdef np.ndarray[long, cast=True] arr = np.array([1], dtype='b')
 -