From d125223d1622cc60b8ea266b6a5abb54a45d40e8 Mon Sep 17 00:00:00 2001 From: Dag Sverre Seljebotn Date: Thu, 14 May 2009 14:58:58 +0200 Subject: [PATCH] Buffer support for complex numbers --- Cython/Compiler/Buffer.py | 2 +- Cython/Compiler/PyrexTypes.py | 2 +- tests/run/bufaccess.pyx | 23 ++++++++++++++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/Cython/Compiler/Buffer.py b/Cython/Compiler/Buffer.py index e19fb3fb..d2788aa1 100644 --- a/Cython/Compiler/Buffer.py +++ b/Cython/Compiler/Buffer.py @@ -635,7 +635,7 @@ def get_type_information_cname(code, dtype, maxdepth=None): typegroup = 'U' else: typegroup = 'I' - elif complex_possible: + elif complex_possible or dtype.is_complex: typegroup = 'C' elif dtype.is_float: typegroup = 'R' diff --git a/Cython/Compiler/PyrexTypes.py b/Cython/Compiler/PyrexTypes.py index 54b81ad1..a106c386 100644 --- a/Cython/Compiler/PyrexTypes.py +++ b/Cython/Compiler/PyrexTypes.py @@ -142,7 +142,7 @@ class PyrexType(BaseType): return 1 def is_simple_buffer_dtype(self): - return (self.is_int or self.is_float or self.is_pyobject or + return (self.is_int or self.is_float or self.is_complex or self.is_pyobject or self.is_extension_type or self.is_ptr) def struct_nesting_depth(self): diff --git a/tests/run/bufaccess.pyx b/tests/run/bufaccess.pyx index db65dcac..f1515d5f 100644 --- a/tests/run/bufaccess.pyx +++ b/tests/run/bufaccess.pyx @@ -1333,10 +1333,31 @@ cdef class LongComplexMockBuffer(MockBuffer): cdef get_itemsize(self): return sizeof(LongComplex) cdef get_default_format(self): return b"Zg" +#cdef extern from "complex.h": +# pass + +@testcase +def complex_dtype(object[long double complex] buf): + """ + >>> complex_dtype(LongComplexMockBuffer(None, [(0, -1)])) + -1j + """ + print buf[0] + +@testcase +def complex_inplace(object[long double complex] buf): + """ + >>> complex_inplace(LongComplexMockBuffer(None, [(0, -1)])) + (1+1j) + """ + buf[0] = buf[0] + 1 + 2j + print buf[0] + @testcase def complex_struct_dtype(object[LongComplex] buf): """ - Note that the format string is "Zg" rather than "2g"... + Note that the format string is "Zg" rather than "2g", yet a struct + is accessed. >>> complex_struct_dtype(LongComplexMockBuffer(None, [(0, -1)])) 0.0 -1.0 """ -- 2.26.2