cython-devel
diff Cython/Includes/numpy.pxd @ 1996:58ff50bda693
__name__ == "__main__" for embedded modules
| author | Robert Bradshaw <robertwb@math.washington.edu> |
|---|---|
| date | Tue Apr 21 02:11:16 2009 -0700 (3 years ago) |
| parents | fa14f80b335c |
| children | fa5aefbc3b0f |
line diff
1.1 --- a/Cython/Includes/numpy.pxd Mon Nov 10 14:15:31 2008 +0100
1.2 +++ b/Cython/Includes/numpy.pxd Tue Apr 21 02:11:16 2009 -0700
1.3 @@ -132,58 +132,9 @@
1.4 return
1.5 else:
1.6 info.format = <char*>stdlib.malloc(255) # static size
1.7 - f = info.format
1.8 - stack = [iter(descr.fields.iteritems())]
1.9 + f = _util_dtypestring(descr, info.format, info.format + 255)
1.10 + f[0] = 0 # Terminate format string
1.11
1.12 - while True:
1.13 - iterator = stack[-1]
1.14 - descr = None
1.15 - while descr is None:
1.16 - try:
1.17 - descr = iterator.next()[1][0]
1.18 - except StopIteration:
1.19 - stack.pop()
1.20 - if len(stack) > 0:
1.21 - f[0] = 125 #"}"
1.22 - f += 1
1.23 - iterator = stack[-1]
1.24 - else:
1.25 - f[0] = 0 # Terminate string!
1.26 - return
1.27 -
1.28 - hasfields = PyDataType_HASFIELDS(descr)
1.29 - if not hasfields:
1.30 - t = descr.type_num
1.31 - if f - info.format > 240: # this should leave room for "T{" and "}" as well
1.32 - raise RuntimeError("Format string allocated too short.")
1.33 -
1.34 - # Until ticket #99 is fixed, use integers to avoid warnings
1.35 - if t == NPY_BYTE: f[0] = 98 #"b"
1.36 - elif t == NPY_UBYTE: f[0] = 66 #"B"
1.37 - elif t == NPY_SHORT: f[0] = 104 #"h"
1.38 - elif t == NPY_USHORT: f[0] = 72 #"H"
1.39 - elif t == NPY_INT: f[0] = 105 #"i"
1.40 - elif t == NPY_UINT: f[0] = 73 #"I"
1.41 - elif t == NPY_LONG: f[0] = 108 #"l"
1.42 - elif t == NPY_ULONG: f[0] = 76 #"L"
1.43 - elif t == NPY_LONGLONG: f[0] = 113 #"q"
1.44 - elif t == NPY_ULONGLONG: f[0] = 81 #"Q"
1.45 - elif t == NPY_FLOAT: f[0] = 102 #"f"
1.46 - elif t == NPY_DOUBLE: f[0] = 100 #"d"
1.47 - elif t == NPY_LONGDOUBLE: f[0] = 103 #"g"
1.48 - elif t == NPY_CFLOAT: f[0] = 90; f[1] = 102; f += 1
1.49 - elif t == NPY_CDOUBLE: f[0] = 90; f[1] = 100; f += 1
1.50 - elif t == NPY_CLONGDOUBLE: f[0] = 90; f[1] = 103; f += 1
1.51 - elif t == NPY_OBJECT: f[0] = 79 #"O"
1.52 - else:
1.53 - raise ValueError("unknown dtype code in numpy.pxd (%d)" % t)
1.54 - f += 1
1.55 - else:
1.56 - f[0] = 84 #"T"
1.57 - f[1] = 123 #"{"
1.58 - f += 2
1.59 - stack.append(iter(descr.fields.iteritems()))
1.60 -
1.61 def __releasebuffer__(ndarray self, Py_buffer* info):
1.62 if PyArray_HASFIELDS(self):
1.63 stdlib.free(info.format)
1.64 @@ -292,3 +243,48 @@
1.65 ctypedef npy_cfloat cfloat_t
1.66 ctypedef npy_cdouble cdouble_t
1.67 ctypedef npy_clongdouble clongdouble_t
1.68 +
1.69 +
1.70 +cdef inline char* _util_dtypestring(dtype descr, char* f, char* end) except NULL:
1.71 + # Recursive utility function used in __getbuffer__ to get format
1.72 + # string. The new location in the format string is returned.
1.73 +
1.74 + cdef dtype child
1.75 + cdef tuple i
1.76 + for i in descr.fields.itervalues():
1.77 + child = i[0]
1.78 + if not PyDataType_HASFIELDS(child):
1.79 + t = child.type_num
1.80 + if end - f < 15: # this should leave room for "T{" and "}" as well
1.81 + raise RuntimeError("Format string allocated too short.")
1.82 +
1.83 + # Until ticket #99 is fixed, use integers to avoid warnings
1.84 + if t == NPY_BYTE: f[0] = 98 #"b"
1.85 + elif t == NPY_UBYTE: f[0] = 66 #"B"
1.86 + elif t == NPY_SHORT: f[0] = 104 #"h"
1.87 + elif t == NPY_USHORT: f[0] = 72 #"H"
1.88 + elif t == NPY_INT: f[0] = 105 #"i"
1.89 + elif t == NPY_UINT: f[0] = 73 #"I"
1.90 + elif t == NPY_LONG: f[0] = 108 #"l"
1.91 + elif t == NPY_ULONG: f[0] = 76 #"L"
1.92 + elif t == NPY_LONGLONG: f[0] = 113 #"q"
1.93 + elif t == NPY_ULONGLONG: f[0] = 81 #"Q"
1.94 + elif t == NPY_FLOAT: f[0] = 102 #"f"
1.95 + elif t == NPY_DOUBLE: f[0] = 100 #"d"
1.96 + elif t == NPY_LONGDOUBLE: f[0] = 103 #"g"
1.97 + elif t == NPY_CFLOAT: f[0] = 90; f[1] = 102; f += 1
1.98 + elif t == NPY_CDOUBLE: f[0] = 90; f[1] = 100; f += 1
1.99 + elif t == NPY_CLONGDOUBLE: f[0] = 90; f[1] = 103; f += 1
1.100 + elif t == NPY_OBJECT: f[0] = 79 #"O"
1.101 + else:
1.102 + raise ValueError("unknown dtype code in numpy.pxd (%d)" % t)
1.103 + f += 1
1.104 + else:
1.105 + f[0] = 84 #"T"
1.106 + f[1] = 123 #"{"
1.107 + f += 2
1.108 + f = _util_dtypestring(child, f, end)
1.109 + f[0] = 125 #"}"
1.110 + f += 1
1.111 + return f
1.112 +
