[pypy-commit] pypy default: cpyext: Finally found a way to allow subclasses of int!
amauryfa
noreply at buildbot.pypy.org
Fri Feb 24 21:44:12 CET 2012
Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch:
Changeset: r52889:0eaf96f13694
Date: 2012-02-24 21:43 +0100
http://bitbucket.org/pypy/pypy/changeset/0eaf96f13694/
Log: cpyext: Finally found a way to allow subclasses of int! It's even
possible to set ob_ival... at least until the object escapes the C
function and is seen by pypy.
diff --git a/pypy/module/cpyext/api.py b/pypy/module/cpyext/api.py
--- a/pypy/module/cpyext/api.py
+++ b/pypy/module/cpyext/api.py
@@ -407,7 +407,7 @@
}.items():
GLOBALS['Py%s_Type#' % (cpyname, )] = ('PyTypeObject*', pypyexpr)
- for cpyname in 'Method List Int Long Dict Tuple Class'.split():
+ for cpyname in 'Method List Long Dict Tuple Class'.split():
FORWARD_DECLS.append('typedef struct { PyObject_HEAD } '
'Py%sObject' % (cpyname, ))
build_exported_objects()
diff --git a/pypy/module/cpyext/include/intobject.h b/pypy/module/cpyext/include/intobject.h
--- a/pypy/module/cpyext/include/intobject.h
+++ b/pypy/module/cpyext/include/intobject.h
@@ -7,6 +7,11 @@
extern "C" {
#endif
+typedef struct {
+ PyObject_HEAD
+ long ob_ival;
+} PyIntObject;
+
#ifdef __cplusplus
}
#endif
diff --git a/pypy/module/cpyext/intobject.py b/pypy/module/cpyext/intobject.py
--- a/pypy/module/cpyext/intobject.py
+++ b/pypy/module/cpyext/intobject.py
@@ -2,11 +2,37 @@
from pypy.rpython.lltypesystem import rffi, lltype
from pypy.interpreter.error import OperationError
from pypy.module.cpyext.api import (
- cpython_api, build_type_checkers, PyObject,
- CONST_STRING, CANNOT_FAIL, Py_ssize_t)
+ cpython_api, cpython_struct, build_type_checkers, bootstrap_function,
+ PyObject, PyObjectFields, CONST_STRING, CANNOT_FAIL, Py_ssize_t)
+from pypy.module.cpyext.pyobject import (
+ make_typedescr, track_reference, RefcountState, from_ref)
from pypy.rlib.rarithmetic import r_uint, intmask, LONG_TEST
+from pypy.objspace.std.intobject import W_IntObject
import sys
+PyIntObjectStruct = lltype.ForwardReference()
+PyIntObject = lltype.Ptr(PyIntObjectStruct)
+PyIntObjectFields = PyObjectFields + \
+ (("ob_ival", rffi.LONG),)
+cpython_struct("PyIntObject", PyIntObjectFields, PyIntObjectStruct)
+
+ at bootstrap_function
+def init_intobject(space):
+ "Type description of PyIntObject"
+ make_typedescr(space.w_int.instancetypedef,
+ basestruct=PyIntObject.TO,
+ realize=int_realize)
+
+def int_realize(space, obj):
+ intval = rffi.cast(lltype.Signed, rffi.cast(PyIntObject, obj).c_ob_ival)
+ w_type = from_ref(space, rffi.cast(PyObject, obj.c_ob_type))
+ w_obj = space.allocate_instance(W_IntObject, w_type)
+ w_obj.__init__(intval)
+ track_reference(space, obj, w_obj)
+ state = space.fromcache(RefcountState)
+ state.set_lifeline(w_obj, obj)
+ return w_obj
+
PyInt_Check, PyInt_CheckExact = build_type_checkers("Int")
@cpython_api([], lltype.Signed, error=CANNOT_FAIL)
diff --git a/pypy/module/cpyext/object.py b/pypy/module/cpyext/object.py
--- a/pypy/module/cpyext/object.py
+++ b/pypy/module/cpyext/object.py
@@ -193,7 +193,7 @@
if not obj:
PyErr_NoMemory(space)
obj.c_ob_type = type
- _Py_NewReference(space, obj)
+ obj.c_ob_refcnt = 1
return obj
@cpython_api([PyVarObject, PyTypeObjectPtr, Py_ssize_t], PyObject)
diff --git a/pypy/module/cpyext/pyobject.py b/pypy/module/cpyext/pyobject.py
--- a/pypy/module/cpyext/pyobject.py
+++ b/pypy/module/cpyext/pyobject.py
@@ -17,6 +17,7 @@
class BaseCpyTypedescr(object):
basestruct = PyObject.TO
+ W_BaseObject = W_ObjectObject
def get_dealloc(self, space):
from pypy.module.cpyext.typeobject import subtype_dealloc
@@ -51,10 +52,14 @@
def attach(self, space, pyobj, w_obj):
pass
- def realize(self, space, ref):
- # For most types, a reference cannot exist without
- # a real interpreter object
- raise InvalidPointerException(str(ref))
+ def realize(self, space, obj):
+ w_type = from_ref(space, rffi.cast(PyObject, obj.c_ob_type))
+ w_obj = space.allocate_instance(self.W_BaseObject, w_type)
+ track_reference(space, obj, w_obj)
+ if w_type is not space.gettypefor(self.W_BaseObject):
+ state = space.fromcache(RefcountState)
+ state.set_lifeline(w_obj, obj)
+ return w_obj
typedescr_cache = {}
@@ -369,13 +374,7 @@
obj.c_ob_refcnt = 1
w_type = from_ref(space, rffi.cast(PyObject, obj.c_ob_type))
assert isinstance(w_type, W_TypeObject)
- if w_type.is_cpytype():
- w_obj = space.allocate_instance(W_ObjectObject, w_type)
- track_reference(space, obj, w_obj)
- state = space.fromcache(RefcountState)
- state.set_lifeline(w_obj, obj)
- else:
- assert False, "Please add more cases in _Py_NewReference()"
+ get_typedescr(w_type.instancetypedef).realize(space, obj)
def _Py_Dealloc(space, obj):
from pypy.module.cpyext.api import generic_cpy_call_dont_decref
diff --git a/pypy/module/cpyext/test/test_intobject.py b/pypy/module/cpyext/test/test_intobject.py
--- a/pypy/module/cpyext/test/test_intobject.py
+++ b/pypy/module/cpyext/test/test_intobject.py
@@ -65,4 +65,97 @@
values = module.values()
types = [type(x) for x in values]
assert types == [int, long, int, int]
-
+
+ def test_int_subtype(self):
+ module = self.import_extension(
+ 'foo', [
+ ("newEnum", "METH_VARARGS",
+ """
+ EnumObject *enumObj;
+ long intval;
+ PyObject *name;
+
+ if (!PyArg_ParseTuple(args, "Oi", &name, &intval))
+ return NULL;
+
+ PyType_Ready(&Enum_Type);
+ enumObj = PyObject_New(EnumObject, &Enum_Type);
+ if (!enumObj) {
+ return NULL;
+ }
+
+ enumObj->ob_ival = intval;
+ Py_INCREF(name);
+ enumObj->ob_name = name;
+
+ return (PyObject *)enumObj;
+ """),
+ ],
+ prologue="""
+ typedef struct
+ {
+ PyObject_HEAD
+ long ob_ival;
+ PyObject* ob_name;
+ } EnumObject;
+
+ static void
+ enum_dealloc(EnumObject *op)
+ {
+ Py_DECREF(op->ob_name);
+ Py_TYPE(op)->tp_free((PyObject *)op);
+ }
+
+ static PyMemberDef enum_members[] = {
+ {"name", T_OBJECT, offsetof(EnumObject, ob_name), 0, NULL},
+ {NULL} /* Sentinel */
+ };
+
+ PyTypeObject Enum_Type = {
+ PyObject_HEAD_INIT(0)
+ /*ob_size*/ 0,
+ /*tp_name*/ "Enum",
+ /*tp_basicsize*/ sizeof(EnumObject),
+ /*tp_itemsize*/ 0,
+ /*tp_dealloc*/ enum_dealloc,
+ /*tp_print*/ 0,
+ /*tp_getattr*/ 0,
+ /*tp_setattr*/ 0,
+ /*tp_compare*/ 0,
+ /*tp_repr*/ 0,
+ /*tp_as_number*/ 0,
+ /*tp_as_sequence*/ 0,
+ /*tp_as_mapping*/ 0,
+ /*tp_hash*/ 0,
+ /*tp_call*/ 0,
+ /*tp_str*/ 0,
+ /*tp_getattro*/ 0,
+ /*tp_setattro*/ 0,
+ /*tp_as_buffer*/ 0,
+ /*tp_flags*/ Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
+ /*tp_doc*/ 0,
+ /*tp_traverse*/ 0,
+ /*tp_clear*/ 0,
+ /*tp_richcompare*/ 0,
+ /*tp_weaklistoffset*/ 0,
+ /*tp_iter*/ 0,
+ /*tp_iternext*/ 0,
+ /*tp_methods*/ 0,
+ /*tp_members*/ enum_members,
+ /*tp_getset*/ 0,
+ /*tp_base*/ &PyInt_Type,
+ /*tp_dict*/ 0,
+ /*tp_descr_get*/ 0,
+ /*tp_descr_set*/ 0,
+ /*tp_dictoffset*/ 0,
+ /*tp_init*/ 0,
+ /*tp_alloc*/ 0,
+ /*tp_new*/ 0
+ };
+ """)
+
+ a = module.newEnum("ULTIMATE_ANSWER", 42)
+ assert type(a).__name__ == "Enum"
+ assert isinstance(a, int)
+ assert a == int(a) == 42
+ assert a.name == "ULTIMATE_ANSWER"
More information about the pypy-commit
mailing list