Skip to content

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

@stharding

Description

@stharding

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
{
public:
    my_vector() {
        _my_int_vector = new std::vector<int>();
    }
    ~my_vector() {
        delete _my_int_vector;
    }

    void push_back(int value) {
        _my_int_vector->push_back(value);
    }


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

private:
    std::vector<int>* _my_int_vector;
};

and this wrapper:

# cy_vec_wrapper.pyx
cdef extern from "vec_wrapper.hpp":
    cdef cppclass my_vector:
        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):
        self._wrapper.push_back(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 setup.py 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/Main.py:367: 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 "setup.py", line 12, in <module>
    language='c++',           # generate C++ code
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Build/Dependencies.py", line 1086, in cythonize
    cythonize_one(*args)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Build/Dependencies.py", 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/Main.py", line 725, in compile_single
    return run_pipeline(source, options, full_module_name)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/Main.py", line 513, in run_pipeline
    err, enddata = Pipeline.run_pipeline(pipeline, source)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/Pipeline.py", line 355, in run_pipeline
    data = run(phase, data)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/Pipeline.py", line 335, in run
    return phase(data)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/Pipeline.py", line 52, in generate_pyx_code_stage
    module_node.process_implementation(options, result)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/ModuleNode.py", line 143, in process_implementation
    self.generate_c_code(env, options, result)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/ModuleNode.py", line 379, in generate_c_code
    self.body.generate_function_definitions(env, code)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/Nodes.py", line 442, in generate_function_definitions
    stat.generate_function_definitions(env, code)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/Nodes.py", 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/Nodes.py", line 442, in generate_function_definitions
    stat.generate_function_definitions(env, code)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/Nodes.py", line 3173, in generate_function_definitions
    FuncDefNode.generate_function_definitions(self, env, code)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/Nodes.py", line 1983, in generate_function_definitions
    self.generate_function_body(env, code)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/Nodes.py", line 1745, in generate_function_body
    self.body.generate_execution_code(code)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/Nodes.py", line 448, in generate_execution_code
    stat.generate_execution_code(code)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/Nodes.py", line 5169, in generate_execution_code
    self.generate_assignment_code(code)
  File "/home/harding/.local/lib/python2.7/site-packages/Cython/Compiler/Nodes.py", 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/ExprNodes.py", 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions