本文整理汇总了Python中pypy.module.cpyext.pyobject.from_ref函数的典型用法代码示例。如果您正苦于以下问题:Python from_ref函数的具体用法?Python from_ref怎么用?Python from_ref使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了from_ref函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
开发者ID:abhinavthomas,项目名称:pypy,代码行数:27,代码来源:bytesobject.py
示例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"))
开发者ID:mozillazg,项目名称:pypy,代码行数:30,代码来源:test_dictobject.py
示例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)
开发者ID:Darriall,项目名称:pypy,代码行数:7,代码来源:stringobject.py
示例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)
开发者ID:mozillazg,项目名称:pypy,代码行数:7,代码来源:unicodeobject.py
示例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')
开发者ID:cimarieta,项目名称:usp,代码行数:25,代码来源:test_tupleobject.py
示例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
开发者ID:Qointum,项目名称:pypy,代码行数:27,代码来源:test_unicodeobject.py
示例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)
开发者ID:cimarieta,项目名称:usp,代码行数:32,代码来源:test_traceback.py
示例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
开发者ID:Darriall,项目名称:pypy,代码行数:13,代码来源:stringobject.py
示例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))
开发者ID:Qointum,项目名称:pypy,代码行数:13,代码来源:pyerrors.py
示例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')
开发者ID:Debug-Orz,项目名称:Sypy,代码行数:14,代码来源:test_tupleobject.py
示例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
开发者ID:mozillazg,项目名称:pypy,代码行数:14,代码来源:bytesobject.py
示例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
开发者ID:cimarieta,项目名称:usp,代码行数:48,代码来源:structmember.py
示例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)
开发者ID:abhinavthomas,项目名称:pypy,代码行数:27,代码来源:typeobject.py
示例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))
开发者ID:mozillazg,项目名称:pypy,代码行数:29,代码来源:typeobject.py
示例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
开发者ID:gorakhargosh,项目名称:pypy,代码行数:16,代码来源:frameobject.py
示例16: 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)
return w_obj
开发者ID:cimarieta,项目名称:usp,代码行数:7,代码来源:intobject.py
示例17: getitems
def getitems(self, w_list):
# called when switching list strategy, so convert storage
storage = self.unerase(w_list.lstorage)
retval = [None] * storage._length
for i in range(storage._length):
retval[i] = from_ref(w_list.space, storage._elems[i])
return retval
开发者ID:mozillazg,项目名称:pypy,代码行数:7,代码来源:sequence.py
示例18: _type_realize
def _type_realize(space, py_obj):
"""
Creates an interpreter type from a PyTypeObject structure.
"""
# missing:
# inheriting tp_as_* slots
# unsupported:
# tp_mro, tp_subclasses
py_type = rffi.cast(PyTypeObjectPtr, py_obj)
if not py_type.c_tp_base:
# borrowed reference, but w_object is unlikely to disappear
base = make_ref(space, space.w_object)
Py_DecRef(space, base)
py_type.c_tp_base = rffi.cast(PyTypeObjectPtr, base)
finish_type_1(space, py_type)
w_metatype = from_ref(space, rffi.cast(PyObject, py_type.c_ob_type))
w_obj = space.allocate_instance(W_PyCTypeObject, w_metatype)
track_reference(space, py_obj, w_obj)
w_obj.__init__(space, py_type)
w_obj.ready()
finish_type_2(space, py_type, w_obj)
state = space.fromcache(RefcountState)
state.non_heaptypes_w.append(w_obj)
return w_obj
开发者ID:Debug-Orz,项目名称:Sypy,代码行数:31,代码来源:typeobject.py
示例19: _PyTuple_Resize
def _PyTuple_Resize(space, ref, newsize):
"""Can be used to resize a tuple. newsize will be the new length of the tuple.
Because tuples are supposed to be immutable, this should only be used if there
is only one reference to the object. Do not use this if the tuple may already
be known to some other part of the code. The tuple will always grow or shrink
at the end. Think of this as destroying the old tuple and creating a new one,
only more efficiently. Returns 0 on success. Client code should never
assume that the resulting value of *p will be the same as before calling
this function. If the object referenced by *p is replaced, the original
*p is destroyed. On failure, returns -1 and sets *p to NULL, and
raises MemoryError or SystemError."""
py_tuple = from_ref(space, ref[0])
if not PyTuple_Check(space, py_tuple):
PyErr_BadInternalCall(space)
py_newtuple = PyTuple_New(space, newsize)
to_cp = newsize
oldsize = space.int_w(space.len(py_tuple))
if oldsize < newsize:
to_cp = oldsize
for i in range(to_cp):
_setitem_tuple(py_newtuple, i, space.getitem(py_tuple, space.wrap(i)))
Py_DecRef(space, ref[0])
ref[0] = make_ref(space, py_newtuple)
return 0
开发者ID:Darriall,项目名称:pypy,代码行数:25,代码来源:tupleobject.py
示例20: PyUnicode_AsUnicode
def PyUnicode_AsUnicode(space, ref):
"""Return a read-only pointer to the Unicode object's internal Py_UNICODE
buffer, NULL if unicode is not a Unicode object."""
# Don't use PyUnicode_Check, it will realize the object :-(
w_type = from_ref(space, rffi.cast(PyObject, ref.c_ob_type))
if not space.issubtype_w(w_type, space.w_unicode):
raise oefmt(space.w_TypeError, "expected unicode object")
return PyUnicode_AS_UNICODE(space, rffi.cast(rffi.VOIDP, ref))
开发者ID:mozillazg,项目名称:pypy,代码行数:8,代码来源:unicodeobject.py
注:本文中的pypy.module.cpyext.pyobject.from_ref函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论