Skip to content

Instantly share code, notes, and snippets.

@eewanco
Created May 1, 2024 21:25
Show Gist options
  • Save eewanco/f21cee1917532e87b1fe72a107728eae to your computer and use it in GitHub Desktop.
Save eewanco/f21cee1917532e87b1fe72a107728eae to your computer and use it in GitHub Desktop.
Cython 3.0.10 class member scoping anomaly: Minimally Reproducible Example
# src/dotbug/dotbug-mre.pyx
cdef extern from "mycpp.h" namespace "my::name::space":
cdef cppclass MyClass:
int LIFE
cdef class MyMgr:
def my_failing_function(self) -> None:
cdef int life = MyClass.LIFE
"""
Results in C++ compilation error:
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -Iinclude -I/tmp/build-env-zm7iab4k/include -I/home/ANT.AMAZON.COM/ewancoe/.pyenv/versions/3.10.1/include/python3.10 -c src/dotbug/dotbug-mre.cpp -o build/temp.linux-x86_64-cpython-310/src/dotbug/dotbug-mre.o -std=c++2a
src/dotbug/dotbug-mre.cpp: In function ‘PyObject* __pyx_pf_6dotbug_5MyMgr_my_failing_function(__pyx_obj_6dotbug_MyMgr*)’:
src/dotbug/dotbug-mre.cpp:1482:39: error: expected primary-expression before ‘.’ token
1482 | __pyx_t_1 = my::name::space::MyClass.LIFE;
| ^
error: command '/usr/bin/gcc' failed with exit code 1
Generated code segment:
/* "src/dotbug/dotbug-mre.pyx":7
* cdef class MyMgr:
* def my_failing_function(self) -> None:
* cdef int life = MyClass.LIFE # <<<<<<<<<<<<<<
*
* """
*/
__pyx_t_1 = my::name::space::MyClass.LIFE;
__pyx_v_life = __pyx_t_1;
"""
include src/dotbug/*.pyx
include src/dotbug/*.pxd
include include/*.h
// include/mycpp.h
namespace my::name::space
{
class MyClass
{
public:
static inline int LIFE { 42 };
};
}
[build-system]
requires = [
"setuptools>=42",
"wheel",
"Cython",
]
build-backend = "setuptools.build_meta"
from setuptools import setup, Extension
from Cython.Build import cythonize
ext_modules = [
Extension(
"dotbug",
sources=["src/dotbug/dotbug-mre.pyx"],
include_dirs=['include'],
language="c++",
extra_compile_args=["-std=c++2a"],
),
]
setup(
name="dotbug", packages=["dotbug"], package_dir={'': 'src'},
ext_modules=cythonize(ext_modules, language_level="3"),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment