UIEntityCollection::getitem returns mcrfpy.Entity, even if a derived class was submitted #76

Open
opened 2025-03-03 20:51:29 +00:00 by john · 0 comments
Owner
  1. return the proper derived type instead of hard coding mcrf_module, "Entity"
    auto type = (PyTypeObject*)PyObject_GetAttrString(McRFPy_API::mcrf_module, "Entity");
    auto o = (PyUIEntityObject*)type->tp_alloc(type, 0);
    auto p = std::static_pointer_cast<UIEntity>(target);
    o->data = p;
    return (PyObject*)o;

should become:

auto o_new = (PyUIEntityObject*)py_type->tp_alloc(py_type, 0);
o_new->data = std::static_pointer_cast<UIEntity>(target);
return (PyObject*)o_new;

where py_type is the (stored somewhere) derived class that was appended

  1. the derived class has to be stored when the entity joins the collection
// from UIEntityCollection::append
    if (!PyObject_IsInstance(o, PyObject_GetAttrString(McRFPy_API::mcrf_module, "Entity")))
    {
        PyErr_SetString(PyExc_TypeError, "Only Entity objects can be added to EntityCollection");
        return NULL;
    }
    PyUIEntityObject* entity = (PyUIEntityObject*)o;
    self->data->push_back(entity->data);
    entity->data->grid = self->grid;

should incorporate:

PyTypeObject* py_type = Py_TYPE(o);

that type has to be stored somewhere, and then restored when referencing the same UIEntity object.

Perhaps a new field on the UIEntity object?

Alternatively, store submitted objects (increment their refcount when appended) and then return back the exact same Python object. Which could also be a way of holding mcrfpy.Entity instances for all Python objects; if it's cached and the refcount is exactly 1, then our cache can decref and delete the (C++-side) shared pointer.

see full chatgpt convo: https://chatgpt.com/c/67c60534-e1d4-800e-b4e6-13191b3e7348

1) return the proper derived type instead of hard coding `mcrf_module, "Entity"` ```cpp auto type = (PyTypeObject*)PyObject_GetAttrString(McRFPy_API::mcrf_module, "Entity"); auto o = (PyUIEntityObject*)type->tp_alloc(type, 0); auto p = std::static_pointer_cast<UIEntity>(target); o->data = p; return (PyObject*)o; ``` should become: ```cpp auto o_new = (PyUIEntityObject*)py_type->tp_alloc(py_type, 0); o_new->data = std::static_pointer_cast<UIEntity>(target); return (PyObject*)o_new; ``` where `py_type` is the *(stored somewhere)* derived class that was appended 2) the derived class has to be stored when the entity joins the collection ```cpp // from UIEntityCollection::append if (!PyObject_IsInstance(o, PyObject_GetAttrString(McRFPy_API::mcrf_module, "Entity"))) { PyErr_SetString(PyExc_TypeError, "Only Entity objects can be added to EntityCollection"); return NULL; } PyUIEntityObject* entity = (PyUIEntityObject*)o; self->data->push_back(entity->data); entity->data->grid = self->grid; ``` should incorporate: ```cpp PyTypeObject* py_type = Py_TYPE(o); ``` that type has to be stored somewhere, and then restored when referencing the same UIEntity object. **Perhaps a new field on the `UIEntity` object?** Alternatively, store submitted objects (increment their refcount when `append`ed) and then return back the exact same Python object. Which could also be a way of holding `mcrfpy.Entity` instances for all Python objects; if it's cached and the refcount is exactly 1, then our cache can decref and delete the (C++-side) shared pointer. see full chatgpt convo: https://chatgpt.com/c/67c60534-e1d4-800e-b4e6-13191b3e7348
john added the
Minor Feature
Bugfix
labels 2025-03-03 20:51:29 +00:00
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: john/McRogueFace#76
No description provided.