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

[BUG] Use constants to size arrays in the stack in pure-python mode #6505

Open
crusaderky opened this issue Nov 19, 2024 · 0 comments
Open

Comments

@crusaderky
Copy link

crusaderky commented Nov 19, 2024

Describe the bug

common.pxd:

cdef enum:
    NPY_MAXDIMS = 32

This constant can be imported from Cython-only modules to create arrays in the stack:

demo.pyx:

from mypackage.common cimport NPY_MAXDIMS

def f():
    cdef int[NPY_MAXDIMS] scratch

However, the same crashes in Pure-python mode:
demo2.py:

import cython
if cython.compiled:
    from cython.cimports.mypackage.common import NPY_MAXDIMS
else:
    NPY_MAXDIMS = 32

def f():
    scratch: cython.int[NPY_MAXDIMS]
    if not cython.compiled:
        scratch = [None] * NPY_MAXDIMS

The latter crashes during compilation:

mypackage.demo2.py:6:41: Array size must be a compile time constant

I could not find any other way to define the size of the scratch buffer in pure-python mode other than hardcoding it. This works but it's very poorly maintainable:

import cython

def f():
    scratch: cython.int[32]
    if not cython.compiled:
        scratch = [None] * 32

Python version

3.12.7

Cython version

3.0.11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant