Commit 12754a43 authored by Bradley M. Froehle's avatar Bradley M. Froehle

Add ctypedef for __builtin__.type.

This allows a "C" function like
    extern "C" PyObject* f(PyTypeObject *a);
to be wrapped by Cython as
    from cpython.type import type
    cdef extern from *:
        object f(type a)

In particular, this allows Cython to produce code which calls f as
    f((PyTypeObject*) a)
which means that it will compile in C++.
parent 684ecc93
......@@ -40,6 +40,11 @@ non_portable_builtins_map = {
'BaseException' : ('PY_VERSION_HEX < 0x02050000', 'Exception'),
}
basicsize_builtins_map = {
# builtins whose type has a different tp_basicsize than sizeof(...)
'PyTypeObject' : 'PyHeapTypeObject',
}
uncachable_builtins = [
# builtin names that cannot be cached because they may or may not
# be available at import time
......
......@@ -2181,6 +2181,8 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
objstruct = type.objstruct_cname
else:
objstruct = "struct %s" % type.objstruct_cname
# Some builtin types have a tp_basicsize which differs from sizeof(...):
objstruct = Code.basicsize_builtins_map.get(objstruct, objstruct)
module_name = type.module_name
condition = None
if module_name not in ('__builtin__', 'builtins'):
......
......@@ -6,6 +6,9 @@ cdef extern from "Python.h":
# 7.1.1 Type Objects
############################################################################
ctypedef class __builtin__.type [object PyTypeObject]:
pass
# PyObject* PyType_Type
# This is the type object for type objects; it is the same object
# as type and types.TypeType in the Python layer.
......@@ -28,16 +31,16 @@ cdef extern from "Python.h":
# Return true if the type object includes support for the cycle
# detector; this tests the type flag Py_TPFLAGS_HAVE_GC.
bint PyType_IsSubtype(object a, object b)
bint PyType_IsSubtype(type a, type b)
# Return true if a is a subtype of b.
object PyType_GenericAlloc(object type, Py_ssize_t nitems)
# Return value: New reference.
object PyType_GenericNew(object type, object args, object kwds)
object PyType_GenericNew(type type, object args, object kwds)
# Return value: New reference.
bint PyType_Ready(object type) except -1
bint PyType_Ready(type type) except -1
# Finalize a type object. This should be called on all type
# objects to finish their initialization. This function is
# responsible for adding inherited slots from a type's base
......
......@@ -19,6 +19,7 @@ DEF _buffer_format_string_len = 255
cimport cpython.buffer as pybuf
from cpython.ref cimport Py_INCREF, Py_XDECREF
from cpython.object cimport PyObject
from cpython.type cimport type
cimport libc.stdlib as stdlib
cimport libc.stdio as stdio
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment