Buffer.py 22.6 KB
Newer Older
1 2 3 4 5 6 7
from Cython.Compiler.Visitor import VisitorTransform, temp_name_handle, CythonTransform
from Cython.Compiler.ModuleNode import ModuleNode
from Cython.Compiler.Nodes import *
from Cython.Compiler.ExprNodes import *
from Cython.Compiler.TreeFragment import TreeFragment
from Cython.Utils import EncodedString
from Cython.Compiler.Errors import CompileError
8
import PyrexTypes
9
from sets import Set as set
10
from textwrap import dedent
11 12 13 14 15 16 17 18 19 20 21

class IntroduceBufferAuxiliaryVars(CythonTransform):

    #
    # Entry point
    #

    buffers_exists = False

    def __call__(self, node):
        assert isinstance(node, ModuleNode)
22
        self.max_ndim = 0
23 24 25 26 27
        result = super(IntroduceBufferAuxiliaryVars, self).__call__(node)
        if self.buffers_exists:
            if "endian.h" not in node.scope.include_files:
                node.scope.include_files.append("endian.h")
            use_py2_buffer_functions(node.scope)
28 29
            use_empty_bufstruct_code(node.scope, self.max_ndim)
            node.scope.use_utility_code(access_utility_code)
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
        return result


    #
    # Basic operations for transforms
    #
    def handle_scope(self, node, scope):
        # For all buffers, insert extra variables in the scope.
        # The variables are also accessible from the buffer_info
        # on the buffer entry
        bufvars = [entry for name, entry
                   in scope.entries.iteritems()
                   if entry.type.is_buffer]
        if len(bufvars) > 0:
            self.buffers_exists = True


        if isinstance(node, ModuleNode) and len(bufvars) > 0:
            # for now...note that pos is wrong 
            raise CompileError(node.pos, "Buffer vars not allowed in module scope")
        for entry in bufvars:
            name = entry.name
            buftype = entry.type
53 54
            if buftype.ndim > self.max_ndim:
                self.max_ndim = buftype.ndim
55 56 57 58 59 60 61 62 63 64 65

            # Get or make a type string checker
            tschecker = buffer_type_checker(buftype.dtype, scope)

            # Declare auxiliary vars
            cname = scope.mangle(Naming.bufstruct_prefix, name)
            bufinfo = scope.declare_var(name="$%s" % cname, cname=cname,
                                        type=PyrexTypes.c_py_buffer_type, pos=node.pos)

            bufinfo.used = True

66
            def var(prefix, idx, initval):
67 68 69 70
                cname = scope.mangle(prefix, "%d_%s" % (idx, name))
                result = scope.declare_var("$%s" % cname, PyrexTypes.c_py_ssize_t_type,
                                         node.pos, cname=cname, is_cdef=True)

71
                result.init = initval
72 73 74 75
                if entry.is_arg:
                    result.used = True
                return result
            
76 77 78
            stridevars = [var(Naming.bufstride_prefix, i, "0") for i in range(entry.type.ndim)]
            shapevars = [var(Naming.bufshape_prefix, i, "0") for i in range(entry.type.ndim)]            
            suboffsetvars = [var(Naming.bufsuboffset_prefix, i, "-1") for i in range(entry.type.ndim)]
79
            entry.buffer_aux = Symtab.BufferAux(bufinfo, stridevars, shapevars, tschecker)
80 81 82
            entry.buffer_aux.lookup = get_buf_lookup_full(scope, entry.type.ndim)
            entry.buffer_aux.suboffsetvars = suboffsetvars
            entry.buffer_aux.get_buffer_cname = tschecker
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
            
        scope.buffer_entries = bufvars
        self.scope = scope

    def visit_ModuleNode(self, node):
        self.handle_scope(node, node.scope)
        self.visitchildren(node)
        return node

    def visit_FuncDefNode(self, node):
        self.handle_scope(node, node.local_scope)
        self.visitchildren(node)
        return node




100 101 102 103 104
def get_flags(buffer_aux, buffer_type):
    flags = 'PyBUF_FORMAT | PyBUF_INDIRECT'
    if buffer_aux.writable_needed: flags += "| PyBUF_WRITABLE"
    return flags
        
105 106 107 108 109
def used_buffer_aux_vars(entry):
    buffer_aux = entry.buffer_aux
    buffer_aux.buffer_info_var.used = True
    for s in buffer_aux.shapevars: s.used = True
    for s in buffer_aux.stridevars: s.used = True
110
    for s in buffer_aux.suboffsetvars: s.used = True
111 112 113 114

def put_unpack_buffer_aux_into_scope(buffer_aux, code):
    bufstruct = buffer_aux.buffer_info_var.cname

115 116 117 118 119 120 121 122
    # __pyx_bstride_0_buf = __pyx_bstruct_buf.strides[0] and so on

    for field, vars in (("strides", buffer_aux.stridevars),
                        ("shape", buffer_aux.shapevars),
                        ("suboffsets", buffer_aux.suboffsetvars)):
        code.putln(" ".join(["%s = %s.%s[%d];" %
                             (s.cname, bufstruct, field, idx)
                             for idx, s in enumerate(vars)]))
123

124 125
def getbuffer_cond_code(obj_cname, buffer_aux, flags, ndim):
    bufstruct = buffer_aux.buffer_info_var.cname
126 127
    return "%s(%s, &%s, %s, %d) == -1" % (
        buffer_aux.get_buffer_cname, obj_cname, bufstruct, flags, ndim)
128
                   
129 130 131 132
def put_acquire_arg_buffer(entry, code, pos):
    buffer_aux = entry.buffer_aux
    cname  = entry.cname
    bufstruct = buffer_aux.buffer_info_var.cname
133
    flags = get_flags(buffer_aux, entry.type)
134
    # Acquire any new buffer
135
    code.putln(code.error_goto_if(getbuffer_cond_code(cname,
136 137 138 139
                                                    buffer_aux,
                                                    flags,
                                                    entry.type.ndim),
                                pos))
140 141 142 143
    # An exception raised in arg parsing cannot be catched, so no
    # need to do care about the buffer then.
    put_unpack_buffer_aux_into_scope(buffer_aux, code)

144 145 146 147 148 149
#def put_release_buffer_normal(entry, code):
#    code.putln("if (%s != Py_None) PyObject_ReleaseBuffer(%s, &%s);" % (
#        entry.cname,
#        entry.cname,
#        entry.buffer_aux.buffer_info_var.cname))

150
def put_release_buffer(entry, code):
151 152 153 154 155 156 157 158 159
    code.putln(dedent("""\
        if (%s != Py_None) {
          PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
          PyErr_Fetch(&__pyx_type, &__pyx_value, &__pyx_tb);
          PyObject_ReleaseBuffer(%s, &%s);
          PyErr_Restore(__pyx_type, __pyx_value, __pyx_tb);
        }""" % (entry.cname,
                entry.cname,
                entry.buffer_aux.buffer_info_var.cname)))
160

161
def put_assign_to_buffer(lhs_cname, rhs_cname, retcode_cname, buffer_aux, buffer_type,
162
                         is_initialized, pos, code):
163 164 165 166 167 168 169 170 171 172 173 174 175 176
    """
    Generate code for reassigning a buffer variables. This only deals with getting
    the buffer auxiliary structure and variables set up correctly, the assignment
    itself and refcounting is the responsibility of the caller.

    However, the assignment operation may throw an exception so that the reassignment
    never happens.
    
    Depending on the circumstances there are two possible outcomes:
    - Old buffer released, new acquired, rhs assigned to lhs
    - Old buffer released, new acquired which fails, reaqcuire old lhs buffer
      (which may or may not succeed).
    """
    
177
    bufstruct = buffer_aux.buffer_info_var.cname
178
    flags = get_flags(buffer_aux, buffer_type)
179

180 181 182 183 184 185
    getbuffer = "%s(%%s, &%s, %s, %d)" % (buffer_aux.get_buffer_cname,
                                          # note: object is filled in later
                                          bufstruct,
                                          flags,
                                          buffer_type.ndim)

186 187 188 189 190 191 192
    if is_initialized:
        # Release any existing buffer
        code.put('if (%s != Py_None) ' % lhs_cname)
        code.begin_block();
        code.putln('PyObject_ReleaseBuffer(%s, &%s);' % (
            lhs_cname, bufstruct))
        code.end_block()
193 194 195 196 197 198 199 200 201 202 203 204
        # Acquire
        code.putln("%s = %s;" % (retcode_cname, getbuffer % rhs_cname))
        # If acquisition failed, attempt to reacquire the old buffer
        # before raising the exception. A failure of reacquisition
        # will cause the reacquisition exception to be reported, one
        # can consider working around this later.
        code.putln('if (%s) ' % (code.unlikely("%s < 0" % retcode_cname)))
        code.begin_block()
        # In anticipation of a better temp system, create non-consistent C code for now
        code.putln('PyObject *__pyx_type, *__pyx_value, *__pyx_tb;')
        code.putln('PyErr_Fetch(&__pyx_type, &__pyx_value, &__pyx_tb);')
        code.put('if (%s) ' % code.unlikely("%s == -1" % (getbuffer % lhs_cname)))
205
        code.begin_block()
206 207 208 209 210 211
        code.putln('Py_XDECREF(__pyx_type); Py_XDECREF(__pyx_value); Py_XDECREF(__pyx_tb);')
        code.putln('PyErr_Format(PyExc_ValueError, "Buffer acquisition failed on assignment; and then reacquiring the old buffer failed too!");')
        code.putln('} else {')
        code.putln('PyErr_Restore(__pyx_type, __pyx_value, __pyx_tb);')
        code.end_block()
        # Unpack indices
212
        code.end_block()
213 214
        put_unpack_buffer_aux_into_scope(buffer_aux, code)
        code.putln(code.error_goto_if_neg(retcode_cname, pos))
215
    else:
216 217 218 219 220 221 222 223 224 225
        # Our entry had no previous value, so set to None when acquisition fails.
        # In this case, auxiliary vars should be set up right in initialization to a zero-buffer,
        # so it suffices to set the buf field to NULL.
        code.putln('if (%s) {' % code.unlikely("%s == -1" % (getbuffer % rhs_cname)))
        code.putln('%s = Py_None; Py_INCREF(Py_None); %s.buf = NULL;' % (lhs_cname, bufstruct))
        code.putln(code.error_goto(pos))
        code.put('} else {')
        # Unpack indices
        put_unpack_buffer_aux_into_scope(buffer_aux, code)
        code.putln('}')
226

227 228 229

def put_access(entry, index_types, index_cnames, tmp_cname, pos, code):
    """Returns a c string which can be used to access the buffer
230 231 232 233 234 235 236
    for reading or writing.

    As the bounds checking can have any number of combinations of unsigned
    arguments, smart optimizations etc. we insert it directly in the function
    body. The lookup however is delegated to a inline function that is instantiated
    once per ndim (lookup with suboffsets tend to get quite complicated).
    """
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    bufaux = entry.buffer_aux
    bufstruct = bufaux.buffer_info_var.cname
    # Check bounds and fix negative indices
    boundscheck = True
    nonegs = True
    if boundscheck:
        code.putln("%s = -1;" % tmp_cname)
    for idx, (type, cname, shape) in enumerate(zip(index_types, index_cnames,
                                  bufaux.shapevars)):
        if type.signed != 0:
            nonegs = False
            # not unsigned, deal with negative index
            code.putln("if (%s < 0) {" % cname)
            code.putln("%s += %s;" % (cname, shape.cname))
            if boundscheck:
                code.putln("if (%s) %s = %d;" % (
                    code.unlikely("%s < 0" % cname), tmp_cname, idx))
            code.put("} else ")
        else:
256
            if idx > 0: code.put("else ")
257 258 259 260 261 262 263
        if boundscheck:
            # check bounds in positive direction
            code.putln("if (%s) %s = %d;" % (
                code.unlikely("%s >= %s" % (cname, shape.cname)),
                tmp_cname, idx))
#    if boundscheck or not nonegs:
#        code.putln("}")
264
    if boundscheck:  
265 266
        code.put("if (%s) " % code.unlikely("%s != -1" % tmp_cname))
        code.begin_block()
267
        code.putln('__Pyx_BufferIndexError(%s);' % tmp_cname)
268 269 270 271 272 273 274 275 276
        code.putln(code.error_goto(pos))
        code.end_block() 
        
    # Create buffer lookup and return it

    offset = " + ".join(["%s * %s" % (idx, stride.cname)
                         for idx, stride in
                         zip(index_cnames, bufaux.stridevars)])
    ptrcode = "(%s.buf + %s)" % (bufstruct, offset)
277 278 279
    ptrcode = "%s(%s.buf, %s)" % (bufaux.lookup, bufstruct, 
                          ", ".join([", ".join([i, s.cname, o.cname]) for i, s, o in
                                     zip(index_cnames, bufaux.stridevars, bufaux.suboffsetvars)]))
280 281 282 283
    valuecode = "*%s" % entry.type.buffer_ptr_type.cast_code(ptrcode)
    return valuecode


284 285 286 287 288 289 290 291

def use_empty_bufstruct_code(env, max_ndim):
    code = dedent("""
        Py_ssize_t __Pyx_zeros[] = {%s};
        Py_ssize_t __Pyx_minusones[] = {%s};
    """) % (", ".join(["0"] * max_ndim), ", ".join(["-1"] * max_ndim))
    env.use_utility_code([code, ""])

292 293
# Utility function to set the right exception
# The caller should immediately goto_error
294
access_utility_code = [
295
"""\
296
static void __Pyx_BufferIndexError(int axis); /*proto*/
297
""","""\
298 299 300 301 302 303
static void __Pyx_BufferIndexError(int axis) {
  PyErr_Format(PyExc_IndexError,
     "Out of bounds on buffer access (axis %d)", axis);
}
"""]

304 305 306 307 308 309 310
#
# Buffer type checking. Utility code for checking that acquired
# buffers match our assumptions. We only need to check ndim and
# the format string; the access mode/flags is checked by the
# exporter.
#
buffer_check_utility_code = ["""\
311
static void __Pyx_ZeroBuffer(Py_buffer* buf); /*proto*/
312 313 314 315
static const char* __Pyx_ConsumeWhitespace(const char* ts); /*proto*/
static const char* __Pyx_BufferTypestringCheckEndian(const char* ts); /*proto*/
static void __Pyx_BufferNdimError(Py_buffer* buffer, int expected_ndim); /*proto*/
""", """
316 317 318 319 320 321 322
static void __Pyx_ZeroBuffer(Py_buffer* buf) {
  buf->buf = NULL;
  buf->strides = __Pyx_zeros;
  buf->shape = __Pyx_zeros;
  buf->suboffsets = __Pyx_minusones;
}

323 324 325 326 327 328 329 330 331 332 333 334
static const char* __Pyx_ConsumeWhitespace(const char* ts) {
  while (1) {
    switch (*ts) {
      case 10:
      case 13:
      case ' ':
        ++ts;
      default:
        return ts;
    }
  }
}
335

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
static const char* __Pyx_BufferTypestringCheckEndian(const char* ts) {
  int ok = 1;
  switch (*ts) {
    case '@':
    case '=':
      ++ts; break;
    case '<':
      if (__BYTE_ORDER == __LITTLE_ENDIAN) ++ts;
      else ok = 0;
      break;
    case '>':
    case '!':
      if (__BYTE_ORDER == __BIG_ENDIAN) ++ts;
      else ok = 0;
      break;
  }
  if (!ok) {
    PyErr_Format(PyExc_ValueError, "Buffer has wrong endianness (rejecting on '%s')", ts);
    return NULL;
  }
  return ts;
}
358

359 360 361 362 363
static void __Pyx_BufferNdimError(Py_buffer* buffer, int expected_ndim) {
  PyErr_Format(PyExc_ValueError,
               "Buffer has wrong number of dimensions (expected %d, got %d)",
               expected_ndim, buffer->ndim);
}
364

365
"""]
366 367


368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397

def get_buf_lookup_full(env, nd):
    """
    Generates and registers as utility a buffer lookup function for the right number
    of dimensions. The function gives back a void* at the right location.
    """
    name = "__Pyx_BufPtrFull_%dd" % nd
    if not env.has_utility_code(name):
        # _i_ndex, _s_tride, sub_o_ffset
        args = ", ".join(["Py_ssize_t i%d, Py_ssize_t s%d, Py_ssize_t o%d" % (i, i, i) for i in range(nd)])
        proto = dedent("""\
        static INLINE void* %s(void* buf, %s);
        """) % (name, args) 

        func = dedent("""
        static INLINE void* %s(void* buf, %s) {
          char* ptr = (char*)buf;
        """) % (name, args) + "".join([dedent("""\
          ptr += s%d * i%d;
          if (o%d >= 0) ptr = *((char**)ptr) + o%d; 
        """) % (i, i, i, i) for i in range(nd)]
        ) + "\nreturn ptr;\n}"

        env.use_utility_code([proto, func], name=name)
        
    return name




398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
#
# Utils for creating type string checkers
#
def mangle_dtype_name(dtype):
    # Use prefixes to seperate user defined types from builtins
    # (consider "typedef float unsigned_int")
    if dtype.typestring is None:
        prefix = "nn_"
    else:
        prefix = ""
    return prefix + dtype.declaration_code("").replace(" ", "_")

def get_ts_check_item(dtype, env):
    # See if we can consume one (unnamed) dtype as next item
    # Put native types and structs in seperate namespaces (as one could create a struct named unsigned_int...)
    name = "__Pyx_BufferTypestringCheck_item_%s" % mangle_dtype_name(dtype)
    if not env.has_utility_code(name):
        char = dtype.typestring
        if char is not None:
417
                # Can use direct comparison
418
            code = """\
419
  if (*ts != '%s') {
420 421
    PyErr_Format(PyExc_ValueError, "Buffer datatype mismatch (rejecting on '%%s')", ts);
    return NULL;
422
  } else return ts + 1;
423
""" % char
424 425 426 427 428
        else:
            # Cannot trust declared size; but rely on int vs float and
            # signed/unsigned to be correctly declared
            ctype = dtype.declaration_code("")
            code = """\
429 430
  int ok;
  switch (*ts) {"""
431 432 433 434 435 436 437 438 439 440 441 442
            if dtype.is_int:
                types = [
                    ('b', 'char'), ('h', 'short'), ('i', 'int'),
                    ('l', 'long'), ('q', 'long long')
                ]
                if dtype.signed == 0:
                    code += "".join(["\n    case '%s': ok = (sizeof(%s) == sizeof(%s) && (%s)-1 > 0); break;" %
                                 (char.upper(), ctype, against, ctype) for char, against in types])
                else:
                    code += "".join(["\n    case '%s': ok = (sizeof(%s) == sizeof(%s) && (%s)-1 < 0); break;" %
                                 (char, ctype, against, ctype) for char, against in types])
                code += """\
443 444 445
    default: ok = 0;
  }
  if (!ok) {
446
    PyErr_Format(PyExc_ValueError, "Buffer datatype mismatch (rejecting on '%s')", ts);
447 448 449
    return NULL;
  } else return ts + 1;
"""
450
        env.use_utility_code(["""\
451 452 453 454 455
static const char* %s(const char* ts); /*proto*/
""" % name, """
static const char* %s(const char* ts) {
%s
}
456
""" % (name, code)], name=name)
457

458
    return name
459

460 461 462 463 464 465 466 467 468 469 470
def get_getbuffer_code(dtype, env):
    """
    Generate a utility function for getting a buffer for the given dtype.
    The function will:
    - Call PyObject_GetBuffer
    - Check that ndim matched the expected value
    - Check that the format string is right
    - Set suboffsets to all -1 if it is returned as NULL.
    """

    name = "__Pyx_GetBuffer_%s" % mangle_dtype_name(dtype)
471 472
    if not env.has_utility_code(name):
        env.use_utility_code(buffer_check_utility_code)
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
        itemchecker = get_ts_check_item(dtype, env)
        utilcode = [dedent("""
        static int %s(PyObject* obj, Py_buffer* buf, int flags, int nd); /*proto*/
        """) % name, dedent("""
        static int %(name)s(PyObject* obj, Py_buffer* buf, int flags, int nd) {
          const char* ts;
          if (obj == Py_None) {
            __Pyx_ZeroBuffer(buf);
            return 0;
          }
          buf->buf = NULL;
          if (PyObject_GetBuffer(obj, buf, flags) == -1) goto fail;
          if (buf->ndim != nd) {
            __Pyx_BufferNdimError(buf, nd);
            goto fail;
          }
          ts = buf->format;
          ts = __Pyx_ConsumeWhitespace(ts);
          ts = __Pyx_BufferTypestringCheckEndian(ts);
          if (!ts) goto fail;
          ts = __Pyx_ConsumeWhitespace(ts);
          ts = %(itemchecker)s(ts);
          if (!ts) goto fail;
          ts = __Pyx_ConsumeWhitespace(ts);
          if (*ts != 0) {
            PyErr_Format(PyExc_ValueError,
              "Expected non-struct buffer data type (rejecting on '%%s')", ts);
            goto fail;
          }
          if (buf->suboffsets == NULL) buf->suboffsets = __Pyx_minusones;
          return 0;
        fail:;
          __Pyx_ZeroBuffer(buf);
          return -1;
        }""") % locals()]
508 509 510 511 512 513 514 515 516
        env.use_utility_code(utilcode, name)
    return name

def buffer_type_checker(dtype, env):
    # Creates a type checker function for the given type.
    if dtype.is_struct_or_union:
        assert False
    elif dtype.is_int or dtype.is_float:
        # This includes simple typedef-ed types
517
        funcname = get_getbuffer_code(dtype, env)
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
    else:
        assert False
    return funcname

def use_py2_buffer_functions(env):
    # will be refactored
    try:
        env.entries[u'numpy']
        env.use_utility_code(["","""
static int numpy_getbuffer(PyObject *obj, Py_buffer *view, int flags) {
  /* This function is always called after a type-check; safe to cast */
  PyArrayObject *arr = (PyArrayObject*)obj;
  PyArray_Descr *type = (PyArray_Descr*)arr->descr;

  
  int typenum = PyArray_TYPE(obj);
  if (!PyTypeNum_ISNUMBER(typenum)) {
    PyErr_Format(PyExc_TypeError, "Only numeric NumPy types currently supported.");
    return -1;
  }
538

539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
  /*
  NumPy format codes doesn't completely match buffer codes;
  seems safest to retranslate.
                            01234567890123456789012345*/
  const char* base_codes = "?bBhHiIlLqQfdgfdgO";

  char* format = (char*)malloc(4);
  char* fp = format;
  *fp++ = type->byteorder;
  if (PyTypeNum_ISCOMPLEX(typenum)) *fp++ = 'Z';
  *fp++ = base_codes[typenum];
  *fp = 0;

  view->buf = arr->data;
  view->readonly = !PyArray_ISWRITEABLE(obj);
  view->ndim = PyArray_NDIM(arr);
  view->strides = PyArray_STRIDES(arr);
  view->shape = PyArray_DIMS(arr);
  view->suboffsets = NULL;
  view->format = format;
  view->itemsize = type->elsize;

  view->internal = 0;
  return 0;
}
564

565 566 567 568
static void numpy_releasebuffer(PyObject *obj, Py_buffer *view) {
  free((char*)view->format);
  view->format = NULL;
}
569

570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
"""])
    except KeyError:
        pass

    codename = "PyObject_GetBuffer" # just a representative unique key

    # Search all types for __getbuffer__ overloads
    types = []
    def find_buffer_types(scope):
        for m in scope.cimported_modules:
            find_buffer_types(m)
        for e in scope.type_entries:
            t = e.type
            if t.is_extension_type:
                release = get = None
                for x in t.scope.pyfunc_entries:
                    if x.name == u"__getbuffer__": get = x.func_cname
                    elif x.name == u"__releasebuffer__": release = x.func_cname
                if get:
                    types.append((t.typeptr_cname, get, release))

    find_buffer_types(env)

    # For now, hard-code numpy imported as "numpy"
    try:
        ndarrtype = env.entries[u'numpy'].as_module.entries['ndarray'].type
        types.append((ndarrtype.typeptr_cname, "numpy_getbuffer", "numpy_releasebuffer"))
    except KeyError:
        pass

    code = """
#if PY_VERSION_HEX < 0x02060000
static int PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags) {
"""
    if len(types) > 0:
        clause = "if"
        for t, get, release in types:
            code += "  %s (PyObject_TypeCheck(obj, %s)) return %s(obj, view, flags);\n" % (clause, t, get)
            clause = "else if"
        code += "  else {\n"
    code += """\
  PyErr_Format(PyExc_TypeError, "'%100s' does not have the buffer interface", Py_TYPE(obj)->tp_name);
  return -1;
"""
    if len(types) > 0: code += "  }"
    code += """
}
617

618 619 620 621 622 623 624 625 626 627
static void PyObject_ReleaseBuffer(PyObject *obj, Py_buffer *view) {
"""
    if len(types) > 0:
        clause = "if"
        for t, get, release in types:
            if release:
                code += "%s (PyObject_TypeCheck(obj, %s)) %s(obj, view);" % (clause, t, release)
                clause = "else if"
    code += """
}
628

629 630 631 632 633 634 635 636
#endif
"""
    env.use_utility_code(["""\
#if PY_VERSION_HEX < 0x02060000
static int PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags);
static void PyObject_ReleaseBuffer(PyObject *obj, Py_buffer *view);
#endif
""" ,code], codename)