Picon

How to wrap function returning C++ objects allocated on stack?

Hi!
I'm creating a python binding of a cpp library using Cython. (I can't change it's source. I only have headers and .so libraries)

There are two classes, say ClassA and FactoryClassA. They are defined like this:

// start of spam.h:
class ClassA
{
     ClassA();
     //some methods
private:
     ClassA(int)
     //some fields
}
class FactoryClassA{
     FactoryClassA();
     ClassA factory();
     //other methods
}
//EOF spam.h

To create objects of ClassA I can only use FactoryClassA::factory() method (if ClassA objects are created otherwise they become unuseable).
How can I create Cython binding for spam.h?

--

---
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.
Sid | 8 Jul 16:49 2014
Picon

Cython being slower than Julia

Hi,

I came across this blog post (http://tullo.ch/articles/python-vs-julia/?) where for the PAVA algorithm (the upper half of the post), Cython is 1.1x - 4x slower than Julia. (The Active set implementation doesn't fully use Cython and numpy so I don't count that).

I thought Cython was close to C++ level speeds usually so this surprised me. Are there any optimizations that the PAVA Cython implementation is missing that could make things faster for Cython?

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.
mir amicitas | 8 Jul 10:56 2014
Picon

Segmentation faults when wrapping a c++ class with a public vector member

I am trying to wrap a c++ class, but am running into segmentation faults whenever a vector is encountered as a class member.

The c++ code I am trying to wrap is as follows:

program.h:
    
#include <vector>

using namespace std;

class LEDGarden {
 public:
  vector <int> the_state;
  void init(void);
};


program.cpp:

#include "program.h"

void LEDGarden::init(void) {

  for (int ii=0; ii<5; ii++) {
    //Segmentation fault happens here.
    the_state.push_back(0);
  }
}

int main() {
  LEDGarden garden;
  garden.init();
}


program.pyx:


cdef extern from "LEDGardenC/LEDGarden.h":
    cdef cppclass LEDGarden:
        void init()

cdef class PyLEDGarden:
    cdef LEDGarden *thisptr

    def init(self):
        self.thisptr.init()

This code compiles and runs fine if I use g++ or clang++ directly.  If I move the vector declaration into program.cpp, everything works fine in cython, but it is then no longer a member variable:

program.h (working version):
    
#include <vector>

using namespace std;

class LEDGarden {
 public:
  void init(void);
};


program.cpp:

#include "program.h"

void LEDGarden::init(void) {

  vector <int> the_state;
  for (int ii=0; ii<5; ii++) {
    //Segmentation fault happens here.
    the_state.push_back(0);
  }
}

int main() {
  LEDGarden garden;
  garden.init();
}


As far as I know, my c++ code is ok, though if there is an error please let me know.   Any help resolving this issue would be greatly appreciated.

Sincerely,
Novimir

--

---
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.
Nick Repole | 8 Jul 03:00 2014
Picon

Wrapped C++ class contains another wrapped class?

Let's say I have these two simple classes defined in C++ :

Location:
    double x
    double y

Circle
    Location location
    double radius

In my .pyx, I have something like:

cdef class PyLocation:
    cdef Location* thisptr      # hold a C++ instance which we're wrapping
    def __cinit__(self, x=None, y=None):
         if x is None or y is None:
            self.thisptr = new Location()
        else:
            self.thisptr = new Location(x, y)
    
    def get_x(self):
        return self.thisptr.x
    
    def set_x(self, value):
        self.thisptr.x = value
    
    x = property(get_x, set_x, None, None)
    
    def get_y(self):
        return self.thisptr.y
    
    def set_y(self, value):
        self.thisptr.y = value
    
    y = property(get_y, set_y, None, None)
    def __dealloc__(self):
        if self.thisptr is not NULL:
            del self.thisptr

Now when I define my Circle wrapper:

 cdef class PyCircle:
    cdef Circle* thisptr
    def __cinit__(self):
         self.thisptr = new Circle()
    
    def get_location(self):
        #I would like to return a PyLocation wrapped version of self.thisptr.location here
        return self.location
    def __dealloc__(self):
        if self.thisptr is not NULL:
            del self.thisptr

So, is there an easy way of returning a wrapped version of PyCircle.thisptr.location? Everything I've tried has left me with unfortunate memory management issues.

--

---
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.
Sid | 8 Jul 04:49 2014
Picon

Automatic translation of python to cython

Hi,

Is there a tool out there than can do type inference on python (that doesn't use unsupported constructs like generators, ec.) and automatically convert it into some form of cython?

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.
Ian Bell | 6 Jul 18:43 2014
Picon

Windows wheels

Anyone interested in windows wheels?  I can build (nightly) wheels for 2.7, 3.3, 3.4 32bit/64bit using anaconda if someone can tell me how to get authenticated for pypi for Cython.  You'd just have to ping me for a rebuild, but I'm happy to do it.

Ian

--

---
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.
Zahra Sheikh | 3 Jul 20:16 2014
Picon

Re: Re: Inconsistency of cython_gsl function with a class instance

my setup.py is as given :
# -*- coding: utf-8 -*-
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext



import numpy
import sys
 
ext = Extension("gsl", ["gsl.pyx"],
    include_dirs=[numpy.get_include(),
                  "/usr/include/"],
    library_dirs=["/usr/lib/"],
    libraries=["gsl"],
    language='c++')
 
setup(ext_modules=[ext],
    cmdclass = {'build_ext': build_ext})

and I moved "ctypedef gsl_function_struct gsl_function" lines inside the extern block "gsl/gsl_math.h" but I still get this out put and this error:
running build_ext
cythoning gsl.pyx to gsl.cpp
building 'gsl' extension
gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/vol/aibn84/aibn84_2/zahra/anaconda/lib/python2.7/site-packages/numpy/core/include -I/usr/include/ -I/vol/aibn84/aibn84_2/zahra/anaconda/include/python2.7 -c gsl.cpp -o build/temp.linux-x86_64-2.7/gsl.o
cc1plus: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
gsl.cpp: In function ‘PyObject* __pyx_pf_3gsl_10MyCallback_4call_gsl(__pyx_obj_3gsl_MyCallback*)’:
gsl.cpp:1113: error: cannot convert ‘gsl_integration_workspace*’ to ‘__pyx_t_3gsl_gsl_integration_workspace*’ in assignment
gsl.cpp:1140: error: cannot convert ‘__pyx_t_3gsl_gsl_integration_workspace*’ to ‘gsl_integration_workspace*’ for argument ‘8’ to ‘int gsl_integration_qag(const gsl_function*, double, double, double, double, size_t, int, gsl_integration_workspace*, double*, double*)’
gsl.cpp:1169: error: cannot convert ‘__pyx_t_3gsl_gsl_integration_workspace*’ to ‘gsl_integration_workspace*’ for argument ‘1’ to ‘void gsl_integration_workspace_free(gsl_integration_workspace*)’
error: command 'gcc' failed with exit status 1

I don't know how the " gsl_integration_workspace" should be defined in first place??!!

Regards,
Zahra 

On Thursday, 3 July 2014 18:34:49 UTC+2, Robert Bradshaw wrote:
On Thu, Jul 3, 2014 at 9:30 AM, Zahra Sheikh <sheikh... <at> gmail.com> wrote:
> Hi,
>
> How should I include the gsl library in my setup.py script? Or it also
> should be done in .pyx file too?

https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=distutils%20link%20library

--

---
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.
Attachment (gsl.pyx): application/octet-stream, 2497 bytes
Robert Ekendahl | 1 Jul 22:17 2014

How to convert to std::string from python string in cython 0.20.2 and python3?

Hi,

I posted a question over at stack overflow but figured I should ask it here as well. It feel like a somewhat basic question but I’m getting stuck so need some ideas for what to test.

You can see the answer at: http://stackoverflow.com/q/24518246/3757019

I’m posting the same info here:

I’m working on wrapping my first few C++ classes in Cython and was lucky to get through the first few simple ones. Now I’m getting a bad_ptr error in the constructor for a C++ class that takes in a std::string name argument. I have not been able to correctly figure out how to convert a Python string to a C++ std::string. Below is what I’m currently doing. What do I need to change?

For information I use latest cython 0.20.2, python 3.4 (32bit) on a Windows 7 machine and I use Visual Studio 2010 for compilation. I suspect the python 3.x could play into it since they have mucked with string definitions but I’m not sure how.

Here is the CPP Class definition I have:

class HDMISOURCE_API Source: public TipInterface { public: Source(std::string name, Interface* env); ...

I want to expose this class to python through a cython class that simply takes in a name argument (and keeps the pointer internally).

Here is the relevant cython code I have:

from libcpp.string cimport string cdef extern from "EnvironmentSelect.h": _TipUserEnvironmentInterface* _GetEnvironment "GetEnvironment" () cdef extern from "HDMISource.h": cdef cppclass _HDMISource "HDMISource": _HDMISource(string, _TipUserEnvironmentInterface*) except + cdef class HDMISource: cdef _HDMISource* thisptr def __cinit__(self, string pin_map): env = _GetEnvironment() self.thisptr = new _HDMISource(pin_map, env) def __dealloc__(self): del self.thisptr def hello(self): print('HDMISource still says hello!')

To debug this I compile using Visual Studio 2010 and with debug flags. I then start python and from visual studio do attach to process. Then from the python prompt I do this:

c:\Projects\psvm\python\tip>python Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import tip >>> name = 'MyHDMITxPins'.encode('UTF-8') >>> type(name) <class 'bytes'> >>> hdmi = tip.HDMISource(name)

At this point I get a break point in the generated code (tip.cpp) and at that level the name is a std::basic_str with the correct value. I wonder if std::basic_str is what I want it to be though…

If I step through some of the code I arrive at the class I expect:

HDMISource::HDMISource(std::string pinMap, TipUserEnvironmentInterface* env): TipInterface(pinMap, dynamic_cast<TipEnvironmentInterface*>(env)), m_videoFrameNum(0) { ... }

But at this point the std::string PinMap reports as bad_ptr so somehow I’ve lost the string on my way down the hierarchy… (And I was so convinced it would be the other class pointer that would be the issue and it seem fine!).

If I let it run I get this in the python window:

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "tip.pyx", line 60, in tip.HDMISource.__cinit__ (tip.cpp:1201) self.thisptr = new _HDMISource(pin_map, env) RuntimeError: Unknown exception >>>

It feels like I need to do something more in the cython code to convert to/from python string and std::str but I can’t figure out what. Please help! I will be forever grateful!


--

---
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.
Lars Buitinck | 3 Jul 16:51 2014
Picon

Bug in Cython's C++ support with operator[] and templates

Hi all,

I think I just found a bug in the C++ support of Cython 0.20.2:

// foo.hpp

template <typename T>
struct Foo {
    T value;

    Foo(T x) : value(x)
    {
    }

    T &operator[](int)
    {
        return value;
    }
};

# bar.pyx

cdef extern from "foo.hpp":
    cdef cppclass Foo[T]:
        Foo(int)
        T &operator[](int)

cdef class Bar:
    cdef Foo[int] *me

    def __cinit__(self):
        self.me = new Foo[int](1)

    def get(self):
        cdef int r = self.me[0]
        return r

Trying to compile this ends in

$ cython --cplus a.pyx
Error compiling Cython file:
------------------------------------------------------------
...

    def __cinit__(self):
        self.me = new Foo[int](1)

    def get(self):
        cdef int r = self.me[0]
                           ^
------------------------------------------------------------

bar.pyx:14:28: Cannot assign type 'Foo[int]' to 'int'

It seems like Cython infers the wrong type for operator[]'s return
value: Foo[int]& instead of int&. AFAICT, this only occurs with
operator[], not ordinary member functions, so if I rename operator[]
to get it works fine (but the library I'm wrapping exposes
operator[]). The same behavior can be observed with std::vector:

# or "from libcpp.vector cimport vector"
cdef extern from "vector":
    cdef cppclass vector[T]:
        vector(size_t)
        T &operator[](size_t)

cdef class Vector:
    cdef vector[int] *me

    def get(self, int i):
        cdef int x = self.me[i]
        return x

Crashes with

$ cython --cplus vec.pyx

Error compiling Cython file:
------------------------------------------------------------
...

cdef class Vector:
    cdef vector[int] *me

    def get(self, int i):
        cdef int x = self.me[i]
                           ^
------------------------------------------------------------

vec.pyx:10:28: Cannot assign type 'vector[int]' to 'int'

Removing the & doesn't change this, so it seems to be the operator
overloading support that is broken. I know I can work around it by
introducing a freestanding function that calls operator[] for me, but
I thought I'd report it anyway.

Regards,
Lars

--

-- 

--- 
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.

matthew | 1 Jul 17:29 2014
Picon

Cythonising a particular data structure and operation

Hello

I am coming over from python (so knew nothing about C, static types etc), because of a need to speed up a scientific computing type project.

Essentially I am representing sparse matrices as lists of cells with this sort of structure in python:

[(index_i,index_j,value)]

It is possible to add two matrices by merging two of these lists, sorting the result, and adding combining any tuples that share indices (and counting various other useful things during the process). This has proved to be much quicker than using scipy.sparse. 

I have cythonised the operation by cdef declaring the lists and the tuples. I can't help suspecting that when these data structures are passed from cdef to def style functions that overheads will be introduced. 

I'd post the code, but there's a fair amount and mostly unrelated to this design issue. Essentially my question is where to go to learn how to think about this sort of issue. I know how to represent it in python but not how to most efficiently convert the data structures into static C(ython). Classes/data types/leave it as is/go learn C and come back.

The docs don't give many clues as they assume a strong knowledge of C, it would be great to know about any project for explaining how to approach Cython from the python direction.

Anyway if you got this far, thanks for any tips.

--

---
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.
Kurt Smith | 30 Jun 01:18 2014
Picon

Regression with cimport of cython.operator.dereference, Cython 0.20.2

Hi all,

Is this a regression with cython.operator.dereference?

$ cython --version
Cython version 0.20.2
$ cat e_dereference.pyx
from cython.operator cimport dereference

def foo():
    cdef int *i
    cdef int j, k
    j = 10
    i = &j
    k = dereference(i)
$ cython e_dereference.pyx

Error compiling Cython file:
------------------------------------------------------------
...
def foo():
    cdef int *i
    cdef int j, k
    j = 10
    i = &j
    k = dereference(i)
                  ^
------------------------------------------------------------

e_dereference.pyx:10:19: undeclared name not builtin: dereference

Error compiling Cython file:
------------------------------------------------------------
...
def foo():
    cdef int *i
    cdef int j, k
    j = 10
    i = &j
    k = dereference(i)
                    ^
------------------------------------------------------------

e_dereference.pyx:10:21: Cannot convert 'int *' to Python object

If I change the cimport to

    from cython.operator cimport dereference as deref

or even

    from cython.operator cimport dereference as dereference

things work, and the correct code is generated.

I can of course use the k = i[0] syntax; just wanted to point out the above issue.

--

---
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