Debian Codegraph @

pypy/module/cpyext/weakrefobject.py

raw symbols SCIP
1 from pypy.module.cpyext.api import cpython_api
2 from pypy.module.cpyext.pyobject import PyObject, CANNOT_FAIL
3 from pypy.module._weakref.interp__weakref import W_Weakref, proxy
4 from pypy.module._weakref.interp__weakref import W_Proxy, W_CallableProxy
5 from rpython.rtyper.lltypesystem import rffi
6
7 @cpython_api([PyObject, PyObject], PyObject)
8 def PyWeakref_NewRef(space, w_obj, w_callback):
9 """Return a weak reference object for the object ob. This will always return
10 a new reference, but is not guaranteed to create a new object; an existing
11 reference object may be returned. The second parameter, callback, can be a
12 callable object that receives notification when ob is garbage collected; it
13 should accept a single parameter, which will be the weak reference object
14 itself. callback may also be None or NULL. If ob is not a
15 weakly-referencable object, or if callback is not callable, None, or
16 NULL, this will return NULL and raise TypeError.
17 """
18 w_weakref = space.gettypeobject(W_Weakref.typedef)
19 return space.call_function(w_weakref, w_obj, w_callback)
20
21 @cpython_api([PyObject, PyObject], PyObject)
22 def PyWeakref_NewProxy(space, w_obj, w_callback):
23 """Return a weak reference proxy object for the object *ob*. This will
24 alwas return a new reference, but is not guaranteed to create a new
25 object; an existing proxy object may be returned. The second parameter,
26 *callback*, can be a callable object that receives notification when *ob*
27 is garbage collected; it should accept a single parameter, which will be
28 the weak reference object itself. *callback* may also be ``None`` or
29 *NULL*. If *ob* is not a weakly-referencable object, or if *callback* is
30 not callable, ``None``, or *NULL*, this will return *NULL* and raise
31 :exc:`TypeError`.
32 """
33 return proxy(space, w_obj, w_callback)
34
35 @cpython_api([PyObject], PyObject, result_borrowed=True)
36 def PyWeakref_GetObject(space, w_ref):
37 """Return the referenced object from a weak reference. If the referent is
38 no longer live, returns None. This function returns a borrowed reference.
39 """
40 return space.call_function(w_ref) # borrowed ref
41
42 @cpython_api([rffi.VOIDP], PyObject, result_borrowed=True)
43 def PyWeakref_GET_OBJECT(space, w_ref):
44 """Similar to PyWeakref_GetObject(), but implemented as a macro that does no
45 error checking.
46 """
47 return space.call_function(w_ref) # borrowed ref
48
49 @cpython_api([PyObject], PyObject)
50 def PyWeakref_LockObject(space, w_ref):
51 """Return the referenced object from a weak reference. If the referent is
52 no longer live, returns None. This function returns a new reference.
53
54 (A PyPy extension that may not be useful any more: use
55 PyWeakref_GetObject() and Py_INCREF().)
56 """
57 return space.call_function(w_ref)
58
59 @cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
60 def PyWeakref_CheckRef(space, w_obj):
61 """Return true if ob is a reference object.
62 """
63 w_obj_type = space.type(w_obj)
64 w_type = space.gettypeobject(W_Weakref.typedef)
65 return (space.is_w(w_obj_type, w_type) or
66 space.issubtype_w(w_obj_type, w_type))
67
68 @cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
69 def PyWeakref_CheckRefExact(space, w_obj):
70 w_obj_type = space.type(w_obj)
71 w_type = space.gettypeobject(W_Weakref.typedef)
72 return space.is_w(w_obj_type, w_type)
73
74 @cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
75 def PyWeakref_CheckProxy(space, w_obj):
76 """Return true if ob is a proxy object.
77 """
78 w_obj_type = space.type(w_obj)
79 w_type1 = space.gettypeobject(W_Proxy.typedef)
80 w_type2 = space.gettypeobject(W_CallableProxy.typedef)
81 return space.is_w(w_obj_type, w_type1) or space.is_w(w_obj_type, w_type2)
82
83 @cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
84 def PyWeakref_Check(space, w_obj):
85 """Return true if ob is either a reference or proxy object.
86 """
87 return (PyWeakref_CheckRef(space, w_obj) or
88 PyWeakref_CheckProxy(space, w_obj))