Recursive struct definition in Pure Python mode

42 views
Skip to first unread message

Gee Transit

unread,
Nov 6, 2022, 2:47:42 PM11/6/22
to cython...@googlegroups.com
Hi all,

In cdef syntax, I can define structs with members that point to the same
struct like so:

cdef struct Node:
    int data
    Node *next

However, I can't seem to do the same thing in Pure Python mode. From the
docs on structs [1]_ and pointers [2]_, I've come up with the following
code:

import cython
Node = cython.struct(
    data=cython.int,
    next=cython.pointer(Node),
)

Unfortunately, this errors with the following:

Error compiling Cython file:
------------------------------------------------------------
...
import cython
Node = cython.struct(
    data=cython.int,
    next=cython.pointer(Node),
                        ^
------------------------------------------------------------

node2.py:4:24: Unknown type

Error compiling Cython file:
------------------------------------------------------------
...
import cython
Node = cython.struct(
    data=cython.int,
    next=cython.pointer(Node),
                       ^
------------------------------------------------------------

node2.py:4:23: Unknown type

My current workaround (other than using cdef syntax and .pyx files) is
to declare next as a cython.p_void and to do cython.cast(Node,
node.next) to use it, but it's fairly tedious.

Have I missed something in the docs (maybe regarding forward
declarations), or is this an unfortunate limitation of Pure Python mode?

.. [1]:
https://cython.readthedocs.io/en/latest/src/userguide/language_basics.html#types
.. [2]:
https://cython.readthedocs.io/en/latest/src/userguide/language_basics.html#structs-unions-enums

Gee Transit

unread,
Nov 6, 2022, 2:47:52 PM11/6/22
to cython-users

D Woods

unread,
Nov 16, 2022, 3:23:20 PM11/16/22
to cython-users
It probably is a bug - I won't have any suggestions for workarounds though. You might try a forward declaration type approach

Node = cython.struct()
Node = cython.struct(
    data=cython.int,
    next=cython.pointer(Node),
)

But I don't have a great deal of hope that this'd work.

If you have time please report it on the bug tracker, and hopefully it's something that can be fixed.

(Sorry for the slow reply. I thought I'd sent this earlier but I think my email client and google groups are not friends right now)

Gee Transit

unread,
Nov 28, 2022, 12:36:52 PM11/28/22
to cython...@googlegroups.com, D Woods

Thanks for replying. Unfortunately your suggestion didn't work:

Error compiling Cython file:
------------------------------------------------------------
...
import cython

Node = cython.struct()
                    ^
------------------------------------------------------------

node2.py:2:20: Struct or union members must be given by name.

Seems like forward declarations don't work for structs yet in Pure Python mode.

I've reported it on the bug tracker here: https://github.com/cython/cython/issues/5155. Hopefully there'll be a fix for this :)

--

---
You received this message because you are subscribed to a topic in the Google Groups "cython-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cython-users/pLIF1ITgC9o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cython-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/e05343f4-c868-4a39-9c08-ac4b9de6294en%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages