Exemplo n.º 1
0
PyQtGuiObject* QtBaseWidget::getControl() {
	PyQtGuiObject* control = (PyQtGuiObject*) PyWeakref_GET_OBJECT(controlRef);
	if(!control) return NULL;
	if(!PyType_IsSubtype(Py_TYPE(control), &QtGuiObject_Type)) {
		printf("QtBaseWidget: control is wrong type\n");
		return NULL;
	}
	Py_INCREF(control);
	return control;
}
Exemplo n.º 2
0
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;
}
Exemplo n.º 3
0
static PyObject *
weakref_richcompare(PyWeakReference* self, PyWeakReference* other, int op)
{
    if ((op != Py_EQ && op != Py_NE) || 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) {
        int res = (self == other);
        if (op == Py_NE)
            res = !res;
        if (res)
            Py_RETURN_TRUE;
        else
            Py_RETURN_FALSE;
    }
    return PyObject_RichCompare(PyWeakref_GET_OBJECT(self),
                                PyWeakref_GET_OBJECT(other), op);
}
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;
}
static PyObject *
weakref_call(PyWeakReference *self, PyObject *args, PyObject *kw)
{
    static char *argnames[] = {NULL};

    if (PyArg_ParseTupleAndKeywords(args, kw, ":__call__", argnames)) {
        PyObject *object = PyWeakref_GET_OBJECT(self);
        Py_INCREF(object);
        return (object);
    }
    return NULL;
}
Exemplo n.º 6
0
static PyObject *
weakref_richcompare(PyWeakReference* self, PyWeakReference* other, int op)
{
    if ((op != Py_EQ && op != Py_NE) ||
        !PyWeakref_Check(self) ||
        !PyWeakref_Check(other)) {
        Py_RETURN_NOTIMPLEMENTED;
    }
    if (PyWeakref_GET_OBJECT(self) == Py_None
        || PyWeakref_GET_OBJECT(other) == Py_None) {
        int res = (self == other);
        if (op == Py_NE)
            res = !res;
        if (res)
            Py_RETURN_TRUE;
        else
            Py_RETURN_FALSE;
    }
    return PyObject_RichCompare(PyWeakref_GET_OBJECT(self),
                                PyWeakref_GET_OBJECT(other), op);
}
Exemplo n.º 7
0
static PyObject *
weakref_repr(PyWeakReference *self)
{
    char buffer[256];
    if (PyWeakref_GET_OBJECT(self) == Py_None) {
        PyOS_snprintf(buffer, sizeof(buffer), "<weakref at %p; dead>", self);
    }
    else {
        char *name = NULL;
        PyObject *nameobj = PyObject_GetAttrString(PyWeakref_GET_OBJECT(self),
                                                   "__name__");
        if (nameobj == NULL)
                PyErr_Clear();
        else if (PyString_Check(nameobj))
                name = PyString_AS_STRING(nameobj);
        if (name != NULL) {
            PyOS_snprintf(buffer, sizeof(buffer),
                          "<weakref at %p; to '%.50s' at %p (%s)>",
                          self,
                          Py_TYPE(PyWeakref_GET_OBJECT(self))->tp_name,
                          PyWeakref_GET_OBJECT(self),
                          name);
        }
        else {
            PyOS_snprintf(buffer, sizeof(buffer),
                          "<weakref at %p; to '%.50s' at %p>",
                          self,
                          Py_TYPE(PyWeakref_GET_OBJECT(self))->tp_name,
                          PyWeakref_GET_OBJECT(self));
        }
        Py_XDECREF(nameobj);
    }
    return PyString_FromString(buffer);
}
Exemplo n.º 8
0
static PyObject *
weakref_repr(PyWeakReference *self)
{
    PyObject *name, *repr;
    _Py_IDENTIFIER(__name__);

    if (PyWeakref_GET_OBJECT(self) == Py_None)
        return PyUnicode_FromFormat("<weakref at %p; dead>", self);

    name = _PyObject_GetAttrId(PyWeakref_GET_OBJECT(self), &PyId___name__);
    if (name == NULL || !PyUnicode_Check(name)) {
        if (name == NULL)
            PyErr_Clear();
        repr = PyUnicode_FromFormat(
            "<weakref at %p; to '%s' at %p>",
            self,
            Py_TYPE(PyWeakref_GET_OBJECT(self))->tp_name,
            PyWeakref_GET_OBJECT(self));
    }
    else {
        repr = PyUnicode_FromFormat(
            "<weakref at %p; to '%s' at %p (%U)>",
            self,
            Py_TYPE(PyWeakref_GET_OBJECT(self))->tp_name,
            PyWeakref_GET_OBJECT(self),
            name);
    }
    Py_XDECREF(name);
    return repr;
}
Exemplo n.º 9
0
/* 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);
    }
}
Exemplo n.º 10
0
PyObject *WeakSet_remover(void *closure,PyObject *args,PyObject *kwargs){
    remover_closure *self = (remover_closure *)closure;
    PyObject *key = NULL;

    // The set we were created to update may not exist anymore.
    PyObject *set = PyWeakref_GET_OBJECT(self->setref);
    if (set == Py_None) Py_RETURN_NONE;
    
    if (!PyArg_ParseTuple(args, "O",&key)) return NULL;

    // Remove ourselves from the set
    int r = PySet_Discard(set,key);
    if (r == -1) return NULL; // Propegate errors from PySet_Discard
    if (r == 0){
        PyErr_Format(PyExc_RuntimeError,
                     "WeakSet_remover: PySet_Discard didn't find key in set");
        return NULL;
    }

    Py_RETURN_NONE;
}
Exemplo n.º 11
0
static void cv_control(SDL_UserEvent *event)
{
    volatile CVCoroutine *coro = (CVCoroutine *)(event->data1);

    {
        PyGILState_STATE gstate;
        CVCoroState *state = (CVCoroState *)coro; 

        gstate = PyGILState_Ensure();

        if (!cv_check_coroutine(state)) {
            PyObject *ret = event->data2;
            Py_XDECREF(ret);
            PyGILState_Release(gstate);
            cv_dealloc_coroutine(coro);
            return;
        }
        else {
            PyObject *current = PyWeakref_GET_OBJECT(state->actor_ptr);
            Py_INCREF(current);
            PyGILState_Release(gstate);
            _cv_globals.current_actor = current;
        }
        _cv_globals.current_coroutine = coro;
    }

    switch(cv_setjmp(to_coroutine)) {
        case 0: {
            CVStack *stack = &(coro->stack)
            coroutine_call(stack);
        }{
            CVCoroutine *parent = ((CVCoroState *)coro)->parent;

            if (parent != NULL) {
                int depth = _main_thread->recursion_depth;
                PyObject *result = (PyObject *)event->data2;

                if (cv_push_event(parent, result, CV_DISPATCHED_EVENT, depth) < 0) {
                    /* cleanup */
                    cv_longjmp(to_main_loop, -1);
                }
                ((CVCoroState *)coro)->parent = NULL;
            }
        }
        case -1: {
            PyObject *current;
            PyGILState_STATE gstate;

            cv_dealloc_coroutine(coro);
            current = _cv_globals.current_actor;
            gstate = PyGILState_Ensure();
            Py_DECREF(global_actor);
            PyGILState_Release(gstate);
            cv_longjmp(to_main_loop, -1);
        }
        case 1: {
            PyObject *current;
            PyGILState_STATE gstate;

            current = _cv_globals.current_actor;
            gstate = PyGILState_Ensure();
            Py_DECREF(global_actor);
            PyGILState_Release(gstate);
            cv_longjmp(to_main_loop, 1);
        }
    }
    /* Cleanup */
}
Exemplo n.º 12
0
static int is_dead(PyObject *actor)
{
    PyObject *a = PyWeakref_GET_OBJECT(actor);
    return ((a == Py_None) || _is_dead(a));
}