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

except + broken for T& operator[](T) in 0.29 #2671

stharding opened this issue Oct 20, 2018 · 1 comment

except + broken for T& operator[](T) in 0.29 #2671

stharding opened this issue Oct 20, 2018 · 1 comment


Copy link

stharding commented Oct 20, 2018

Just updated to 0.29 and my codebase no longer builds. I tracked it down to the use of except + on an operator[] declaration. Here is a minimal example that illustrates the problem:

given this c++ class:

// vec_wrapper.hpp
#include <vector>

class my_vector
    my_vector() {
        _my_int_vector = new std::vector<int>();
    ~my_vector() {
        delete _my_int_vector;

    void push_back(int value) {

    int& operator[](int index) {
        return (*_my_int_vector)[index];

    std::vector<int>* _my_int_vector;

and this wrapper:

# cy_vec_wrapper.pyx
cdef extern from "vec_wrapper.hpp":
    cdef cppclass my_vector:
        int push_back(int)
        int& operator[](int) except +

cdef class cy_vec_wrapper:
    cdef my_vector* _wrapper
    def __init__(self):
        self._wrapper = new my_vector()

    def __dealloc__(self):
        del self._wrapper

    cpdef append(self, int val):

    def __getitem__(self, index):
        return self._wrapper[0][index]

    def __setitem__(self, index, int value):
        self._wrapper[0][index] = <int>value

I get:

python build_ext
Compiling cy_vec_wrapper.pyx because it changed.
[1/1] Cythonizing cy_vec_wrapper.pyx
/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/ FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /home/harding/tmp/vec_wrapper/cy_vec_wrapper.pyx
  tree = Parsing.p_module(s, pxd, full_module_name)
Traceback (most recent call last):
  File "", line 12, in <module>
    language='c++',           # generate C++ code
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Build/", line 1086, in cythonize
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Build/", line 1192, in cythonize_one
    result = compile_single(pyx_file, options, full_module_name=full_module_name)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/", line 725, in compile_single
    return run_pipeline(source, options, full_module_name)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/", line 513, in run_pipeline
    err, enddata = Pipeline.run_pipeline(pipeline, source)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/", line 355, in run_pipeline
    data = run(phase, data)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/", line 335, in run
    return phase(data)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/", line 52, in generate_pyx_code_stage
    module_node.process_implementation(options, result)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/", line 143, in process_implementation
    self.generate_c_code(env, options, result)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/", line 379, in generate_c_code
    self.body.generate_function_definitions(env, code)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/", line 442, in generate_function_definitions
    stat.generate_function_definitions(env, code)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/", line 4839, in generate_function_definitions
    self.body.generate_function_definitions(self.scope, code)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/", line 442, in generate_function_definitions
    stat.generate_function_definitions(env, code)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/", line 3173, in generate_function_definitions
    FuncDefNode.generate_function_definitions(self, env, code)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/", line 1983, in generate_function_definitions
    self.generate_function_body(env, code)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/", line 1745, in generate_function_body
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/", line 448, in generate_execution_code
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/", line 5169, in generate_execution_code
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/", line 5466, in generate_assignment_code
    self.lhs.generate_assignment_code(self.rhs, code)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/", line 4105, in generate_assignment_code
    self.result() if self.lhs.is_pyobject else None,
AttributeError: 'IndexNode' object has no attribute 'lhs'

Removing the except + from the operator[] results in a successful build.

This builds fine with or without the except + in cython 0.28.5

@stharding stharding changed the title except + broken for type& operator[](type) in 0.29 except + broken for T& operator[](T) in 0.29 Oct 20, 2018
@scoder scoder added the defect label Nov 18, 2018
@scoder scoder added this to the 0.29.1 milestone Nov 18, 2018
Copy link

scoder commented Nov 24, 2018

Thanks for the report and the reproducer.

@scoder scoder closed this as completed in 3865311 Nov 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

No branches or pull requests

2 participants