本文整理汇总了Python中pypy.module.cpyext.pyobject.from_ref函数的典型用法代码示例。如果您正苦于以下问题:Python from_ref函数的具体用法?Python from_ref怎么用?Python from_ref使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了from_ref函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: PyString_AsStringAndSize
def PyString_AsStringAndSize(space, ref, buffer, length):
if not PyString_Check(space, ref):
from pypy.module.cpyext.unicodeobject import (
PyUnicode_Check, _PyUnicode_AsDefaultEncodedString)
if PyUnicode_Check(space, ref):
ref = _PyUnicode_AsDefaultEncodedString(space, ref, lltype.nullptr(rffi.CCHARP.TO))
else:
raise oefmt(space.w_TypeError,
"expected string or Unicode object, %T found",
from_ref(space, ref))
ref_str = rffi.cast(PyStringObject, ref)
if not ref_str.c_buffer:
# copy string buffer
w_str = from_ref(space, ref)
s = space.str_w(w_str)
ref_str.c_buffer = rffi.str2charp(s)
buffer[0] = ref_str.c_buffer
if length:
length[0] = ref_str.c_size
else:
i = 0
while ref_str.c_buffer[i] != '\0':
i += 1
if i != ref_str.c_size:
raise OperationError(space.w_TypeError, space.wrap(
"expected string without null bytes"))
return 0
示例2: test_iterkeys
def test_iterkeys(self, space, api):
w_dict = space.sys.getdict(space)
py_dict = make_ref(space, w_dict)
ppos = lltype.malloc(Py_ssize_tP.TO, 1, flavor='raw')
pkey = lltype.malloc(PyObjectP.TO, 1, flavor='raw')
pvalue = lltype.malloc(PyObjectP.TO, 1, flavor='raw')
keys_w = []
values_w = []
try:
ppos[0] = 0
while api.PyDict_Next(w_dict, ppos, pkey, None):
w_key = from_ref(space, pkey[0])
keys_w.append(w_key)
ppos[0] = 0
while api.PyDict_Next(w_dict, ppos, None, pvalue):
w_value = from_ref(space, pvalue[0])
values_w.append(w_value)
finally:
lltype.free(ppos, flavor='raw')
lltype.free(pkey, flavor='raw')
lltype.free(pvalue, flavor='raw')
api.Py_DecRef(py_dict) # release borrowed references
assert space.eq_w(space.newlist(keys_w),
space.call_method(w_dict, "keys"))
assert space.eq_w(space.newlist(values_w),
space.call_method(w_dict, "values"))
示例3: PyString_Size
def PyString_Size(space, ref):
if from_ref(space, rffi.cast(PyObject, ref.c_ob_type)) is space.w_str:
ref = rffi.cast(PyStringObject, ref)
return ref.c_size
else:
w_obj = from_ref(space, ref)
return space.len_w(w_obj)
示例4: PyUnicode_GetSize
def PyUnicode_GetSize(space, ref):
if from_ref(space, rffi.cast(PyObject, ref.c_ob_type)) is space.w_unicode:
ref = rffi.cast(PyUnicodeObject, ref)
return ref.c_length
else:
w_obj = from_ref(space, ref)
return space.len_w(w_obj)
示例5: test_tuple_resize
def test_tuple_resize(self, space, api):
w_42 = space.wrap(42)
ar = lltype.malloc(PyObjectP.TO, 1, flavor='raw')
py_tuple = api.PyTuple_New(3)
# inside py_tuple is an array of "PyObject *" items which each hold
# a reference
rffi.cast(PyTupleObject, py_tuple).c_ob_item[0] = make_ref(space, w_42)
ar[0] = py_tuple
api._PyTuple_Resize(ar, 2)
w_tuple = from_ref(space, ar[0])
assert space.int_w(space.len(w_tuple)) == 2
assert space.int_w(space.getitem(w_tuple, space.wrap(0))) == 42
api.Py_DecRef(ar[0])
py_tuple = api.PyTuple_New(3)
rffi.cast(PyTupleObject, py_tuple).c_ob_item[0] = make_ref(space, w_42)
ar[0] = py_tuple
api._PyTuple_Resize(ar, 10)
w_tuple = from_ref(space, ar[0])
assert space.int_w(space.len(w_tuple)) == 10
assert space.int_w(space.getitem(w_tuple, space.wrap(0))) == 42
api.Py_DecRef(ar[0])
lltype.free(ar, flavor='raw')
示例6: test_fsconverter
def test_fsconverter(self, space, api):
# Input is bytes
w_input = space.wrapbytes("test")
with lltype.scoped_alloc(PyObjectP.TO, 1) as result:
# Decoder
ret = api.PyUnicode_FSDecoder(w_input, result)
assert ret == Py_CLEANUP_SUPPORTED
assert space.isinstance_w(from_ref(space, result[0]), space.w_unicode)
assert api.PyUnicode_FSDecoder(None, result) == 1
# Converter
ret = api.PyUnicode_FSConverter(w_input, result)
assert ret == Py_CLEANUP_SUPPORTED
assert space.eq_w(from_ref(space, result[0]), w_input)
assert api.PyUnicode_FSDecoder(None, result) == 1
# Input is unicode
w_input = space.wrap("test")
with lltype.scoped_alloc(PyObjectP.TO, 1) as result:
# Decoder
ret = api.PyUnicode_FSDecoder(w_input, result)
assert ret == Py_CLEANUP_SUPPORTED
assert space.eq_w(from_ref(space, result[0]), w_input)
assert api.PyUnicode_FSDecoder(None, result) == 1
# Converter
ret = api.PyUnicode_FSConverter(w_input, result)
assert ret == Py_CLEANUP_SUPPORTED
assert space.isinstance_w(from_ref(space, result[0]), space.w_bytes)
assert api.PyUnicode_FSDecoder(None, result) == 1
示例7: test_traceback
def test_traceback(self, space, api):
w_traceback = space.appexec(
[],
"""():
import sys
try:
1/0
except:
return sys.exc_info()[2]
""",
)
py_obj = make_ref(space, w_traceback)
py_traceback = rffi.cast(PyTracebackObject, py_obj)
assert from_ref(space, rffi.cast(PyObject, py_traceback.c_ob_type)) is space.gettypeobject(PyTraceback.typedef)
traceback = space.interp_w(PyTraceback, w_traceback)
assert traceback.lasti == py_traceback.c_tb_lasti
assert traceback.get_lineno() == py_traceback.c_tb_lineno
assert space.eq_w(space.getattr(w_traceback, space.wrap("tb_lasti")), space.wrap(py_traceback.c_tb_lasti))
assert space.is_w(
space.getattr(w_traceback, space.wrap("tb_frame")),
from_ref(space, rffi.cast(PyObject, py_traceback.c_tb_frame)),
)
while not space.is_w(w_traceback, space.w_None):
assert space.is_w(w_traceback, from_ref(space, rffi.cast(PyObject, py_traceback)))
w_traceback = space.getattr(w_traceback, space.wrap("tb_next"))
py_traceback = py_traceback.c_tb_next
assert lltype.normalizeptr(py_traceback) is None
api.Py_DecRef(py_obj)
示例8: PyString_AsString
def PyString_AsString(space, ref):
if from_ref(space, rffi.cast(PyObject, ref.c_ob_type)) is space.w_str:
pass # typecheck returned "ok" without forcing 'ref' at all
elif not PyString_Check(space, ref): # otherwise, use the alternate way
raise OperationError(space.w_TypeError, space.wrap(
"PyString_AsString only support strings"))
ref_str = rffi.cast(PyStringObject, ref)
if not ref_str.c_buffer:
# copy string buffer
w_str = from_ref(space, ref)
s = space.str_w(w_str)
ref_str.c_buffer = rffi.str2charp(s)
return ref_str.c_buffer
示例9: PyErr_NormalizeException
def PyErr_NormalizeException(space, exc_p, val_p, tb_p):
"""Under certain circumstances, the values returned by PyErr_Fetch() below
can be "unnormalized", meaning that *exc is a class object but *val is
not an instance of the same class. This function can be used to instantiate
the class in that case. If the values are already normalized, nothing happens.
The delayed normalization is implemented to improve performance."""
operr = OperationError(from_ref(space, exc_p[0]),
from_ref(space, val_p[0]))
operr.normalize_exception(space)
Py_DecRef(space, exc_p[0])
Py_DecRef(space, val_p[0])
exc_p[0] = make_ref(space, operr.w_type)
val_p[0] = make_ref(space, operr.get_w_value(space))
示例10: test_tuple_resize
def test_tuple_resize(self, space, api):
py_tuple = api.PyTuple_New(3)
ar = lltype.malloc(PyObjectP.TO, 1, flavor='raw')
ar[0] = rffi.cast(PyObject, make_ref(space, py_tuple))
api._PyTuple_Resize(ar, 2)
py_tuple = from_ref(space, ar[0])
assert space.int_w(space.len(py_tuple)) == 2
api._PyTuple_Resize(ar, 10)
py_tuple = from_ref(space, ar[0])
assert space.int_w(space.len(py_tuple)) == 10
api.Py_DecRef(ar[0])
lltype.free(ar, flavor='raw')
示例11: _PyString_AsString
def _PyString_AsString(space, ref):
if from_ref(space, rffi.cast(PyObject, ref.c_ob_type)) is space.w_str:
pass # typecheck returned "ok" without forcing 'ref' at all
elif not PyString_Check(space, ref): # otherwise, use the alternate way
from pypy.module.cpyext.unicodeobject import (
PyUnicode_Check, _PyUnicode_AsDefaultEncodedString)
if PyUnicode_Check(space, ref):
ref = _PyUnicode_AsDefaultEncodedString(space, ref, lltype.nullptr(rffi.CCHARP.TO))
else:
raise oefmt(space.w_TypeError,
"expected string or Unicode object, %T found",
from_ref(space, ref))
ref_str = rffi.cast(PyBytesObject, ref)
return ref_str.c_ob_sval
示例12: PyMember_GetOne
def PyMember_GetOne(space, obj, w_member):
addr = rffi.cast(ADDR, obj)
addr += w_member.c_offset
member_type = rffi.cast(lltype.Signed, w_member.c_type)
for converter in integer_converters:
typ, lltyp, _ = converter
if typ == member_type:
result = rffi.cast(rffi.CArrayPtr(lltyp), addr)
if lltyp is rffi.FLOAT:
w_result = space.wrap(lltype.cast_primitive(lltype.Float,
result[0]))
elif typ == T_BOOL:
x = rffi.cast(lltype.Signed, result[0])
w_result = space.wrap(x != 0)
else:
w_result = space.wrap(result[0])
return w_result
if member_type == T_STRING:
result = rffi.cast(rffi.CCHARPP, addr)
if result[0]:
w_result = PyString_FromString(space, result[0])
else:
w_result = space.w_None
elif member_type == T_STRING_INPLACE:
result = rffi.cast(rffi.CCHARP, addr)
w_result = PyString_FromString(space, result)
elif member_type == T_CHAR:
result = rffi.cast(rffi.CCHARP, addr)
w_result = space.wrap(result[0])
elif member_type == T_OBJECT:
obj_ptr = rffi.cast(PyObjectP, addr)
if obj_ptr[0]:
w_result = from_ref(space, obj_ptr[0])
else:
w_result = space.w_None
elif member_type == T_OBJECT_EX:
obj_ptr = rffi.cast(PyObjectP, addr)
if obj_ptr[0]:
w_result = from_ref(space, obj_ptr[0])
else:
w_name = space.wrap(rffi.charp2str(w_member.c_name))
raise OperationError(space.w_AttributeError, w_name)
else:
raise OperationError(space.w_SystemError,
space.wrap("bad memberdescr type"))
return w_result
示例13: finish_type_2
def finish_type_2(space, pto, w_obj):
"""
Sets up other attributes, when the interpreter type has been created.
"""
pto.c_tp_mro = make_ref(space, space.newtuple(w_obj.mro_w))
base = pto.c_tp_base
if base:
inherit_special(space, pto, base)
for w_base in space.fixedview(from_ref(space, pto.c_tp_bases)):
inherit_slots(space, pto, w_base)
if not pto.c_tp_setattro:
from pypy.module.cpyext.object import PyObject_GenericSetAttr
pto.c_tp_setattro = llhelper(
PyObject_GenericSetAttr.api_func.functype,
PyObject_GenericSetAttr.api_func.get_wrapper(space))
if not pto.c_tp_getattro:
from pypy.module.cpyext.object import PyObject_GenericGetAttr
pto.c_tp_getattro = llhelper(
PyObject_GenericGetAttr.api_func.functype,
PyObject_GenericGetAttr.api_func.get_wrapper(space))
if w_obj.is_cpytype():
Py_DecRef(space, pto.c_tp_dict)
w_dict = w_obj.getdict(space)
pto.c_tp_dict = make_ref(space, w_dict)
示例14: __init__
def __init__(self, space, pto):
bases_w = space.fixedview(from_ref(space, pto.c_tp_bases))
dict_w = {}
add_operators(space, dict_w, pto)
convert_method_defs(space, dict_w, pto.c_tp_methods, self)
convert_getset_defs(space, dict_w, pto.c_tp_getset, self)
convert_member_defs(space, dict_w, pto.c_tp_members, self)
name = rffi.charp2str(pto.c_tp_name)
flag_heaptype = pto.c_tp_flags & Py_TPFLAGS_HEAPTYPE
if flag_heaptype:
minsize = rffi.sizeof(PyHeapTypeObject.TO)
else:
minsize = rffi.sizeof(PyObject.TO)
new_layout = (pto.c_tp_basicsize > minsize or pto.c_tp_itemsize > 0)
W_TypeObject.__init__(self, space, name,
bases_w or [space.w_object], dict_w, force_new_layout=new_layout,
is_heaptype=flag_heaptype)
self.flag_cpytype = True
# if a sequence or a mapping, then set the flag to force it
if pto.c_tp_as_sequence and pto.c_tp_as_sequence.c_sq_item:
self.flag_map_or_seq = 'S'
elif (pto.c_tp_as_mapping and pto.c_tp_as_mapping.c_mp_subscript and
not (pto.c_tp_as_sequence and pto.c_tp_as_sequence.c_sq_slice)):
self.flag_map_or_seq = 'M'
if pto.c_tp_doc:
self.w_doc = space.wrap(rffi.charp2str(pto.c_tp_doc))
示例15: frame_realize
def frame_realize(space, py_obj):
"""
Creates the frame in the interpreter. The PyFrameObject structure must not
be modified after this call.
"""
py_frame = rffi.cast(PyFrameObject, py_obj)
py_code = rffi.cast(PyObject, py_frame.c_f_code)
w_code = from_ref(space, py_code)
code = space.interp_w(PyCode, w_code)
w_globals = from_ref(space, py_frame.c_f_globals)
frame = space.FrameClass(space, code, w_globals, closure=None)
frame.f_lineno = py_frame.c_f_lineno
w_obj = space.wrap(frame)
track_reference(space, py_obj, w_obj)
return w_obj