@slot_function([PyObject], lltype.Void) def dict_dealloc(space, py_obj): py_dict = rffi.cast(PyDictObject, py_obj) decref(space, py_dict.c__tmpkeys) py_dict.c__tmpkeys = lltype.nullptr(PyObject.TO) _dealloc(space, py_obj) @cpython_api([], PyObject) def PyDict_New(space): return space.newdict() PyDict_Check, PyDict_CheckExact = build_type_checkers_flags("Dict") @cpython_api([PyObject, PyObject], PyObject, error=CANNOT_FAIL, result_borrowed=True) def PyDict_GetItem(space, w_dict, w_key): if not isinstance(w_dict, W_DictMultiObject): return None # NOTE: this works so far because all our dict strategies store # *values* as full objects, which stay alive as long as the dict is # alive and not modified. So we can return a borrowed ref. # XXX this is wrong with IntMutableCell. Hope it works... return w_dict.getitem(w_key)
track_reference, decref, as_pyobj) from pypy.module.cpyext.slotdefs import (slotdefs_for_tp_slots, slotdefs_for_wrappers, get_slot_tp_function, llslot) from pypy.module.cpyext.state import State from pypy.module.cpyext.structmember import PyMember_GetOne, PyMember_SetOne from pypy.module.cpyext.typeobjectdefs import (PyGetSetDef, PyMemberDef, PyMappingMethods, PyNumberMethods, PySequenceMethods, PyBufferProcs) from pypy.objspace.std.typeobject import W_TypeObject, find_best_base #WARN_ABOUT_MISSING_SLOT_FUNCTIONS = False PyType_Check, PyType_CheckExact = build_type_checkers_flags("Type") PyHeapTypeObject = cts.gettype('PyHeapTypeObject *') class W_GetSetPropertyEx(GetSetProperty): def __init__(self, getset, w_type): self.getset = getset self.w_type = w_type doc = fset = fget = fdel = None if doc: # XXX dead code? doc = rffi.charp2str(getset.c_doc) if getset.c_get: fget = GettersAndSetters.getter.im_func if getset.c_set:
def init_unicodeobject(space): make_typedescr(space.w_unicode.layout.typedef, basestruct=PyUnicodeObject.TO, attach=unicode_attach, dealloc=unicode_dealloc, realize=unicode_realize) # Buffer for the default encoding (used by PyUnicde_GetDefaultEncoding) DEFAULT_ENCODING_SIZE = 100 default_encoding = lltype.malloc(rffi.CCHARP.TO, DEFAULT_ENCODING_SIZE, flavor='raw', zero=True) PyUnicode_Check, PyUnicode_CheckExact = build_type_checkers_flags("Unicode") def new_empty_unicode(space, length): """ Allocate a PyUnicodeObject and its buffer, but without a corresponding interpreter object. The buffer may be mutated, until unicode_realize() is called. Refcount of the result is 1. """ typedescr = get_typedescr(space.w_unicode.layout.typedef) py_obj = typedescr.allocate(space, space.w_unicode) py_uni = rffi.cast(PyUnicodeObject, py_obj) buflen = length + 1 py_uni.c_length = length py_uni.c_str = lltype.malloc(rffi.CWCHARP.TO,
PyBytesObjectFields = PyVarObjectFields + \ (("ob_shash", rffi.LONG), ("ob_sstate", rffi.INT), ("ob_sval", rffi.CArray(lltype.Char))) cpython_struct("PyBytesObject", PyBytesObjectFields, PyBytesObjectStruct) @bootstrap_function def init_bytesobject(space): "Type description of PyBytesObject" make_typedescr(space.w_bytes.layout.typedef, basestruct=PyBytesObject.TO, attach=bytes_attach, dealloc=bytes_dealloc, realize=bytes_realize) PyBytes_Check, PyBytes_CheckExact = build_type_checkers_flags( "Bytes", "w_bytes") def new_empty_str(space, length): """ Allocate a PyBytesObject and its ob_sval, but without a corresponding interpreter object. The ob_sval may be mutated, until bytes_realize() is called. Refcount of the result is 1. """ typedescr = get_typedescr(space.w_bytes.layout.typedef) py_obj = typedescr.allocate(space, space.w_bytes, length) py_str = rffi.cast(PyBytesObject, py_obj) py_str.c_ob_shash = -1 py_str.c_ob_sstate = rffi.cast(rffi.INT, 0) # SSTATE_NOT_INTERNED return py_str
value must not be modified. """ py_int = rffi.cast(PyIntObject, py_obj) py_int.c_ob_ival = space.int_w(w_obj) 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) return w_obj PyInt_Check, PyInt_CheckExact = build_type_checkers_flags("Int") @cpython_api([], lltype.Signed, error=CANNOT_FAIL) def PyInt_GetMax(space): """Return the system's idea of the largest integer it can handle (LONG_MAX, as defined in the system header files).""" return sys.maxint @cpython_api([lltype.Signed], PyObject) def PyInt_FromLong(space, ival): """Create a new integer object with a value of ival. """ return space.newint(ival)
from rpython.rtyper.lltypesystem import lltype, rffi from pypy.module.cpyext.api import (cpython_api, PyObject, build_type_checkers_flags, Py_ssize_t, CONST_STRING, ADDR, CANNOT_FAIL, INTP_real) from pypy.objspace.std.longobject import W_LongObject from pypy.interpreter.error import OperationError, oefmt from pypy.module.cpyext.intobject import PyInt_AsUnsignedLongMask from rpython.rlib.rbigint import rbigint, InvalidSignednessError PyLong_Check, PyLong_CheckExact = build_type_checkers_flags("Long") @cpython_api([lltype.Signed], PyObject) def PyLong_FromLong(space, val): """Return a new PyLongObject object from v, or NULL on failure.""" return space.newlong(val) @cpython_api([Py_ssize_t], PyObject) def PyLong_FromSsize_t(space, val): """Return a new PyLongObject object from a C Py_ssize_t, or NULL on failure. """ return space.newlong(val) @cpython_api([rffi.SIZE_T], PyObject) def PyLong_FromSize_t(space, val): """Return a new PyLongObject object from a C size_t, or NULL on failure. """
cpython_struct("PyTupleObject", PyTupleObjectFields, PyTupleObjectStruct) @bootstrap_function def init_tupleobject(space): "Type description of PyTupleObject" state = space.fromcache(State) make_typedescr(space.w_tuple.layout.typedef, basestruct=PyTupleObject.TO, attach=tuple_attach, alloc=tuple_alloc, dealloc=state.C._PyPy_tuple_dealloc, realize=tuple_realize) PyTuple_Check, PyTuple_CheckExact = build_type_checkers_flags("Tuple") def tuple_check_ref(space, ref): w_type = from_ref(space, rffi.cast(PyObject, ref.c_ob_type)) return (w_type is space.w_tuple or space.issubtype_w(w_type, space.w_tuple)) _BAD_ITEMCOUNT = None # patched in test_badinternalcall_from_rpy def tuple_alloc(typedescr, space, w_type, itemcount): state = space.fromcache(State) if w_type is space.w_tuple: if not we_are_translated() and itemcount == _BAD_ITEMCOUNT:
PyBytesObjectFields = PyVarObjectFields + \ (("ob_shash", rffi.LONG), ("ob_sstate", rffi.INT), ("ob_sval", rffi.CArray(lltype.Char))) cpython_struct("PyStringObject", PyBytesObjectFields, PyBytesObjectStruct) @bootstrap_function def init_bytesobject(space): "Type description of PyBytesObject" make_typedescr(space.w_bytes.layout.typedef, basestruct=PyBytesObject.TO, attach=bytes_attach, dealloc=bytes_dealloc, realize=bytes_realize) PyString_Check, PyString_CheckExact = build_type_checkers_flags( "String", "w_bytes") def new_empty_str(space, length): """ Allocate a PyBytesObject and its ob_sval, but without a corresponding interpreter object. The ob_sval may be mutated, until bytes_realize() is called. Refcount of the result is 1. """ typedescr = get_typedescr(space.w_bytes.layout.typedef) py_obj = typedescr.allocate(space, space.w_bytes, length) py_str = rffi.cast(PyBytesObject, py_obj) py_str.c_ob_shash = -1 py_str.c_ob_sstate = rffi.cast(rffi.INT, 0) # SSTATE_NOT_INTERNED return py_str
from rpython.rlib.objectmodel import always_inline from rpython.rtyper.lltypesystem import rffi, lltype from pypy.module.cpyext.api import (cpython_api, CANNOT_FAIL, Py_ssize_t, build_type_checkers_flags) from pypy.module.cpyext.pyerrors import PyErr_BadInternalCall from pypy.module.cpyext.pyobject import decref, incref, PyObject, make_ref from pypy.objspace.std.listobject import W_ListObject from pypy.interpreter.error import oefmt PyList_Check, PyList_CheckExact = build_type_checkers_flags("List") @cpython_api([Py_ssize_t], PyObject) def PyList_New(space, len): """Return a new list of length len on success, or NULL on failure. If length is greater than zero, the returned list object's items are set to NULL. Thus you cannot use abstract API functions such as PySequence_SetItem() or expose the object to Python code before setting all items to a real object with PyList_SetItem(). """ w_list = space.newlist([None] * len) #w_list.convert_to_cpy_strategy(space) return w_list @always_inline def get_list_storage(space, w_list): from pypy.module.cpyext.sequence import CPyListStrategy w_list.convert_to_cpy_strategy(space) return CPyListStrategy.unerase(w_list.lstorage)