Commit 32fcfdd8 authored by scoder's avatar scoder

Merge pull request #126 from bfroehle/_cpython_type_PyTypeObject

cpython.type: Add ctypedef for `__builtin__.type`
parents 906bbedc 6a1f9292
...@@ -40,6 +40,11 @@ non_portable_builtins_map = { ...@@ -40,6 +40,11 @@ non_portable_builtins_map = {
'BaseException' : ('PY_VERSION_HEX < 0x02050000', 'Exception'), 'BaseException' : ('PY_VERSION_HEX < 0x02050000', 'Exception'),
} }
basicsize_builtins_map = {
# builtins whose type has a different tp_basicsize than sizeof(...)
'PyTypeObject' : 'PyHeapTypeObject',
}
uncachable_builtins = [ uncachable_builtins = [
# builtin names that cannot be cached because they may or may not # builtin names that cannot be cached because they may or may not
# be available at import time # be available at import time
......
...@@ -2191,6 +2191,8 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -2191,6 +2191,8 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
objstruct = type.objstruct_cname objstruct = type.objstruct_cname
else: else:
objstruct = "struct %s" % type.objstruct_cname 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 module_name = type.module_name
condition = None condition = None
if module_name not in ('__builtin__', 'builtins'): if module_name not in ('__builtin__', 'builtins'):
......
...@@ -6,6 +6,9 @@ cdef extern from "Python.h": ...@@ -6,6 +6,9 @@ cdef extern from "Python.h":
# 7.1.1 Type Objects # 7.1.1 Type Objects
############################################################################ ############################################################################
ctypedef class __builtin__.type [object PyTypeObject]:
pass
# PyObject* PyType_Type # PyObject* PyType_Type
# This is the type object for type objects; it is the same object # This is the type object for type objects; it is the same object
# as type and types.TypeType in the Python layer. # as type and types.TypeType in the Python layer.
...@@ -28,16 +31,16 @@ cdef extern from "Python.h": ...@@ -28,16 +31,16 @@ cdef extern from "Python.h":
# Return true if the type object includes support for the cycle # Return true if the type object includes support for the cycle
# detector; this tests the type flag Py_TPFLAGS_HAVE_GC. # 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. # Return true if a is a subtype of b.
object PyType_GenericAlloc(object type, Py_ssize_t nitems) object PyType_GenericAlloc(object type, Py_ssize_t nitems)
# Return value: New reference. # 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. # 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 # Finalize a type object. This should be called on all type
# objects to finish their initialization. This function is # objects to finish their initialization. This function is
# responsible for adding inherited slots from a type's base # responsible for adding inherited slots from a type's base
......
...@@ -19,6 +19,7 @@ DEF _buffer_format_string_len = 255 ...@@ -19,6 +19,7 @@ DEF _buffer_format_string_len = 255
cimport cpython.buffer as pybuf cimport cpython.buffer as pybuf
from cpython.ref cimport Py_INCREF, Py_XDECREF from cpython.ref cimport Py_INCREF, Py_XDECREF
from cpython.object cimport PyObject from cpython.object cimport PyObject
from cpython.type cimport type
cimport libc.stdlib as stdlib cimport libc.stdlib as stdlib
cimport libc.stdio as stdio cimport libc.stdio as stdio
......
from cpython.type cimport PyType_IsSubtype
class mylist(list): pass
def test_issubtype(a, b):
"""
>>> test_issubtype(mylist, list)
True
>>> test_issubtype(mylist, dict)
False
>>> o = object()
>>> test_issubtype(o, list)
Traceback (most recent call last):
...
TypeError: Cannot convert object to type
"""
return PyType_IsSubtype(a, b)
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