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

Compiler crash when constructor is missing #3193

Open
mje-nz opened this issue Oct 17, 2019 · 2 comments

Comments

@mje-nz
Copy link
Contributor

@mje-nz mje-nz commented Oct 17, 2019

cdef extern from *:
    """class Foo {
    public:
        Foo() {}
    };
    """
    cdef cppclass Foo:
        pass

def create_foo():
    Foo()

This gives

Error compiling Cython file:
------------------------------------------------------------
...
    """
    cdef cppclass Foo:
        pass

def create_foo():
    Foo()
   ^
------------------------------------------------------------

foo.pyx:11:4: no constructor found for C++  type 'Foo'

Error compiling Cython file:
------------------------------------------------------------
...
    """
    cdef cppclass Foo:
        pass

def create_foo():
    Foo()
      ^
------------------------------------------------------------

foo.pyx:11:7: Compiler crash in OptimizeBuiltinCalls

ModuleNode.body = StatListNode(foo.pyx:1:0)
StatListNode.stats[1] = DefNode(foo.pyx:10:0,
    modifiers = [...]/0,
    name = 'create_foo',
    np_args_idx = [...]/0,
    outer_attrs = [...]/2,
    py_wrapper_required = True,
    reqd_kw_flags_cname = '0',
    used = True)
DefNode.body = StatListNode(foo.pyx:11:7)
StatListNode.stats[0] = ExprStatNode(foo.pyx:11:7)
ExprStatNode.expr = SimpleCallNode(foo.pyx:11:7,
    use_managed_ref = True)

Compiler crash traceback from this point on:
  File "/Users/matthew/Code/ThirdParty/cython/Cython/Compiler/Visitor.py", line 180, in _visit
    return handler_method(obj)
  File "/Users/matthew/Code/ThirdParty/cython/Cython/Compiler/Visitor.py", line 514, in visit_SimpleCallNode
    if function.type.is_pyobject:
AttributeError: 'NoneType' object has no attribute 'is_pyobject'

The docs say it's not necessary to declare the constructor in this case, am I doing something wrong?

@scoder

This comment has been minimized.

Copy link
Contributor

@scoder scoder commented Oct 18, 2019

Agreed that it shouldn't crash (so this is definitely a bug), but shouldn't you use new Foo() ?

@mje-nz

This comment has been minimized.

Copy link
Contributor Author

@mje-nz mje-nz commented Oct 18, 2019

I was expecting f = Foo() to default-construct a Foo on the stack, like cdef Foo f or the C++ auto f = Foo() would, and then I minimised the example to just Foo().

The code I started from was this, which crashes the compiler if the default constructor for std::greater is not declared:

from libcpp cimport bool 
from libcpp.algorithm cimport sort 
from libcpp.vector cimport vector 

cdef extern from "<functional>" namespace "std" nogil: 
    cdef cppclass greater[T=*]: 
        greater() except +  # remove this to crash compiler
        bool operator()(const T& lhs, const T& rhs) except + 

def sort_ints_reverse(vector[int] values): 
    """Test sort using a standard library comparison function object. 
 
    >>> sort_ints_reverse([5, 7, 4, 2, 8, 6, 1, 9, 0, 3]) 
    [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] 
    """ 
    sort(values.begin(), values.end(), greater[int]()) 
    return values 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.