Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling cdef class inheritance and circular cimports #2914

Open
AnishN opened this Issue Apr 6, 2019 · 2 comments

Comments

Projects
None yet
2 participants
@AnishN
Copy link

commented Apr 6, 2019

I have the following simplified scenario for my game engine I am writing in cython. I have an "App" cdef class that is responsible for processing a list of "System" cdef class objects. I would like each "System" object to have a reference back to its parent "App" object that it belongs to. The "System" cdef class has some basic functions, and I want to extend this class in sub-cdef-classes (e.g. "GraphicsSystem", "AudioSystem", "PhysicsSystem", etc.). This boils down to the following code:

#system.pxd
from pyorama.app cimport *

cdef class System:
    cdef App app
    
    cpdef void init(self)
    cpdef void quit(self)
    cpdef void update(self)

#system.pyx
cdef class System:
    
    def __init__(self):
        self.app = None
    
    cpdef void init(self): print("system init")
    cpdef void quit(self): print("system quit")
    cpdef void update(self): print("system update")
#graphics_system.pxd
from pyorama.system cimport *

cdef class GraphicsSystem(System):
    pass

#graphics_system.pyx
cdef class GraphicsSystem(System):
    pass
#app.pxd
from pyorama.graphics_system cimport *
cdef class App:
    pass

#app.pyx
cdef class App:
    pass

Attempting to compile this yields the following error message during the cythonization phase:

anish@laptop:~/Documents/Programming/cython/inheritance$ python3 setup.py build_ext -i
Compiling ./pyorama/graphics_system.pyx because it depends on ./pyorama/system.pxd.
Compiling ./pyorama/app.pyx because it depends on ./pyorama/system.pxd.
Compiling ./pyorama/system.pyx because it changed.
[1/3] Cythonizing ./pyorama/system.pyx

Error compiling Cython file:
------------------------------------------------------------
...
from pyorama.system cimport *

cdef class GraphicsSystem(System):
    ^
------------------------------------------------------------

pyorama/graphics_system.pxd:3:5: Compiler crash in AnalyseDeclarationsTransform

ModuleNode.body = StatListNode(graphics_system.pxd:1:0)
StatListNode.stats[1] = CClassDefNode(graphics_system.pxd:3:5,
    as_name = 'GraphicsSystem',
    class_name = 'GraphicsSystem',
    in_pxd = True,
    module_name = '',
    visibility = 'private')

Compiler crash traceback from this point on:
  File "Cython/Compiler/Visitor.py", line 180, in Cython.Compiler.Visitor.TreeVisitor._visit
  File "/home/anish/.local/lib/python3.6/site-packages/Cython/Compiler/ParseTreeTransforms.py", line 1621, in visit_CClassDefNode
    node = self.visit_ClassDefNode(node)
  File "Cython/Compiler/Visitor.py", line 383, in Cython.Compiler.Visitor.EnvTransform.visit_ClassDefNode
AttributeError: 'CClassDefNode' object has no attribute 'scope'

Error compiling Cython file:
------------------------------------------------------------
...
from pyorama.system cimport *

cdef class GraphicsSystem(System):
    ^
------------------------------------------------------------

pyorama/graphics_system.pxd:3:5: C class 'GraphicsSystem' already defined
Traceback (most recent call last):
  File "setup.py", line 66, in <module>
    compiler_directives=directives,
  File "/home/anish/.local/lib/python3.6/site-packages/Cython/Build/Dependencies.py", line 1097, in cythonize
    cythonize_one(*args)
  File "/home/anish/.local/lib/python3.6/site-packages/Cython/Build/Dependencies.py", line 1220, in cythonize_one
    raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: ./pyorama/system.pyx

Commenting out the cimport in the "App" allows the code to compile, and using the "GraphicsSystem" cdef class in other cython modules works without issue. Similarly, cimporting "System" into the app.pxd file works without issues; it appears that just the subclass causes issues during compilation. Any help would be appreciated!

@jdemeyer

This comment has been minimized.

Copy link
Contributor

commented Apr 18, 2019

Does it make a difference if you don't cimport * but instead cimport only precisely what you need?

@AnishN

This comment has been minimized.

Copy link
Author

commented Apr 21, 2019

Unfortunately, it does not seem to make a difference modifying the following three cimports as so:

from pyorama.app cimport App
from pyorama.system cimport System
from pyorama.graphics_system cimport GraphicsSystem

The end result is still the same error as described before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.