Opened 5 years ago

#545 new defect

Compiler crash with C++ templates/buffers

Reported by: robertwb Owned by: dagss
Priority: major Milestone: wishlist
Component: Buffers Keywords:


My bad,

the error actually goes away when I change to void *:
- the return type of myFactory
- the ptr local variable of myFactory
- the ptr local variable of called_by_python

I also must explicit the template argument of myTemplateObject when doing the "new", like so:
    ptr = new cxx.myTemplateObject[float](<float *>

Sorry about that,


2010/6/18 Lionel Data <>

The C++ wrapper in Cython makes a pretty good job, even when using template variables. However I came across a strange error, and I don't know if I'm the one messing with cython too much, or if it is an actual bug. (I pulled the cython repository yesterday, changeset:   3394:37d40646533e)

So here it is (I hope you'll be able to reproduce it):

cimport mycppDecl as cxx # <- this is my .px file containing the declarations
cimport numpy as np

cdef myFactory(np.ndarray stuff):
    cxx.myTemplateObject[T] * ptr

    if stuff.dtype == np.dtype(np.float32):  # Is it the best way to do this btw ?
        ptr = new cxx.myTemplateObject(<float *>
        ptr = new cxx.myTemplateObject(<double *>
    return ptr

def called_by_python(np.ndarray stuff):
    cxx.myTemplateObject[T] *  ptr  # Error goes away with void * here
    ptr = myFactory(stuff)

This code produces an exception when cython tries to parse it (I think). It goes away when I change the type of ptr to void * in called_by_python().
here is the output of the error:

Traceback (most recent call last):
  File "./", line 25, in <module>
    ext_modules = ext_modules
  File "/home/lionel/work/ets/python26/lib/python2.6/distutils/", line 152, in setup
  File "/home/lionel/work/ets/python26/lib/python2.6/distutils/", line 975, in run_commands
  File "/home/lionel/work/ets/python26/lib/python2.6/distutils/", line 995, in run_command
  File "/home/lionel/work/ets/python26/lib/python2.6/distutils/command/", line 345, in run
  File "/home/lionel/work/cython/cython-devel/Cython/Distutils/", line 75, in build_extensions
    ext.sources = self.cython_sources(ext.sources, ext)
  File "/home/lionel/work/cython/cython-devel/Cython/Distutils/", line 197, in cython_sources
  File "/home/lionel/work/cython/cython-devel/Cython/Compiler/", line 749, in compile
    return compile_single(source, options, full_module_name)
  File "/home/lionel/work/cython/cython-devel/Cython/Compiler/", line 696, in compile_single
    return run_pipeline(source, options, full_module_name)
  File "/home/lionel/work/cython/cython-devel/Cython/Compiler/", line 593, in run_pipeline
    err, enddata = context.run_pipeline(pipeline, source)
  File "/home/lionel/work/cython/cython-devel/Cython/Compiler/", line 234, in run_pipeline
    data = phase(data)
  File "/home/lionel/work/cython/cython-devel/Cython/Compiler/", line 965, in __call__
    return super(AnalyseDeclarationsTransform, self).__call__(root)        
  File "/home/lionel/work/cython/cython-devel/Cython/Compiler/", line 276, in __call__
    return super(CythonTransform, self).__call__(node)
  File "/home/lionel/work/cython/cython-devel/Cython/Compiler/", line 259, in __call__
    return self.visit(root)
  File "/home/lionel/work/cython/cython-devel/Cython/Compiler/", line 28, in visit
    return handler_method(obj)
  File "/home/lionel/work/cython/cython-devel/Cython/Compiler/", line 973, in visit_ModuleNode
  File "/home/lionel/work/cython/cython-devel/Cython/Compiler/", line 63, in analyse_declarations
  File "/home/lionel/work/cython/cython-devel/Cython/Compiler/", line 341, in analyse_declarations
  File "/home/lionel/work/cython/cython-devel/Cython/Compiler/", line 341, in analyse_declarations
  File "/home/lionel/work/cython/cython-devel/Cython/Compiler/", line 1515, in analyse_declarations
    base_type = self.base_type.analyse(env)
  File "/home/lionel/work/cython/cython-devel/Cython/Compiler/", line 836, in analyse
    self.type = base_type.specialize_here(self.pos, template_types)
  File "/home/lionel/work/cython/cython-devel/Cython/Compiler/", line 1924, in specialize_here
    return self.specialize(dict(zip(self.templates, template_values)))
  File "/home/lionel/work/cython/cython-devel/Cython/Compiler/", line 1940, in specialize
    specialized.scope = self.scope.specialize(values)
  File "/home/lionel/work/cython/cython-devel/Cython/Compiler/", line 1605, in specialize
  File "/home/lionel/work/cython/cython-devel/Cython/Compiler/", line 350, in declare_type
    entry = self.declare(name, cname, type, pos, visibility)
  File "/home/lionel/work/cython/cython-devel/Cython/Compiler/", line 306, in declare
    if type.is_buffer and not isinstance(self, LocalScope):
AttributeError: 'NoneType' object has no attribute 'is_buffer'

Change History (0)

Note: See TracTickets for help on using tickets.