| 1 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 2 |
from rpython.rlib.rarithmetic import widen |
| 3 |
from rpython.rlib import rgc, jit, rawrefcount |
| 4 |
from rpython.rlib.unroll import unrolling_iterable |
| 5 |
|
| 6 |
from pypy.interpreter.error import oefmt |
| 7 |
from pypy.interpreter.baseobjspace import W_Root, DescrMismatch |
| 8 |
from pypy.interpreter.gateway import interp2app |
| 9 |
from pypy.interpreter.typedef import GetSetProperty |
| 10 |
|
| 11 |
from pypy.module.cpyext import pyobject |
| 12 |
from pypy.module.cpyext.methodobject import PyMethodDef, PyCFunction |
| 13 |
from pypy.module.cpyext.modsupport import convert_method_defs |
| 14 |
from pypy.module.cpyext.api import (PyTypeObjectPtr, cts as cpyts, |
| 15 |
generic_cpy_call, Py_TPFLAGS_HEAPTYPE) |
| 16 |
from pypy.module.cpyext import structmemberdefs |
| 17 |
from pypy.module.cpyext.state import State |
| 18 |
from pypy.module.cpyext.typeobject import PyHeapTypeObject |
| 19 |
|
| 20 |
from pypy.module._hpy_universal.apiset import API |
| 21 |
from pypy.module._hpy_universal.interp_descr import W_HPyMemberDescriptor |
| 22 |
from pypy.module._hpy_universal.interp_type import W_HPyObject |
| 23 |
|
| 24 |
@API.func("HPy HPy_FromPyObject(HPyContext *ctx, void *obj)", cpyext=True) |
| 25 |
def HPy_FromPyObject(space, handles, ctx, obj): |
| 26 |
if not obj: |
| 27 |
return 0 |
| 28 |
w_obj = pyobject.from_ref(space, rffi.cast(pyobject.PyObject, obj)) |
| 29 |
return handles.new(w_obj) |
| 30 |
|
| 31 |
@API.func("void *HPy_AsPyObject(HPyContext *ctx, HPy h)", cpyext=True) |
| 32 |
def HPy_AsPyObject(space, handles, ctx, h): |
| 33 |
if not h: |
| 34 |
return rffi.cast(rffi.VOIDP, 0) |
| 35 |
w_obj = handles.deref(h) |
| 36 |
pyobj = pyobject.make_ref(space, w_obj) |
| 37 |
return rffi.cast(rffi.VOIDP, pyobj) |
| 38 |
|
| 39 |
@API.func("void *HPy_AsStruct_Legacy(HPyContext *ctx, HPy h)") |
| 40 |
def HPy_AsStruct_Legacy(space, handles, ctx, h): |
| 41 |
w_obj = handles.deref(h) |
| 42 |
storage = w_obj._hpy_get_raw_storage(space) |
| 43 |
if not storage: |
| 44 |
|
| 45 |
pyobj = pyobject.make_ref(space, w_obj) |
| 46 |
return rffi.cast(rffi.VOIDP, pyobj) |
| 47 |
else: |
| 48 |
pyobj = rffi.cast(pyobject.PyObject, storage) |
| 49 |
if pyobj.c_ob_refcnt > 0: |
| 50 |
|
| 51 |
|
| 52 |
pyobject.incref(space, pyobj) |
| 53 |
return storage |
| 54 |
|
| 55 |
|
| 56 |
@API.func("void ObjectFreeNOOP(void *)", cpyext=True, is_helper=True) |
| 57 |
def ObjectFreeNOOP(space, *args): |
| 58 |
pass |
| 59 |
|
| 60 |
@jit.dont_look_inside |
| 61 |
def create_pyobject_from_storage(space, w_obj, w_metatype=None, basicsize=0): |
| 62 |
|
| 63 |
storage = w_obj._hpy_get_raw_storage(space) |
| 64 |
w_type = space.type(w_obj) |
| 65 |
if pyobject.w_obj_has_pyobj(w_obj): |
| 66 |
raise oefmt(space.w_TypeError, |
| 67 |
"internal error: seeing a PyObject before one was expected") |
| 68 |
|
| 69 |
pyobject.as_pyobj(space, w_type) |
| 70 |
|
| 71 |
typedescr = pyobject.get_typedescr(w_obj.typedef) |
| 72 |
py_obj = rffi.cast(pyobject.PyObject, storage) |
| 73 |
if w_metatype: |
| 74 |
py_obj.c_ob_type = rffi.cast(PyTypeObjectPtr, pyobject.make_ref(space, w_metatype)) |
| 75 |
|
| 76 |
py_heaptype = rffi.cast(PyHeapTypeObject, py_obj) |
| 77 |
pto = py_heaptype.c_ht_type |
| 78 |
pto.c_tp_flags = rffi.cast(rffi.ULONG, Py_TPFLAGS_HEAPTYPE) |
| 79 |
pto.c_tp_as_async = py_heaptype.c_as_async |
| 80 |
pto.c_tp_as_number = py_heaptype.c_as_number |
| 81 |
pto.c_tp_as_sequence = py_heaptype.c_as_sequence |
| 82 |
pto.c_tp_as_mapping = py_heaptype.c_as_mapping |
| 83 |
pto.c_tp_as_buffer = py_heaptype.c_as_buffer |
| 84 |
pto.c_tp_itemsize = 0 |
| 85 |
pyobject.track_reference(space, py_obj, w_obj) |
| 86 |
typedescr.attach(space, py_obj, w_obj) |
| 87 |
|
| 88 |
if w_metatype: |
| 89 |
pto = rffi.cast(PyTypeObjectPtr, py_obj) |
| 90 |
pto.c_tp_basicsize = basicsize |
| 91 |
if basicsize: |
| 92 |
|
| 93 |
ll_objectfree = ObjectFreeNOOP.get_llhelper(space) |
| 94 |
pto.c_tp_free = ll_objectfree |
| 95 |
else: |
| 96 |
|
| 97 |
|
| 98 |
pto.c_tp_basicsize = rffi.sizeof(pyobject.PyObject.TO) |
| 99 |
return py_obj |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
def attach_legacy_methods(space, pymethods, w_obj, modname, type_name): |
| 106 |
""" |
| 107 |
pymethods is passed as a void*, but it is expected to be a PyMethodDef[]. |
| 108 |
Wrap its items into the proper cpyext.W_*Function objects, and attach them |
| 109 |
to w_obj (which can be either a module or a type). |
| 110 |
""" |
| 111 |
pymethods = cpyts.cast('PyMethodDef*', pymethods) |
| 112 |
dict_w = {} |
| 113 |
if modname: |
| 114 |
|
| 115 |
convert_method_defs(space, dict_w, pymethods, None, w_obj, modname, type_name) |
| 116 |
else: |
| 117 |
|
| 118 |
convert_method_defs(space, dict_w, pymethods, w_obj, w_obj, modname, type_name) |
| 119 |
for key, w_func in dict_w.items(): |
| 120 |
space.setattr(w_obj, space.newtext(key), w_func) |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
def attach_legacy_members(space, pymembers, w_type, type_name): |
| 126 |
PyMemberDef = cpyts.gettype('PyMemberDef') |
| 127 |
pymembers = rffi.cast(rffi.CArrayPtr(PyMemberDef), pymembers) |
| 128 |
if not pymembers: |
| 129 |
return |
| 130 |
i = 0 |
| 131 |
while True: |
| 132 |
pymember = pymembers[i] |
| 133 |
name = pymember.c_name |
| 134 |
if not name: |
| 135 |
break |
| 136 |
i += 1 |
| 137 |
name = rffi.constcharp2str(pymember.c_name) |
| 138 |
doc = rffi.constcharp2str(pymember.c_doc) if pymember.c_doc else None |
| 139 |
offset = rffi.cast(lltype.Signed, pymember.c_offset) |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
kind = rffi.cast(lltype.Signed, pymember.c_type) |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
flags = rffi.cast(lltype.Signed, pymember.c_flags) |
| 149 |
is_readonly = flags & structmemberdefs.READONLY |
| 150 |
w_member = W_HPyMemberDescriptor(w_type, kind, name, doc, offset, is_readonly) |
| 151 |
w_type.setdictvalue(space, name, w_member) |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
def check_descr(space, w_self, w_type): |
| 157 |
if not space.isinstance_w(w_self, w_type): |
| 158 |
raise DescrMismatch() |
| 159 |
|
| 160 |
|
| 161 |
class GettersAndSetters: |
| 162 |
def getter(self, space, w_self): |
| 163 |
assert isinstance(self, W_GetSetPropertyHPy) |
| 164 |
assert isinstance(w_self, W_HPyObject) |
| 165 |
check_descr(space, w_self, self.w_type) |
| 166 |
return generic_cpy_call( |
| 167 |
space, self.getset.c_get, w_self.get_pyobject(), |
| 168 |
self.getset.c_closure) |
| 169 |
|
| 170 |
def setter(self, space, w_self, w_value): |
| 171 |
assert isinstance(self, W_GetSetPropertyHPy) |
| 172 |
assert isinstance(w_self, W_HPyObject) |
| 173 |
assert isinstance(w_value, W_HPyObject) |
| 174 |
check_descr(space, w_self, self.w_type) |
| 175 |
res = generic_cpy_call( |
| 176 |
space, self.getset.c_set, |
| 177 |
w_self.get_pyobject(), w_value.get_pyobject(), |
| 178 |
self.getset.c_closure) |
| 179 |
if rffi.cast(lltype.Signed, res) < 0: |
| 180 |
state = space.fromcache(State) |
| 181 |
state.check_and_raise_exception() |
| 182 |
|
| 183 |
def deleter(self, space, w_self): |
| 184 |
assert isinstance(self, W_GetSetPropertyHPy) |
| 185 |
assert isinstance(w_self, W_HPyObject) |
| 186 |
check_descr(space, w_self, self.w_type) |
| 187 |
res = generic_cpy_call( |
| 188 |
space, self.getset.c_set, w_self.get_pyobject(), None, |
| 189 |
self.getset.c_closure) |
| 190 |
if rffi.cast(lltype.Signed, res) < 0: |
| 191 |
state = space.fromcache(State) |
| 192 |
state.check_and_raise_exception() |
| 193 |
|
| 194 |
|
| 195 |
class W_GetSetPropertyHPy(GetSetProperty): |
| 196 |
def __init__(self, getset, w_type): |
| 197 |
self.getset = getset |
| 198 |
self.w_type = w_type |
| 199 |
doc = fset = fget = fdel = None |
| 200 |
if doc: |
| 201 |
|
| 202 |
doc = rffi.constcharp2str(getset.c_doc) |
| 203 |
if getset.c_get: |
| 204 |
fget = GettersAndSetters.getter.im_func |
| 205 |
if getset.c_set: |
| 206 |
fset = GettersAndSetters.setter.im_func |
| 207 |
fdel = GettersAndSetters.deleter.im_func |
| 208 |
GetSetProperty.__init__(self, fget, fset, fdel, doc, |
| 209 |
cls=None, use_closure=True, |
| 210 |
tag="HPy_legacy") |
| 211 |
self.name = rffi.constcharp2str(getset.c_name) |
| 212 |
|
| 213 |
def readonly_attribute(self, space): |
| 214 |
raise oefmt(space.w_AttributeError, |
| 215 |
"attribute '%s' of '%N' objects is not writable", |
| 216 |
self.name, self.w_type) |
| 217 |
|
| 218 |
def attach_legacy_getsets(space, pygetsets, w_type): |
| 219 |
from pypy.module.cpyext.typeobjectdefs import PyGetSetDef |
| 220 |
getsets = rffi.cast(rffi.CArrayPtr(PyGetSetDef), pygetsets) |
| 221 |
if getsets: |
| 222 |
i = -1 |
| 223 |
while True: |
| 224 |
i = i + 1 |
| 225 |
getset = getsets[i] |
| 226 |
name = getset.c_name |
| 227 |
if not name: |
| 228 |
break |
| 229 |
name = rffi.constcharp2str(name) |
| 230 |
w_descr = W_GetSetPropertyHPy(getset, w_type) |
| 231 |
space.setattr(w_type, space.newtext(name), w_descr) |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
def make_slot_wrappers_table(): |
| 237 |
from pypy.module.cpyext.typeobject import SLOT_TABLE |
| 238 |
from pypy.module.cpyext.slotdefs import slotdefs |
| 239 |
table = [] |
| 240 |
for typeslot in slotdefs: |
| 241 |
|
| 242 |
if typeslot.slot_names[-1] in ('c_bf_getbuffer', |
| 243 |
'c_bf_getreadbuffer', |
| 244 |
'c_bf_getwritebuffer'): |
| 245 |
continue |
| 246 |
for num, membername, slotname, TARGET in SLOT_TABLE: |
| 247 |
if typeslot.slot_names[-1] == slotname: |
| 248 |
ts = typeslot |
| 249 |
table.append((num, ts.method_name, ts.doc, ts.wrapper_class)) |
| 250 |
break |
| 251 |
else: |
| 252 |
assert False, 'Cannot find slot num for typeslot %s' % typeslot.slot_name |
| 253 |
return table |
| 254 |
SLOT_WRAPPERS_TABLE = unrolling_iterable(make_slot_wrappers_table()) |
| 255 |
|
| 256 |
@jit.dont_look_inside |
| 257 |
def attach_legacy_slots_to_type(space, w_type, c_legacy_slots, needs_hpytype_dealloc): |
| 258 |
from pypy.module.cpyext.slotdefs import wrap_unaryfunc |
| 259 |
from pypy.module.cpyext.typeobjectdefs import newfunc, destructor, allocfunc, freefunc |
| 260 |
slotdefs = rffi.cast(rffi.CArrayPtr(cpyts.gettype('PyType_Slot')), c_legacy_slots) |
| 261 |
i = 0 |
| 262 |
type_name = w_type.getqualname(space) |
| 263 |
pytype = rffi.cast(PyTypeObjectPtr, pyobject.as_pyobj(space, w_type)) |
| 264 |
while True: |
| 265 |
slotdef = slotdefs[i] |
| 266 |
slotnum = rffi.cast(lltype.Signed, slotdef.c_slot) |
| 267 |
if slotnum == 0: |
| 268 |
break |
| 269 |
elif slotnum == cpyts.macros['Py_tp_methods']: |
| 270 |
attach_legacy_methods(space, slotdef.c_pfunc, w_type, None, type_name) |
| 271 |
elif slotnum == cpyts.macros['Py_tp_members']: |
| 272 |
attach_legacy_members(space, slotdef.c_pfunc, w_type, type_name) |
| 273 |
elif slotnum == cpyts.macros['Py_tp_getset']: |
| 274 |
attach_legacy_getsets(space, slotdef.c_pfunc, w_type) |
| 275 |
elif slotnum == cpyts.macros['Py_tp_dealloc']: |
| 276 |
if needs_hpytype_dealloc: |
| 277 |
raise oefmt(space.w_TypeError, |
| 278 |
"legacy tp_dealloc is incompatible with HPy_tp_traverse" |
| 279 |
" or HPy_tp_destroy.") |
| 280 |
|
| 281 |
w_type.has_tp_dealloc = True |
| 282 |
funcptr = slotdef.c_pfunc |
| 283 |
if not hasattr(space, 'is_fake_objspace'): |
| 284 |
|
| 285 |
pytype.c_tp_dealloc = rffi.cast(destructor, funcptr) |
| 286 |
|
| 287 |
elif slotnum == cpyts.macros['Py_tp_new']: |
| 288 |
funcptr = slotdef.c_pfunc |
| 289 |
pytype.c_tp_new = rffi.cast(newfunc, funcptr) |
| 290 |
elif slotnum == cpyts.macros['Py_tp_alloc']: |
| 291 |
funcptr = slotdef.c_pfunc |
| 292 |
pytype.c_tp_alloc = rffi.cast(allocfunc, funcptr) |
| 293 |
elif slotnum == cpyts.macros['Py_tp_free']: |
| 294 |
funcptr = slotdef.c_pfunc |
| 295 |
|
| 296 |
|
| 297 |
pytype.c_tp_free = rffi.cast(freefunc, funcptr) |
| 298 |
else: |
| 299 |
attach_legacy_slot(space, w_type, slotdef, slotnum, type_name) |
| 300 |
i += 1 |
| 301 |
|
| 302 |
def attach_legacy_slot(space, w_type, slotdef, slotnum, type_name): |
| 303 |
|
| 304 |
for num, method_name, doc, wrapper_class in SLOT_WRAPPERS_TABLE: |
| 305 |
if num == slotnum: |
| 306 |
if wrapper_class is None: |
| 307 |
|
| 308 |
raise oefmt(space.w_NotImplementedError, |
| 309 |
"slot wrapper for slot %d %s",num, method_name) |
| 310 |
funcptr = slotdef.c_pfunc |
| 311 |
w_wrapper = wrapper_class(space, w_type, method_name, doc, funcptr, type_name) |
| 312 |
w_type.setdictvalue(space, method_name, w_wrapper) |
| 313 |
break |
| 314 |
else: |
| 315 |
raise oefmt(space.w_NotImplementedError, |
| 316 |
'cannot find the slot %d when creating type %s', slotnum, type_name) |