Bertrand Bordage | 7 Jul 16:00 2015
Picon

PyFloat_FromString and Python 2/3 compatibility

Hi!

I’m having some trouble using cpython.float.PyFloat_FromString, since its definition changed between Python 2 & 3 from PyFloat_FromString(PyObject *str, char **pend) to PyFloat_FromString(PyObject *str) (sources: for Python 2 & for Python 3).
The Cython source defines the Python 2 version, and claims that the second argument is ignored in Python 3. I tried using the NULL pointer as second argument, but when compiling with Python 3.4, I get an error because of that extra argument.

Am I missing something?

If not, then a solution would be to add PY_MAJOR_VERSION to the compilation constants environment (in these lines) and conditionally declare Python 2 and 3 versions of PyFloat_FromString in float.pxd.
It could also be a nice occasion to allow users to add their own compilation constants through the cython command and distutils.

Regards,
Bertrand

--

---
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.
Himanshu Mishra | 7 Jul 10:44 2015
Picon

Some good examples of C++ wrappers using classes

Hello,

I have to wrap an external C++ library, which is written for 13 years and is very mature. It's LEMON.

So, while wrapping up the classes, I sorta felt confused between the use of private public and protected keywords in C++. So, are there some good examples of such wrappers using Cython to wrap external C++ libraries?

Or, some extended documentation/tutorial will also be helpful.

Thank you very much. :)

--

---
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.
David Verelst | 6 Jul 09:41 2015
Picon

Wrapping Fortran derived types

Hi all,

I am trying to wrap a somewhat complex Fortran code base that heavily depends on derived types. I have a fair amount of experience with Python, but posses relatively little Cython and Fortran knowledge.

According to [1] it is possible to elegantly wrap Fortran derived types with Cython using iso_c_binding [2]. On Stackoverflow there is a nice Cython-Fortran example [3] that uses the iso_c_binding and works for me. Or another one [4][5]. There are also nice looking books [6][7] but I don't think they include such examples (correct me if I am wrong).

However, I have not been able to find a working example that includes a Fortran derived type wrapped with Cython. Has someone by any chance knowledge about a worked out example somewhere? Or maybe has some good pointers on how to advance?

I also bumped into f90wrap [6], and would like to see which approach suites our needs best (Cython or f90wrap).

I think it would be nice to have such an example (wrapped Fortran derived types) in the Cython documentation, and I am willing to contribute one once I know how exactly :-)

Best regards,
David

--

---
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.
Hiroaki Nakamura | 6 Jul 06:55 2015
Picon

How to use .encode("UTF-8") for a list of strings to C?

Hi all,

I tried the following example code, but I got a "Segmentation fault" error.
https://github.com/hnakamur/cygroonga/blob/7d711e2ce2df9cb48e239db062e62694b1cc3af0/example_index.py#L20-L23

My implemetation for this code is at:
https://github.com/hnakamur/cygroonga/blob/7d711e2ce2df9cb48e239db062e62694b1cc3af0/cygroonga.pyx#L608-L618

I have a list of Python strings and I need to convert them to byte strings and pass them to C.
Could you tell me how to do this please?

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.
Hiroaki Nakamura | 6 Jul 03:26 2015
Picon

Storing unsafe C derivative of temporary Python in malloc'ed arrays

Hi, all.

I tried to store C strings in malloc'ed arrays, but I got
"Storing unsafe C derivative of temporary Python" errors.

My full source code and full error messages are at:
https://github.com/hnakamur/cygroonga/commit/a60f451522b06e28340a3020ba2b78385499c310

Could you help me work around these errors please?
Thanks!

Hiroaki

--

---
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.
Loco Jay | 5 Jul 21:22 2015
Picon

std::reference_wrapper usage

Hi,

I would like to use a class from a library which uses std::reference_wrapper and std::ref.
I was not able to find it in libcpp and therefore created a file reference_wrapper.pxd with the following content


cdef extern from "<functional>" namespace "std" nogil:
    cdef cppclass reference_wrapper[T]:
        reference_wrapper()
        reference_wrapper(T& ref)
        reference_wrapper(T&&)
        reference_wrapper(const reference_wrapper[T]&)
        # reference_wrapper& operator=(const reference_wrapper& x)
        # operator T& ()
        T& get()
    reference_wrapper[T] ref(T &t)

include "reference_wrapper.pxd"
def test():
    cdef int a=2
    cdef reference_wrapper[int] refa
    refa = ref(a)

But unfortunately get

    reference_wrapper[T] ref(T &t)
                      ^
------------------------------------------------------------
reference_wrapper.pxd:11:23: unknown type in template argument

Any help to get a basic example working would be very helpful.
Many 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.
Markus Brachner | 3 Jul 16:19 2015
Picon

Bug when creating vector object in prange loop

I think I found some bug.
I use the current release version of Cython 0.22.1

For me this minimal example leads to a compilation error:
from cython.parallel import prange
from libcpp.vector cimport vector

ctypedef vector[float] pointvector
        
def test_parallel():
    cdef Py_ssize_t j
    cdef pointvector *pv
    
    for j in prange(0,10,nogil=True):
        pv = new pointvector()

The problem is the creation of the pointvector object with the libcpp vector. In a normal range loop the same compiles fine with Cython. It works fine with prange, if I use my own vector wrapper:
cdef extern from "<vector>" namespace "std":
    cpdef cppclass vector[T]:
        cppclass iterator:
            T operator*()
            iterator operator++()
            bint operator==(iterator)
            bint operator!=(iterator)
        vector() nogil
        void push_back(T&) nogil
        T& operator[](int)
        T& at(int)
        iterator begin()
        iterator end()
Can sombebody reproduce this?

--

---
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.
Jeroen Demeyer | 3 Jul 15:56 2015
Picon

Cython looks for .pxd files everywhere?

Hello Cython-users,

when I cythonize a file /path/to/package/foo.pyx, then Cython 
automatically looks for anything_in_sys_path/package/foo.pxd

I know this is documented behaviour, but I consider it a bug anyway.

More explanation:

There are two kinds of cimporting:
* implicit cimporting: when cythonizing foo.pyx, Cython looks for foo.pxd
* explicit cimporting: when the code contains "cimport"

It seems that Cython currently treats both of these in the same way. 
However, I do not understand why the implicit cimporting looks all over 
sys.path for the .pxd file. Is there an actual use case for that?

Here is my problem: If I install a .pxd file and then change my mind and 
delete that .pxd file from my source files, the installed .pxd file is 
still there and will be automatically cimported. I know that .so and .py 
files and everything are also kept in the installation directory even if 
I remove the corresponding sources. But those files are unlikely to 
break anything. But unwanted .pxd files do break stuff.

Jeroen.

--

-- 

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

Hiroaki Nakamura | 3 Jul 14:59 2015
Picon

How to return a new instance of a cdef'ed class?

Hi all,

I started learning Cython and tried to write a Python binding for
"groonga/groonga" https://github.com/groonga/groonga
using Cython.

However when I tried to return a new instance of a cdef'ed class,
I got a "Cannot convert '~~~' to Python object" error.

I have read Cython document http://docs.cython.org/index.html
and the Cython book from O'reilly, but I cannot find a way to fix this error.

Could you please help me how to fix this?

My source codes are at:
https://github.com/hnakamur/cygroonga/commit/3b2378597427d00af15e41eb35eb2090eba378f9
I copied full error messages to the commit message.

The groonga C header file's source code is at:
https://github.com/groonga/groonga/blob/v5.0.5/include/groonga/groonga.h

Any help or comment is welcome.

Thanks in advance!

---
Hiroaki Nakamura

--

---
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.
Ana Marija Sokovic | 2 Jul 22:08 2015
Picon

Cython + OpenMP

I have a quick question about using prange in my code:

# for a given integer p
# find all integer combinations of a b c for which
# a*a+b*b=c*c
# a+b+c=p
# where a<b<c

from cython.parallel import prange

def find_num_solutions(int p):
    cdef int a,b,c,n
    n=0
    for a in prange(1,p/2,nogil=True,schedule="guided"):

        for b in prange(a,p/2):
            c=p-a-b
            if(a*a+b*b==c*c):
                n=n+1

    return n

when I compile it via: python setup.py build_ext --inplace
I got error:local variable 'n' referenced before assignment

while when I use: for b in range(a,p/2) instead the code compiles without problem. So in this situation when I have nested loop can I use prange twice and how?

--

---
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.
Skip Montanaro | 2 Jul 19:44 2015
Picon

Using Cython as a Boost.Python replacement for C++ libraries?

Awhile ago I asked on comp.lang.python about using cffi as an interface to C++ libraries. Lele Gaifax pointed me to Cython. I glanced quickly at the doc page Lele referenced:

http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html

then got onto other things, and let that lay for awhile, but just bumped into some bogosities in some Boost.Python wrappers which made me think I should take a more serious look.

I just reread the above page. It's not at all clear to me how I'm supposed to create an interface to an existing C++ library. I don't want to compile the source to the library, just reveal some classes (and part of their public interfaces) to my Python code. I've used Cython up to this point to lightly boost some existing Python code, but haven't yet used it as a replacement for writing an extension module. I have pretty much zero experience with setup.py, even less with C++ or Boost.Python. I'm just looking for an easier way to write an extension module interfacing to some C++ libraries. Extension modules I can write, but I'm lazy, and was hoping Cython would offer a more Pythonic alternative. Any tutorials which reference this process appreciated. Maybe something like the Rectangle.cpp/h example which assumes that the C++ class code is external? I suspect it's right in front of my face, but I'm not looking at things in the right way.

Thanks,

Skip Montanaro

--

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