本文整理汇总了Python中pypy.module.cpyext.pyobject.make_ref函数的典型用法代码示例。如果您正苦于以下问题:Python make_ref函数的具体用法?Python make_ref怎么用?Python make_ref使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了make_ref函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: 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
示例2: 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
示例3: frame_attach
def frame_attach(space, py_obj, w_obj):
"Fills a newly allocated PyFrameObject with a frame object"
frame = space.interp_w(PyFrame, w_obj)
py_frame = rffi.cast(PyFrameObject, py_obj)
py_frame.c_f_code = rffi.cast(PyCodeObject, make_ref(space, frame.pycode))
py_frame.c_f_globals = make_ref(space, frame.w_globals)
rffi.setintfield(py_frame, 'c_f_lineno', frame.f_lineno)
开发者ID:gorakhargosh,项目名称:pypy,代码行数:7,代码来源:frameobject.py
示例4: PyErr_GetExcInfo
def PyErr_GetExcInfo(space, ptype, pvalue, ptraceback):
"""---Cython extension---
Retrieve the exception info, as known from ``sys.exc_info()``. This
refers to an exception that was already caught, not to an exception
that was freshly raised. Returns new references for the three
objects, any of which may be *NULL*. Does not modify the exception
info state.
.. note::
This function is not normally used by code that wants to handle
exceptions. Rather, it can be used when code needs to save and
restore the exception state temporarily. Use
:c:func:`PyErr_SetExcInfo` to restore or clear the exception
state.
"""
ec = space.getexecutioncontext()
operror = ec.sys_exc_info()
if operror:
ptype[0] = make_ref(space, operror.w_type)
pvalue[0] = make_ref(space, operror.get_w_value(space))
ptraceback[0] = make_ref(space, space.wrap(operror.get_traceback()))
else:
ptype[0] = lltype.nullptr(PyObject.TO)
pvalue[0] = lltype.nullptr(PyObject.TO)
ptraceback[0] = lltype.nullptr(PyObject.TO)
开发者ID:Qointum,项目名称:pypy,代码行数:27,代码来源:pyerrors.py
示例5: test_setitem
def test_setitem(self, space, api):
py_tuple = api.PyTuple_New(2)
api.PyTuple_SetItem(py_tuple, 0, make_ref(space, space.wrap(42)))
api.PyTuple_SetItem(py_tuple, 1, make_ref(space, space.wrap(43)))
w_tuple = from_ref(space, py_tuple)
assert space.eq_w(w_tuple, space.newtuple([space.wrap(42),
space.wrap(43)]))
开发者ID:mozillazg,项目名称:pypy,代码行数:8,代码来源:test_tupleobject.py
示例6: PyFrame_New
def PyFrame_New(space, tstate, w_code, w_globals, w_locals):
typedescr = get_typedescr(PyFrame.typedef)
py_obj = typedescr.allocate(space, space.gettypeobject(PyFrame.typedef))
py_frame = rffi.cast(PyFrameObject, py_obj)
space.interp_w(PyCode, w_code) # sanity check
py_frame.c_f_code = rffi.cast(PyCodeObject, make_ref(space, w_code))
py_frame.c_f_globals = make_ref(space, w_globals)
return py_frame
开发者ID:gorakhargosh,项目名称:pypy,代码行数:8,代码来源:frameobject.py
示例7: type_attach
def type_attach(space, py_obj, w_type):
"""
Fills a newly allocated PyTypeObject from an existing type.
"""
from pypy.module.cpyext.object import PyObject_Del
assert isinstance(w_type, W_TypeObject)
pto = rffi.cast(PyTypeObjectPtr, py_obj)
typedescr = get_typedescr(w_type.layout.typedef)
# dealloc
pto.c_tp_dealloc = typedescr.get_dealloc(space)
# buffer protocol
if space.is_w(w_type, space.w_str):
setup_string_buffer_procs(space, pto)
if space.is_w(w_type, space.w_buffer):
setup_buffer_buffer_procs(space, pto)
pto.c_tp_free = llhelper(PyObject_Del.api_func.functype,
PyObject_Del.api_func.get_wrapper(space))
pto.c_tp_alloc = llhelper(PyType_GenericAlloc.api_func.functype,
PyType_GenericAlloc.api_func.get_wrapper(space))
if pto.c_tp_flags & Py_TPFLAGS_HEAPTYPE:
w_typename = space.getattr(w_type, space.wrap('__name__'))
heaptype = rffi.cast(PyHeapTypeObject, pto)
heaptype.c_ht_name = make_ref(space, w_typename)
from pypy.module.cpyext.bytesobject import PyString_AsString
pto.c_tp_name = PyString_AsString(space, heaptype.c_ht_name)
else:
pto.c_tp_name = rffi.str2charp(w_type.name)
# uninitialized fields:
# c_tp_print, c_tp_getattr, c_tp_setattr
# XXX implement
# c_tp_compare and the following fields (see http://docs.python.org/c-api/typeobj.html )
w_base = best_base(space, w_type.bases_w)
pto.c_tp_base = rffi.cast(PyTypeObjectPtr, make_ref(space, w_base))
builder = space.fromcache(StaticObjectBuilder)
if builder.cpyext_type_init is not None:
builder.cpyext_type_init.append((pto, w_type))
else:
finish_type_1(space, pto)
finish_type_2(space, pto, w_type)
pto.c_tp_basicsize = rffi.sizeof(typedescr.basestruct)
if pto.c_tp_base:
if pto.c_tp_base.c_tp_basicsize > pto.c_tp_basicsize:
pto.c_tp_basicsize = pto.c_tp_base.c_tp_basicsize
# will be filled later on with the correct value
# may not be 0
if space.is_w(w_type, space.w_object):
pto.c_tp_new = rffi.cast(newfunc, 1)
update_all_slots(space, w_type, pto)
pto.c_tp_flags |= Py_TPFLAGS_READY
return pto
开发者ID:abhinavthomas,项目名称:pypy,代码行数:58,代码来源:typeobject.py
示例8: slice_attach
def slice_attach(space, py_obj, w_obj):
"""
Fills a newly allocated PySliceObject with the given slice object. The
fields must not be modified.
"""
py_slice = rffi.cast(PySliceObject, py_obj)
assert isinstance(w_obj, W_SliceObject)
py_slice.c_start = make_ref(space, w_obj.w_start)
py_slice.c_stop = make_ref(space, w_obj.w_stop)
py_slice.c_step = make_ref(space, w_obj.w_step)
开发者ID:Qointum,项目名称:pypy,代码行数:10,代码来源:sliceobject.py
示例9: traceback_attach
def traceback_attach(space, py_obj, w_obj):
py_traceback = rffi.cast(PyTracebackObject, py_obj)
traceback = space.interp_w(PyTraceback, w_obj)
if traceback.next is None:
w_next_traceback = None
else:
w_next_traceback = space.wrap(traceback.next)
py_traceback.c_tb_next = rffi.cast(PyTracebackObject, make_ref(space, w_next_traceback))
py_traceback.c_tb_frame = rffi.cast(PyFrameObject, make_ref(space, space.wrap(traceback.frame)))
rffi.setintfield(py_traceback, 'c_tb_lasti', traceback.lasti)
rffi.setintfield(py_traceback, 'c_tb_lineno',traceback.get_lineno())
开发者ID:abhinavthomas,项目名称:pypy,代码行数:11,代码来源:pytraceback.py
示例10: code_attach
def code_attach(space, py_obj, w_obj):
py_code = rffi.cast(PyCodeObject, py_obj)
assert isinstance(w_obj, PyCode)
py_code.c_co_name = make_ref(space, space.wrap(w_obj.co_name))
py_code.c_co_filename = make_ref(space, space.wrap(w_obj.co_filename))
co_flags = 0
for name, value in ALL_CODE_FLAGS:
if w_obj.co_flags & getattr(pycode, name):
co_flags |= value
rffi.setintfield(py_code, 'c_co_flags', co_flags)
rffi.setintfield(py_code, 'c_co_argcount', w_obj.co_argcount)
开发者ID:mozillazg,项目名称:pypy,代码行数:11,代码来源:funcobject.py
示例11: 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
示例12: test_ConcatAndDel
def test_ConcatAndDel(self, space, api):
ref1 = make_ref(space, space.wrap('abc'))
ref2 = make_ref(space, space.wrap('def'))
ptr = lltype.malloc(PyObjectP.TO, 1, flavor='raw')
ptr[0] = ref1
api.PyString_ConcatAndDel(ptr, ref2)
assert space.str_w(from_ref(space, ptr[0])) == 'abcdef'
assert ref2.c_ob_refcnt == 0
Py_DecRef(space, ptr[0])
ptr[0] = lltype.nullptr(PyObject.TO)
ref2 = make_ref(space, space.wrap('foo'))
api.PyString_ConcatAndDel(ptr, ref2) # should not crash
assert ref2.c_ob_refcnt == 0
lltype.free(ptr, flavor='raw')
开发者ID:Debug-Orz,项目名称:Sypy,代码行数:14,代码来源:test_stringobject.py
示例13: PyList_SET_ITEM
def PyList_SET_ITEM(space, w_list, index, w_item):
"""Macro form of PyList_SetItem() without error checking. This is normally
only used to fill in new lists where there is no previous content.
This function "steals" a reference to item, and, unlike PyList_SetItem(),
does not discard a reference to any item that it being replaced; any
reference in list at position i will be leaked.
"""
assert isinstance(w_list, W_ListObject)
assert 0 <= index < w_list.length()
# Deliberately leak, so that it can be safely decref'd.
make_ref(space, w_list.getitem(index))
Py_DecRef(space, w_item)
w_list.setitem(index, w_item)
return w_item
开发者ID:mozillazg,项目名称:pypy,代码行数:15,代码来源:listobject.py
示例14: PyObject_GetBuffer
def PyObject_GetBuffer(space, w_obj, view, flags):
"""Export obj into a Py_buffer, view. These arguments must
never be NULL. The flags argument is a bit field indicating what
kind of buffer the caller is prepared to deal with and therefore what
kind of buffer the exporter is allowed to return. The buffer interface
allows for complicated memory sharing possibilities, but some caller may
not be able to handle all the complexity but may want to see if the
exporter will let them take a simpler view to its memory.
Some exporters may not be able to share memory in every possible way and
may need to raise errors to signal to some consumers that something is
just not possible. These errors should be a BufferError unless
there is another error that is actually causing the problem. The
exporter can use flags information to simplify how much of the
Py_buffer structure is filled in with non-default values and/or
raise an error if the object can't support a simpler view of its memory.
0 is returned on success and -1 on error."""
flags = widen(flags)
buf = space.buffer_w(w_obj, flags)
try:
view.c_buf = rffi.cast(rffi.VOIDP, buf.get_raw_address())
except ValueError:
raise BufferError("could not create buffer from object")
ret = fill_Py_buffer(space, buf, view)
view.c_obj = make_ref(space, w_obj)
return ret
开发者ID:mozillazg,项目名称:pypy,代码行数:27,代码来源:memoryobject.py
示例15: convert_argument_libffi
def convert_argument_libffi(self, space, w_obj, argchain, call_local):
if hasattr(space, "fake"):
raise NotImplementedError
space.getbuiltinmodule("cpyext")
from pypy.module.cpyext.pyobject import make_ref
ref = make_ref(space, w_obj)
argchain.arg(rffi.cast(rffi.VOIDP, ref))
开发者ID:MichaelBlume,项目名称:pypy,代码行数:7,代码来源:converter.py
示例16: _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
示例17: bytes_getbuffer
def bytes_getbuffer(space, w_str, view, flags):
from pypy.module.cpyext.bytesobject import PyBytes_AsString
view.c_obj = make_ref(space, w_str)
view.c_buf = rffi.cast(rffi.VOIDP, PyBytes_AsString(space, view.c_obj))
view.c_len = space.len_w(w_str)
return 0
开发者ID:Qointum,项目名称:pypy,代码行数:7,代码来源:typeobject.py
示例18: 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
示例19: PyByteArray_FromObject
def PyByteArray_FromObject(space, w_obj):
"""Return a new bytearray object from any object, o, that implements the
buffer protocol.
XXX expand about the buffer protocol, at least somewhere"""
w_buffer = space.call_function(space.w_bytearray, w_obj)
return make_ref(space, w_buffer)
开发者ID:mozillazg,项目名称:pypy,代码行数:7,代码来源:bytearrayobject.py
示例20: buffer_attach
def buffer_attach(space, py_obj, w_obj):
"""
Fills a newly allocated PyBufferObject with the given (str) buffer object.
"""
py_buf = rffi.cast(PyBufferObject, py_obj)
py_buf.c_b_offset = 0
rffi.setintfield(py_buf, 'c_b_readonly', 1)
rffi.setintfield(py_buf, 'c_b_hash', -1)
if isinstance(w_obj, SubBuffer):
py_buf.c_b_offset = w_obj.offset
w_obj = w_obj.buffer
# If w_obj already allocated a fixed buffer, use it, and keep a
# reference to w_obj.
# Otherwise, b_base stays NULL, and we own the b_ptr.
if isinstance(w_obj, StringBuffer):
py_buf.c_b_base = lltype.nullptr(PyObject.TO)
py_buf.c_b_ptr = rffi.cast(rffi.VOIDP, rffi.str2charp(w_obj.value))
py_buf.c_b_size = w_obj.getlength()
elif isinstance(w_obj, ArrayBuffer):
w_base = w_obj.array
py_buf.c_b_base = make_ref(space, w_base)
py_buf.c_b_ptr = rffi.cast(rffi.VOIDP, w_obj.array._charbuf_start())
py_buf.c_b_size = w_obj.getlength()
else:
raise OperationError(space.w_NotImplementedError, space.wrap(
"buffer flavor not supported"))
开发者ID:Debug-Orz,项目名称:Sypy,代码行数:29,代码来源:bufferobject.py
注:本文中的pypy.module.cpyext.pyobject.make_ref函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论