static PyObject * weakref_proxy(PyObject *self, PyObject *args) { PyObject *object; PyObject *callback = NULL; PyObject *result = NULL; if (PyArg_UnpackTuple(args, "proxy", 1, 2, &object, &callback)) { result = PyWeakref_NewProxy(object, callback); } return result; }
/* * This function returns a NEW reference, i.e. caller must decref it in the end. */ PyObject* JySync_Init_PyWeakCallableProxy_From_JyWeakCallableProxy(jobject src, PyTypeObject* nonNativeSubtype) { // Todo: Handle case that the native counterpart of the Java-referent is // not weakly referenceable in CPython-terms. // See PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob)) env(NULL); jobject jReferent = (*env)->CallObjectMethod(env, src, AbstractReference_get); PyObject* referent = JyNI_PyObject_FromJythonPyObject(jReferent); /* Note that an extra Py_INCREF is not necessary since the conversion method * returns a new reference. * JyNI-note: It is okay to hold a refcount for the weakly referenced object. * This will be decreffed when the Java-side GlobalRef is released. */ PyObject* result = PyWeakref_NewProxy(referent, NULL); // Todo: Support callback. return result; }
static PyObject * World_get_object(PyWorld *self, PyObject * id) { if (!PyString_CheckExact(id)) { PyErr_SetString(PyExc_TypeError, "World.get_object must be string"); return NULL; } LocatedEntity * ent = BaseWorld::instance().getEntity(PyString_AsString(id)); if (ent == NULL) { Py_INCREF(Py_None); return Py_None; } PyObject * wrapper = wrapEntity(ent); if (wrapper == NULL) { return NULL; } PyObject * wrapper_proxy = PyWeakref_NewProxy(wrapper, NULL); // FIXME Have wrapEntity return a borrowed reference Py_DECREF(wrapper); return wrapper_proxy; }