Jonathan Hanke | 4 Nov 08:50 2014
Picon

GMP "Symbol not found" Error Question

Hi,

I'm trying to use cython with GMP 6.0.0 to wrap some C++ libraries, and I'm getting the error

In [2]: from my_test2 import PyMatrix_mpz
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-2-b378b34db57e> in <module>()
----> 1 from my_test2 import PyMatrix_mpz

ImportError: dlopen(./my_test2.so, 2): Symbol not found: __ZlsRSoPK12__mpz_struct
  Referenced from: ./my_test2.so
  Expected in: flat namespace
 in ./my_test2.so

when I try to import the cythonized PyMatrix_mpz class.  This error resulted when I added some C++ routines that made an explicit call to the C-level GMP routines, but I'm not sure why this should be a problem since I include and link against both the gmp and gmpxx libraries, which are installed in /usr/local/bin.  I'm using GMP 6.0.0 on a MacBook Air, building with "make clean; make build" and have files that look like:

my_test2.pyx:
===========
# distutils: language = c++
# distutils: sources = ['misc_utilities.cpp', 'test2.cpp', 'call_operators.cpp', 'useful_tests.cpp', 'local_normal.cpp']
# distutils: include_dirs = ['/usr/local/include']
# distutils: libraries = ['gmp', 'gmpxx', 'm']
# distutils: library_dirs = ['/usr/local/lib']
....

setup.py:
=======
from distutils.core import setup
from Cython.Build import cythonize

setup(
    name = "QFLIBapp",
    ext_modules = cythonize('*.pyx'),
)

Makefile:
=======
build:
        python setup.py build_ext --inplace

clean:
        rm -rf build/
        rm -f *.so


Any comments are appreciated!  Thanks,

-Jon

--

---
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.
Daniele Nicolodi | 2 Nov 17:07 2014
Picon

Allocating c++ object on the stack

Hello,

there is a way in cython to allocate on the stack a c++ object that has
a constructor with arguments?  Example:

cdef extern from "example.h":
    cppclass Foo:
        Foo()
        Foo(int v)

# this works fine
cdef Foo f1
# this results in an error
cdef Foo f2(25)
# those also work fine
cdef Foo *f3 = new Foo(1)
cdef Foo *f4 = new Foo(1)

the error is:

example.pyx:14:12: Expected an identifier, found 'INT'

Thank you!

Cheers,
Daniele

--

-- 

--- 
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 | 31 Oct 13:39 2014
Picon

[cython-users] Regression in v0.21.1: ‘__pyx_convert_PyStr_string_to_py_std__string’ was not declared in this scope

It looks like "c_string_type = str" interferes with the new string conversion logic in v0.21.1, see the minimal example below:

#cython: c_string_type = str
#cython: c_string_encoding = utf-8

from libcpp.string cimport string

cdef string s = 'foo'
print '%s' % s

$ cython --cplus test.pyx
$ g++ -I/home/smirnov/anaconda/include/python2.7 -c test.cpp

test.cpp: In function ‘void inittest()’:
test.cpp:964: error: ‘__pyx_convert_PyStr_string_to_py_std__string’ was not declared in this scope

This compiles (and works) just fine in v0.21.0; in v0.21.1 it only compiles if one removes c_string_* qualifiers.

--

---
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 | 30 Oct 11:10 2014
Picon

PyInt_FromSize_t is missing from int.pxd

This is missing from the cpython/int.pxd file:

PyInt_FromSize_t(size_t ival)

and also:

int PyInt_ClearFreeList()

--

-- 

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

Marc Schlaich | 30 Oct 08:08 2014
Picon

Cannot overload in a custom cppclass

Hello,

I want to write a custom cppclass with overloading but this fails with "Function signature does not match previous declaration". Simple Example:


    cdef cppclass OverloadTest:

        float area():
            return 0.0

        float area(int i):
            return 0.5 * i


Is this a bug or just not supported?

Regards, Marc

--

---
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.
Daniele Nicolodi | 29 Oct 19:10 2014
Picon

C symbol renaming in a namespace

Hello,

given a class Bar defined in namespace foo:

namespace foo
{
    class Bar {
    public:
	Bar() {  };
    };
};

and the Cython code:

from libcpp.string cimport string

cdef extern from "example.h" namespace "foo":
    cppclass _Bar "Bar":
       pass

cdef class Bar:
    cdef _Bar *bar
    def __cinit__(self):
        self.bar = new _Bar()

I obtain a compilation error:

example.cpp:555:3: error: 'Bar' does not name a type
   Bar *bar;
   ^
...

If I change the Cython declaration to:

cdef extern from "example.h" namespace "foo":
    cppclass _Bar "foo::Bar":
       pass

everything works as expected. However I would expect that the fact that
the declaration of the namespace happens in the cdef extern ... line
makes the specification of the namespace in the symbol name redundant.

What do other things?  Is this a bug?

Cheers,
Daniele

--

-- 

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

Daniele Nicolodi | 29 Oct 14:02 2014
Picon

C++ class inheritance

Hello,

I'm wrapping with Cython a vast C++ project where a large number of
classes inherit from a limited number of base classes.  To avoid
repeating myself too much I would like to exposed the interface
described by the base classes using class inheritance in Cython as well.
However I haven't found a way to do that that does not requires some
ugly explicit type casting.

A simple example. Given the C++ class definitions:

class Base {
private:
    std::string _name;
public:
    Base(std::string n) : _name(n) { };
    std::string name() { return _name; };
};

class Example : public Base {
public:
    Example(std::string n) : Base(n) { };
    int foo(int a, int b) { return (a + b); };
};

I can write the following Cython:

cdef extern from "example.h":
    cppclass _Base "Base":
        _Base(string n)
        string name()
    cppclass _Example "Example":
        _Example(string n)
        string name()
        int foo(int a, int b)

cdef class Base:
    cdef _Base *obj
    def __cinit__(self, n):
        self.obj = new _Base(n)
    def name(self):
        return self.obj.name()

cdef class Example:
    cdef _Example *obj
    def __cinit__(self, name):
        self.obj = new _Example(name)
    def name(self):
        return self.obj.name()
    def foo(self, a, b):
        return self.obj.foo(a, b)

What I would like to do is to avoid to have to repeat the declaration of
the inherited methods. My solution is the following:

from libcpp.string cimport string

cdef extern from "example.h":
    cppclass _Base "Base":
        _Base(string n)
        string name()
    cppclass _Example "Example":
        _Example(string n)
        int foo(int a, int b)

cdef class Base:
    cdef _Base *obj
    def __cinit__(self, n):
        self.obj = new _Base(n)
    def name(self):
        return self.obj.name()

cdef class Example(Base):
    def __cinit__(self, name):
        self.obj = <_Base *>(new _Example(name))
    def foo(self, a, b):
        return (<_Example *>self.obj).foo(a, b)

which works fine (as for as I can tell), but the explicit type castings
really look ugly. I don't have much C++ or Cython experience, there is a
better way of doing what I'm trying to do?

Thank you!

Cheers,
Daniele

--

-- 

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

Abbeville | 28 Oct 21:26 2014
Picon

Cython and imported external libraries

If a Python module imports an external library (eg. Requests or Flask) then is it cythoniz'ed along with the module or is it left as-is?

--

---
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 Vedeneev | 28 Oct 18:25 2014
Picon

Out of bounds on buffer after numpy.resize

Hello all

I have found strange behavior of cython code after calling numpy.ndarray.resize:

Simple code:

%%cython
import numpy as np
cimport numpy as np
cimport cython
def test():
    cdef np.ndarray[np.int64_t, ndim=1] x 
    x = np.array([1,2,3,4])
    x.resize(x.size + 10, refcheck=False)
    print x
    print len(x)
    print x[4]

Output of test():
[1 2 3 4 0 0 0 0 0 0 0 0 0 0] 14
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-57-ea594c21b25d> in <module>() ----> 1 test() /home/ubertrader/.cache/ipython/cython/_cython_magic_84b75606843843e3fe982579e029eb5a.so in _cython_magic_84b75606843843e3fe982579e029eb5a.test (/home/ubertrader/.cache/ipython/cython/_cython_magic_84b75606843843e3fe982579e029eb5a.c:1421)()  
IndexError: Out of bounds on buffer access (axis 0) 

Is that bug or I'm doing something wrong ? Is there workaround ?

Thx, all

--

---
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.
yi huang | 28 Oct 10:05 2014
Picon

How to convert python integer object to c int types safely?

Say i want to optimize int to decimal bytestring convertion, so i have:

cdef bytes size_to_decimal_bytes(long n):
    pass

cdef bytes int_to_decimal_bytes(n):
    if isinstance(n, int):
        return size_to_decimal_bytes(n)
    elif isinstance(n, long):
        return bytes(str(n), 'latin-1')
    raise ValueError

But in python3, `isinstance(n, int)` is just the same as `isinstance(n, long)`, so when i pass a big int to `int_to_decimal_bytes`, it raises OverflowError.

How to make this compatiable with both python2 and python3 ?

--
http://yi-programmer.com/

--

---
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.
Mark Fornace | 27 Oct 03:56 2014
Picon

Adding the __add__ operator to a Cython wrapper class?

I also posted this on Stack Overflow, but I figured this board might be more helpful:

The following C++ code goes off the standard Cython rectangle example, with an arbitrary added "+" operator:

#include "Rectangle.h" using namespace shapes; Rectangle::Rectangle(int X0, int Y0, int X1, int Y1) { x0 = X0; y0 = Y0; x1 = X1; y1 = Y1; } Rectangle::~Rectangle() {} int Rectangle::getLength() { return (x1 - x0); } Rectangle operator+(const Rectangle &r1, const Rectangle &r2) { return Rectangle(r1.X0 + r2.X0, r1.Y0 + r2.Y0, r1.X1 + r2.X1, r1.Y1 + r2.Y1) }

This goes with the Cython C++ class definition:

cdef extern from "Rectangle.h" namespace "shapes": cdef cppclass Rectangle: Rectangle(int, int, int, int) except + int x0, y0, x1, y1 int getLength() Rectangle operator+(Rectangle) nogil

The only way we've figured out of doing this is by the following Cython code:

cdef class PyRectangle: cdef Rectangle *thisptr # hold a C++ instance which we're wrapping def __cinit__(self, int x0=0, int y0=0, int x1=0, int y1=0): if x0 == 0: self.thisptr = NULL else: self.thisptr = new Rectangle(x0, y0, x1, y1) def __dealloc__(self): del self.thisptr def getLength(self): return self.thisptr.getLength() def __add__(self, other): cdef Rectangle rect = deref(self.thisptr) + deref(other.thisptr) cdef Rectangle* ptr_rect = new Rectangle(rect.x0, rect.y0, rect.x1, rect.y1) ret = PyRectangle() ret.thisptr = ptr_rect return ret

This is not very optimal, since we have an extra copy in __add__, and the code is not very simple/short either. This is for wrapping an external library, so we can't simply define any new constructors to Rectangle, and we can't rewrite the addition at the Cython level.

We thought that we could simply write something like:

ret = PyRectangle() deref(ret.thisptr) = deref(self.thisptr) + deref(other.thisptr) return ret

But this gives the error "Cannot assign to or delete this."

Is there a more preferred way of doing this sort of thing in Cython? The solution we found is not viable in our code.

--

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