#What's wrong with this PyTypeObject tp_init method?

5 messages · Page 1 of 1 (latest)

daring bear
#

Here's the code (I've added some debug printing):

typedef struct {
    PyObject base;
    int width, height;
    PyObject *walls_dict;
    PyObject *distances;
    PyObject *vector_dict;
} VectorField;

static int vector_field_init(VectorField *self, PyObject *args, PyObject *kwds) {
    PyObject *walls;
    int sprite_size, height, width;
    if (!PyArg_ParseTuple(args, "Oiii", &walls, &sprite_size, &height, &width)) {
        return -1;
    }

    if (sprite_size && height && width) {
        SPRITE_SIZE = sprite_size;
        self->height = height;
        self->width = width;
    } else {
        return -1;
    }

    PyObject *sprite_list = PyObject_GetAttrString(walls, "sprite_list");
    PyObject *temp_walls_dict = PyDict_New();
    PyObject *infinity = Py_BuildValue("i", std::numeric_limits<int>::max());
    for (int i = 0; i < PyList_Size(sprite_list); i++) {
        std::cout << "iteration " << i << "\n";
        PyObject *item = PyList_GetItem(sprite_list, i);
        PyObject_Print(item, stdout, 0);
        std::cout << "\n";
        std::cout << "attr check " << PyObject_HasAttrString(item, "position") << "\n";
        PyObject *sprite_position = PyObject_GetAttrString(item, "position");
        PyObject_Print(sprite_position, stdout, 0);
        std::cout << "\n";
        PyDict_SetItem(temp_walls_dict, pixel_to_tile_pos(NULL, sprite_position), infinity);
    }
    self->walls_dict = temp_walls_dict;

    /* Return success */
    std::cout << "r" << "\n";
    return 0;
}

And the console output:

iteration 0
<arcade.sprite.Sprite object at 0x000002551F03B1F0>
attr check 1
(84.0, 84.0)
iteration 1
<arcade.sprite.Sprite object at 0x000002551F03B1C0>
attr check 0
(140.0, 84.0)

The attribute check should be returning 1 for every single iteration, but its not

formal tartanBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

daring bear
#

!solved

formal tartanBOT
#

Thank you and let us know if you have any more questions!

#

[SOLVED] What's wrong with this PyTypeObject tp_init method?