📄 weakrefobject.c
字号:
/* Portions Copyright (c) 2005 Nokia Corporation */
#include "Python.h"
#include "structmember.h"
#define GET_WEAKREFS_LISTPTR(o) \
((PyWeakReference **) PyObject_GET_WEAKREFS_LISTPTR(o))
#ifndef SYMBIAN
static PyWeakReference *free_list = NULL;
#else
#define free_list (pyglobals->WRO_free_list)
#endif
DL_EXPORT(long)
_PyWeakref_GetWeakrefCount(PyWeakReference *head)
{
long count = 0;
while (head != NULL) {
++count;
head = head->wr_next;
}
return count;
}
static PyWeakReference *
new_weakref(void)
{
PyWeakReference *result;
#ifdef SYMBIAN
SPy_Python_globals* pyglobals = PYTHON_GLOBALS; // avoid TLS reads
#endif
if (free_list != NULL) {
result = free_list;
free_list = result->wr_next;
result->ob_type = &_PyWeakref_RefType;
_Py_NewReference((PyObject *)result);
}
else {
result = PyObject_GC_New(PyWeakReference, &_PyWeakref_RefType);
}
if (result)
result->hash = -1;
return result;
}
/* This function clears the passed-in reference and removes it from the
* list of weak references for the referent. This is the only code that
* removes an item from the doubly-linked list of weak references for an
* object; it is also responsible for clearing the callback slot.
*/
static void
clear_weakref(PyWeakReference *self)
{
PyObject *callback = self->wr_callback;
if (PyWeakref_GET_OBJECT(self) != Py_None) {
PyWeakReference **list = GET_WEAKREFS_LISTPTR(
PyWeakref_GET_OBJECT(self));
if (*list == self)
*list = self->wr_next;
self->wr_object = Py_None;
self->wr_callback = NULL;
if (self->wr_prev != NULL)
self->wr_prev->wr_next = self->wr_next;
if (self->wr_next != NULL)
self->wr_next->wr_prev = self->wr_prev;
self->wr_prev = NULL;
self->wr_next = NULL;
Py_XDECREF(callback);
}
}
static void
weakref_dealloc(PyWeakReference *self)
{
#ifdef SYMBIAN
SPy_Python_globals* pyglobals = PYTHON_GLOBALS; // avoid TLS reads
#endif
PyObject_GC_UnTrack((PyObject *)self);
clear_weakref(self);
self->wr_next = free_list;
free_list = self;
}
static int
gc_traverse(PyWeakReference *self, visitproc visit, void *arg)
{
if (self->wr_callback != NULL)
return visit(self->wr_callback, arg);
return 0;
}
static int
gc_clear(PyWeakReference *self)
{
clear_weakref(self);
return 0;
}
static PyObject *
weakref_call(PyWeakReference *self, PyObject *args, PyObject *kw)
{
#ifndef SYMBIAN
static char *argnames[] = {NULL};
#else
#define argnames (PYTHON_GLOBALS->argnames)
#endif
if (PyArg_ParseTupleAndKeywords(args, kw, ":__call__", argnames)) {
PyObject *object = PyWeakref_GET_OBJECT(self);
Py_INCREF(object);
return (object);
}
return NULL;
}
static long
weakref_hash(PyWeakReference *self)
{
if (self->hash != -1)
return self->hash;
if (PyWeakref_GET_OBJECT(self) == Py_None) {
PyErr_SetString(PyExc_TypeError, "weak object has gone away");
return -1;
}
self->hash = PyObject_Hash(PyWeakref_GET_OBJECT(self));
return self->hash;
}
static PyObject *
weakref_repr(PyWeakReference *self)
{
char buffer[256];
if (PyWeakref_GET_OBJECT(self) == Py_None) {
PyOS_snprintf(buffer, sizeof(buffer), "<weakref at %lx; dead>",
(long)(self));
}
else {
PyOS_snprintf(buffer, sizeof(buffer),
"<weakref at %#lx; to '%.50s' at %#lx>",
(long)(self),
PyWeakref_GET_OBJECT(self)->ob_type->tp_name,
(long)(PyWeakref_GET_OBJECT(self)));
}
return PyString_FromString(buffer);
}
/* Weak references only support equality, not ordering. Two weak references
are equal if the underlying objects are equal. If the underlying object has
gone away, they are equal if they are identical. */
static PyObject *
weakref_richcompare(PyWeakReference* self, PyWeakReference* other, int op)
{
if (op != Py_EQ || self->ob_type != other->ob_type) {
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
if (PyWeakref_GET_OBJECT(self) == Py_None
|| PyWeakref_GET_OBJECT(other) == Py_None) {
PyObject *res = self==other ? Py_True : Py_False;
Py_INCREF(res);
return res;
}
return PyObject_RichCompare(PyWeakref_GET_OBJECT(self),
PyWeakref_GET_OBJECT(other), op);
}
#ifndef SYMBIAN
PyTypeObject
_PyWeakref_RefType = {
PyObject_HEAD_INIT(&PyType_Type)
#else
const PyTypeObject
_c_PyWeakref_RefType = {
PyObject_HEAD_INIT(NULL)
#endif
0,
"weakref",
sizeof(PyWeakReference),
0,
(destructor)weakref_dealloc,/*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
(reprfunc)weakref_repr, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
(hashfunc)weakref_hash, /*tp_hash*/
(ternaryfunc)weakref_call, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_RICHCOMPARE,
0, /*tp_doc*/
(traverseproc)gc_traverse, /*tp_traverse*/
(inquiry)gc_clear, /*tp_clear*/
(richcmpfunc)weakref_richcompare, /*tp_richcompare*/
0, /*tp_weaklistoffset*/
};
static int
proxy_checkref(PyWeakReference *proxy)
{
if (PyWeakref_GET_OBJECT(proxy) == Py_None) {
PyErr_SetString(PyExc_ReferenceError,
"weakly-referenced object no longer exists");
return 0;
}
return 1;
}
/* If a parameter is a proxy, check that it is still "live" and wrap it,
* replacing the original value with the raw object. Raises ReferenceError
* if the param is a dead proxy.
*/
#define UNWRAP(o) \
if (PyWeakref_CheckProxy(o)) { \
if (!proxy_checkref((PyWeakReference *)o)) \
return NULL; \
o = PyWeakref_GET_OBJECT(o); \
}
#define UNWRAP_I(o) \
if (PyWeakref_CheckProxy(o)) { \
if (!proxy_checkref((PyWeakReference *)o)) \
return -1; \
o = PyWeakref_GET_OBJECT(o); \
}
#define WRAP_UNARY(method, generic) \
static PyObject * \
method(PyObject *proxy) { \
UNWRAP(proxy); \
return generic(proxy); \
}
#define WRAP_BINARY(method, generic) \
static PyObject * \
method(PyObject *x, PyObject *y) { \
UNWRAP(x); \
UNWRAP(y); \
return generic(x, y); \
}
/* Note that the third arg needs to be checked for NULL since the tp_call
* slot can receive NULL for this arg.
*/
#define WRAP_TERNARY(method, generic) \
static PyObject * \
method(PyObject *proxy, PyObject *v, PyObject *w) { \
UNWRAP(proxy); \
UNWRAP(v); \
if (w != NULL) \
UNWRAP(w); \
return generic(proxy, v, w); \
}
/* direct slots */
WRAP_BINARY(proxy_getattr, PyObject_GetAttr)
WRAP_UNARY(proxy_str, PyObject_Str)
WRAP_TERNARY(proxy_call, PyEval_CallObjectWithKeywords)
static int
proxy_print(PyWeakReference *proxy, FILE *fp, int flags)
{
if (!proxy_checkref(proxy))
return -1;
return PyObject_Print(PyWeakref_GET_OBJECT(proxy), fp, flags);
}
static PyObject *
proxy_repr(PyWeakReference *proxy)
{
char buf[160];
PyOS_snprintf(buf, sizeof(buf),
"<weakref at %p to %.100s at %p>", proxy,
PyWeakref_GET_OBJECT(proxy)->ob_type->tp_name,
PyWeakref_GET_OBJECT(proxy));
return PyString_FromString(buf);
}
static int
proxy_setattr(PyWeakReference *proxy, PyObject *name, PyObject *value)
{
if (!proxy_checkref(proxy))
return -1;
return PyObject_SetAttr(PyWeakref_GET_OBJECT(proxy), name, value);
}
static int
proxy_compare(PyObject *proxy, PyObject *v)
{
UNWRAP_I(proxy);
UNWRAP_I(v);
return PyObject_Compare(proxy, v);
}
/* number slots */
WRAP_BINARY(proxy_add, PyNumber_Add)
WRAP_BINARY(proxy_sub, PyNumber_Subtract)
WRAP_BINARY(proxy_mul, PyNumber_Multiply)
WRAP_BINARY(proxy_div, PyNumber_Divide)
WRAP_BINARY(proxy_mod, PyNumber_Remainder)
WRAP_BINARY(proxy_divmod, PyNumber_Divmod)
WRAP_TERNARY(proxy_pow, PyNumber_Power)
WRAP_UNARY(proxy_neg, PyNumber_Negative)
WRAP_UNARY(proxy_pos, PyNumber_Positive)
WRAP_UNARY(proxy_abs, PyNumber_Absolute)
WRAP_UNARY(proxy_invert, PyNumber_Invert)
WRAP_BINARY(proxy_lshift, PyNumber_Lshift)
WRAP_BINARY(proxy_rshift, PyNumber_Rshift)
WRAP_BINARY(proxy_and, PyNumber_And)
WRAP_BINARY(proxy_xor, PyNumber_Xor)
WRAP_BINARY(proxy_or, PyNumber_Or)
WRAP_UNARY(proxy_int, PyNumber_Int)
WRAP_UNARY(proxy_long, PyNumber_Long)
WRAP_UNARY(proxy_float, PyNumber_Float)
WRAP_BINARY(proxy_iadd, PyNumber_InPlaceAdd)
WRAP_BINARY(proxy_isub, PyNumber_InPlaceSubtract)
WRAP_BINARY(proxy_imul, PyNumber_InPlaceMultiply)
WRAP_BINARY(proxy_idiv, PyNumber_InPlaceDivide)
WRAP_BINARY(proxy_imod, PyNumber_InPlaceRemainder)
WRAP_TERNARY(proxy_ipow, PyNumber_InPlacePower)
WRAP_BINARY(proxy_ilshift, PyNumber_InPlaceLshift)
WRAP_BINARY(proxy_irshift, PyNumber_InPlaceRshift)
WRAP_BINARY(proxy_iand, PyNumber_InPlaceAnd)
WRAP_BINARY(proxy_ixor, PyNumber_InPlaceXor)
WRAP_BINARY(proxy_ior, PyNumber_InPlaceOr)
static int
proxy_nonzero(PyWeakReference *proxy)
{
PyObject *o = PyWeakref_GET_OBJECT(proxy);
if (!proxy_checkref(proxy))
return 1;
if (o->ob_type->tp_as_number &&
o->ob_type->tp_as_number->nb_nonzero)
return (*o->ob_type->tp_as_number->nb_nonzero)(o);
else
return 1;
}
/* sequence slots */
static PyObject *
proxy_slice(PyWeakReference *proxy, int i, int j)
{
if (!proxy_checkref(proxy))
return NULL;
return PySequence_GetSlice(PyWeakref_GET_OBJECT(proxy), i, j);
}
static int
proxy_ass_slice(PyWeakReference *proxy, int i, int j, PyObject *value)
{
if (!proxy_checkref(proxy))
return -1;
return PySequence_SetSlice(PyWeakref_GET_OBJECT(proxy), i, j, value);
}
static int
proxy_contains(PyWeakReference *proxy, PyObject *value)
{
if (!proxy_checkref(proxy))
return -1;
return PySequence_Contains(PyWeakref_GET_OBJECT(proxy), value);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -