Chang Li | 12 May 16:05 2016
Picon

Cython: Workaround for nested typedefs

I am trying to wrap some C++ code with Cython.

The c++ header file "graph.h" contains the following definition:


   
#include "Block.h"
   
template <typename graphtype> class Graph
   
{
   
public:
           
typedef enum
           
{
                   SOURCE  
= 0,
                   SINK    
= 1
           
} termtype; // terminals
           
typedef int node_id;
           
typedef Block<double> Block_D;
   
           
Graph()
   
}


I tried the following in "Graph.pyx":

    cdef extern from "graph.h":
       cdef cppclass
Graph[graphtype]:
           ctypedef
int node_id
           ctypedef
enum termtype:
               SOURCE
               SINK
           ctypedef
Block<double> Block;

However, none of those work. I found that Cython might not support nested typedefs. If that's true, is there any workaround for this issue?

Thanks!

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Ivan Smirnov | 12 May 15:05 2016
Picon
Gravatar

mangle_dtype_name fails to mangle C++ structs (using memoryviews)

Here's the smalllest example that fails (only if you use memoryviews though; removing the last line will make it compile):

// foo.h
namespace foo { struct Bar { int x; }; }

cdef extern from "foo.h" namespace "foo":
   
struct Bar:
       
int x

import numpy as np
cdef
Bar[::1] bar = np.ones(1, 'int')

From the error message, looks like Cython fails to properly mangle the struct.

Looking at the source code (I'm using 0.24), it seems that "::" needs to be mangled as well (however using the underscore will make the names collide with valid names from the global namespace...).

# Cython/Compiler/Buffer.py:622
def mangle_dtype_name(dtype):
   
# Use prefixes to seperate user defined types from builtins
   
# (consider "typedef float unsigned_int")
   
if dtype.is_pyobject:
       
return "object"
   
elif dtype.is_ptr:
       
return "ptr"
   
else:
       
if dtype.is_typedef or dtype.is_struct_or_union:
            prefix
= "nn_"
       
else:
            prefix
= ""
        type_decl
= dtype.empty_declaration_code()
        type_decl
= type_decl.replace(" ", "_")
       
### !!!
       
### !!! should '::' be mangled as well here?
        ### !!!
       
return prefix + type_decl.replace("[", "_").replace("]", "_")


Here's the full error log:

test.cpp:1196:41: error: __Pyx_PyObject_to_MemoryviewSlice_dc_nn_struct_foo has not been declared
 
static CYTHON_INLINE __Pyx_memviewslice __Pyx_PyObject_to_MemoryviewSlice_dc_nn_struct_foo::Bar(PyObject *);
                                         
^
test
.cpp:1258:26: error: __Pyx_StructFields_nn_struct_foo has not been declared
 
static __Pyx_StructField __Pyx_StructFields_nn_struct_foo::Bar[] = {
                         
^
test
.cpp:1258:64: error: __Pyx_StructField Bar [] redeclared as different kind of symbol
 
static __Pyx_StructField __Pyx_StructFields_nn_struct_foo::Bar[] = {
                                                               
^
test
.cpp:1196:41: error: previous declaration of __Pyx_memviewslice Bar(PyObject*)
 
static CYTHON_INLINE __Pyx_memviewslice __Pyx_PyObject_to_MemoryviewSlice_dc_nn_struct_foo::Bar(PyObject *);
                                         
^
test
.cpp:1262:23: error: __Pyx_TypeInfo_nn_struct_foo has not been declared
 
static __Pyx_TypeInfo __Pyx_TypeInfo_nn_struct_foo::Bar = { "Bar", __Pyx_StructFields_nn_struct_foo::Bar, sizeof(struct foo::Bar), { 0 }, 0, 'S', 0, 0 };
                       
^
test
.cpp:1262:53: error: __Pyx_TypeInfo Bar redeclared as different kind of symbol
 
static __Pyx_TypeInfo __Pyx_TypeInfo_nn_struct_foo::Bar = { "Bar", __Pyx_StructFields_nn_struct_foo::Bar, sizeof(struct foo::Bar), { 0 }, 0, 'S', 0, 0 };
                                                     
^
test
.cpp:1196:41: error: previous declaration of __Pyx_memviewslice Bar(PyObject*)
 
static CYTHON_INLINE __Pyx_memviewslice __Pyx_PyObject_to_MemoryviewSlice_dc_nn_struct_foo::Bar(PyObject *);
                                         
^
test
.cpp:1262:68: error: __Pyx_StructFields_nn_struct_foo has not been declared
 
static __Pyx_TypeInfo __Pyx_TypeInfo_nn_struct_foo::Bar = { "Bar", __Pyx_StructFields_nn_struct_foo::Bar, sizeof(struct foo::Bar), { 0 }, 0, 'S', 0, 0 };
                                                                   
^
test
.cpp: In function PyObject* PyInit__cython_magic_84cef9641559271e5bc5e38348adbd3d():
test
.cpp:14190:15: error: __Pyx_PyObject_to_MemoryviewSlice_dc_nn_struct_foo has not been declared
   __pyx_t_3
= __Pyx_PyObject_to_MemoryviewSlice_dc_nn_struct_foo::Bar(__pyx_t_1);
               
^
test
.cpp: At global scope:
test
.cpp:17387:51: error: __Pyx_PyObject_to_MemoryviewSlice_dc_nn_struct_foo has not been declared
           
static CYTHON_INLINE __Pyx_memviewslice __Pyx_PyObject_to_MemoryviewSlice_dc_nn_struct_foo::Bar(PyObject *obj) {
                                                   
^
test
.cpp: In function __Pyx_memviewslice Bar(PyObject*):
test
.cpp:17398:51: error: __Pyx_TypeInfo_nn_struct_foo has not been declared
                                                 
&__Pyx_TypeInfo_nn_struct_foo::Bar, stack,


--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Alexander Eberspächer | 11 May 22:56 2016
Picon
Gravatar

Complex-valued math and compiler crashes

Dear list,


this small pyx file crashes the Cython compiler (Cython 0.24):


from libcpp.complex cimport exp as complex_exp

cpdef demonstrate_crash():
    cdef double complex imaginary_unit = 1j
    cdef double complex result

    result = complex_exp(imaginary_unit)

    return res


This crashes in the line with complex_exp call with "complex_crash.pyx:10:24: Compiler crash in AnalyseExpressionsTransform". The original error found in the trace is "AttributeError: 'CComplexType' object has no attribute 'template_type'".

What''s the correct way of using the complex math functions in libcpp.complex?


Thanks for your reply

Alex

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Jérôme Kieffer | 10 May 12:37 2016

Using the GIL to get atomic_add feature ...

Dear Cython community,

I am trying to write some parallel code using Cython and I need to
increment atomically a shared variable to define unique place in memory.
For this I use a global counter, locked with the GIL. The issue comes
when I try to retrieve (locally in the thread) its value ? 

Example:

cdef int counter

for i in prange(n):
   if condition(i):
       with gil:
           counter += 1
           if counter >= max_allocated:
               raise MemoryError("Not enough memory allocated"
       arry[counter] = smth

fails in "if counter >= max_allocated:" with error: 
Cannot read reduction variable in loop body

Thanks in advance.

Jérôme

--

-- 

--- 
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

rmrmg.chem | 9 May 08:48 2016
Picon

statically linked module

Hello,

I wrote python/cython module which requires shared object python module (which is NOT part of python std library). I want to build module which includes my code and that module (in other words I want to provide to users one .so file which combines my code and other .so module). Is it possible?

Regards,

Rafal

ps. I know this question is pretty weird and probably you want to ask 'why users cannot install that module by themself?'. They already have installed it but in different (incompatible) version.

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Degang WU | 8 May 13:04 2016
Picon

How to specify a particular gdb to cygdb on Mac OS X?

Hi,

I installed Python 3.5, Cython 0.24 and gdb 7.9.1 through Macports on Mac OS X 10.11.4. Since Macports renames gdb to ggdb, cygdb complains it cannot find gdb.

Traceback (most recent call last):

  File "/opt/local/bin/cygdb-3.5", line 8, in <module>

    cygdb.main()

  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Cython/Debugger/Cygdb.py", line 143, in main

    p = subprocess.Popen([options.gdb, '-command', tempfilename] + gdb_argv)

  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 950, in __init__

    restore_signals, start_new_session)

  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 1544, in _execute_child

    raise child_exception_type(errno_num, err_msg)

FileNotFoundError: [Errno 2] No such file or directory: 'gdb'


How can I specify the name of a particular gdb, i.e., ggdb, to cygdb?

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Joris Vankerschaver | 7 May 21:50 2016
Picon
Gravatar

tp_dictoffset/__dict__ access for cdef class

Hi all,

Is there a way to control tp_dictoffset for a cdef class? I am trying to rewrite a legacy C implementation in Cython, but the C implementation relies explicitly on tp_dictoffset to locate the attribute dictionary. I can get things to work with my current implementation by manually patching the generated C code, but obviously that's not a viable solution :-) If this is not possible, is there another way of endowing a cdef class with a __dict__?

Some googling uncovered a thread from 2013 where a similar question was discussed, but AFAICT this was never implemented. Please let me know if I'm overlooking something.

All the best,
Joris.

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
yselivanov.ml | 6 May 00:42 2016
Picon

inspect.signature support

Hi,

I remember a while ago I was working with Stefan on http://bugs.python.org/issue17159 (and a few other tickets) to make sure, that inspect.signature can work with Cython generated code.  However, I can't get Cython to make it work for my uvloop project (https://github.com/MagicStack/uvloop).

Is this a regression, or should I somehow configure Cython to make signatures work? "embedsignature" option doesn't help.

Thanks,

Yury Selivanov

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Pallav Gupta | 7 May 13:06 2016
Picon

Issue with ctypedef

I have the following:

cdef extern from 'Fc2Par.h':
    cdef cppclass Fc2Par[T]:
        Fc2Par(string&) except +
        vector[C_BoundingBox[T]] convert_boxes(vector[C_BoundingBox[T]]&)

ctypedef C_BoundingBox[double] Rect

cdef class PyFc2Par:
    cdef Fc2Par[double]* thisptr

    def __cinit__(self, par_file):
        self.thisptr = new Fc2Par[double](par_file)
    def __dealloc__(self):
        del self.thisptr
        
    def convert_boxes(self, boxes):
        cdef Rect r
        cdef vector[Rect] c, result
        for xmin,ymin,xmax,ymax in boxes:
            r.set(xmin, xmax, ymin, ymax)
            c.push_back(r)
            
        result = self.thisptr.convert_boxes(c)
        return [(r.xmin(), r.ymin(), r.xmax(), r.ymax()) for r in result]

When I try to build, I get the following error which I don't understand. Rect is a ctypedef, so why is cython complaining? How to fix?  If i explicitly say vector[C_BoundingBox[double]] c, result, it works fine. Why does it not work with ctypedef?

Error compiling Cython file:
------------------------------------------------------------
...
        cdef vector[Rect] c, result
        for xmin,ymin,xmax,ymax in boxes:
            r.set(xmin, xmax, ymin, ymax)
            c.push_back(r)
            
        result = self.thisptr.convert_boxes(c)
                                            ^
------------------------------------------------------------

coord.pyx:50:45: Cannot assign type 'vector[Rect]' to 'vector[C_BoundingBox[double]]'


--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Christian Meyer | 4 May 18:39 2016
Picon

Valgrind debugging: PyObject_Realloc and PyObject_Free errors despite suppression and unexplained "indirectly lost" bytes

Hi all,

Disclaimer: I am very new to cython, but I thought I would dive in head first and poke around to learn.

According to the README file if I uncomment the sections about PyObject_Realloc and PyObject_Free I shouldn't be getting errors arising from the python libraries; however, I still am. I feel like this isn't really an issue but I was curious why they would still be around.

Looking at the memory leak section, I have 0 bytes directly lost (yay), but 5Kb indirectly lost despite freeing all of the allocated pointers. I even get this when executing an empty function. I was wondering if this is coming from python itself or if I'm doing something wrong.

I did just a cursory search of cython and valgrind and didn't seem to find much about this so I thought I would ask here!

Thanks in advance!

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Alexander Bass | 4 May 16:49 2016
Picon

Exposing a c++ function to python using cython

I am trying to expose a c++ function to python using cython as follows:


from libcpp.vector cimport vector cdef extern from "auction.h": cpdef double util( int q, int sq, double alpha, double beta, double kappa, int s, int ss, int m, const vector[double]& p, double a0);

I am getting the following (not very descriptive) error:

src\RFQLib.pyx:4:21: Cannot convert 'double (int, int, double, double, double, int, int, int, const vector[double] &, double)' to Python object

What's the problem here?

Thanks in advance for all your help.

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gmane