Colin Pattison | 13 Jun 23:32 2016
Picon

cimport results in struct declaration being copied into another location

Hey Guys - 


I’ve just started using Cython after having handwritten extensions for a while. I’ve run into an issue and would appreciate any insight you guys have - I have a common pxd which wraps our standard data representation. I then have module B built separately that cimports the data representation. This then results in a redefinition error as the struct DataPy appears in the header for the common module and then is redefined in the cpp of module B. Is there any way around this?


A replication of this is attached - build the common module first using the setup.sh, then build the session module using the setup.sh. DataPy will be declared in common.h and then redeclared in session.cpp.


Is there any way to avoid this happening?


Cheers

C

--

---
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 (redef.tgz): application/x-gtar, 1130 bytes
Alexander Botev | 13 Jun 20:57 2016
Picon

C++ operator returning by value

Hi, so I'm trying to wrap some C++ code in cython. However, I'm struggling to reuse an operator overload, which returns by value.

The cython class looks like this:

cdef extern from "symbolic.h" namespace "metadiff::symbolic":
    cdef cppclass
SymbolicMonomial:
        vector
[pair[int, int]] powers
       
long long coefficient;
       
SymbolicMonomial()
       
SymbolicMonomial(long)
       
SymbolicMonomial(const SymbolicMonomial&)
       
bool is_constant()
       
long long int eval(vector[int]&)
       
long long int eval()
       
string to_string()
       
string to_string_with_star() const

The accompanying wrapper class:
cdef class SymMonomial:
    cdef
SymbolicMonomial* thisptr      # hold a C++ instance which we're wrapping
   
def __cinit__(self):
       
self.thisptr = new SymbolicMonomial()
   
# def __cinit__(self, SymbolicMonomial value):
   
#     self.thisptr = new SymbolicMonomial(value)
   
def is_constant(self):
       
return self.thisptr.is_constant()
   
def eval(self):
       
return self.thisptr.eval()
   
def __str__(self):
       
return self.to_string_with_star()
   
def to_string(self):
       
return self.thisptr.to_string().decode('UTF-8')
   
def to_string_with_star(self):
       
return self.thisptr.to_string_with_star().decode('UTF-8')

Now I have in the C++ code a definition of operator overloading in the following manner (which is outside the class definition):
SymbolicMonomial operator*(const SymbolicMonomial &lhs, const SymbolicMonomial &rhs){...};

My first try of adding it was to add the following, in the cython definition outside the class:
cdef extern SymbolicMonomial operator*(const SymbolicMonomial&, const SymbolicMonomial&)

Then in the wrapper class:
def __mul__(SymMonomial self, SymMonomial other):
        cdef
SymbolicMonomial mon3 = operator*(deref(self.thisptr), deref(other.thisptr))
        a
= SymMonomial()
        a
.thisptr = new SymbolicMonomial(mon3)
       
return a

However this gives me the following error (with a caret under operator*):
symbolic_wrap.pyx:44:45: Cannot convert Python object to 'SymbolicMonomial'

So my second try was to rename the function
cdef extern SymbolicMonomial mul_mm"operator*"(const SymbolicMonomial&, const SymbolicMonomial&)
and now change in the wrapper to mon3 = mul_mm(...)

However this gives the following error:
/home/alex/work/c/swig_test/symbolic.h:298: multiple definition of `metadiff::symbolic::operator*(metadiff::symbolic::SymbolicMonomial const&, metadiff::symbolic::SymbolicMonomial const&)'
So somehow the renaming is screwing up things.

My main question is what am I doing wrong, and how to fix it to make it work? 
 

--

---
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 Botev | 13 Jun 20:49 2016
Picon

C++ Problem with operator overloading returning by value

So I have the following Cython port for C++

cdef extern from "symbolic.h" namespace "metadiff::symbolic":
    cdef cppclass
SymbolicMonomial:
        vector
[pair[int, int]] powers
       
long long coefficient;
       
SymbolicMonomial()
       
SymbolicMonomial(long)
       
SymbolicMonomial(const SymbolicMonomial&)
       
bool is_constant()
       
long long int eval(vector[int]&)
       
long long int eval()
       
string to_string()
       
string to_string_with_star() const

The python wrapper looks like this:
cdef class SymMonomial:
    cdef
SymbolicMonomial* thisptr      # hold a C++ instance which we're wrapping
   
def __cinit__(self):
       
self.thisptr = new SymbolicMonomial()
   
def is_constant(self):
       
return self.thisptr.is_constant()
   
def eval(self):
       
return self.thisptr.eval()
   
def __str__(self):
       
return self.to_string_with_star()
   
def to_string(self):
       
return self.thisptr.to_string().decode('UTF-8')
   
def to_string_with_star(self):
       
return self.thisptr.to_string_with_star().decode('UTF-8')


And it works ok. However, I'm overloading an operator outside the class in c++ like so:
namespace metadiff{
   
namespace symbolic{
     
SymbolicMonomial operator*(const SymbolicMonomial &lhs, const SymbolicMonomial &rhs){...}
 
}
}

The problem is how do I port this with Cython? 

My first attempt, to the above file under the same namespace add:
cdef extern SymbolicMonomial operator*(const SymbolicMonomial&, const SymbolicMonomial&)



However, when I try this inside the wrapper class:
 def __mul__(SymMonomial self, SymMonomial other):
        cdef
SymbolicMonomial mon3 = operator*(deref(self.thisptr), deref(other.thisptr))
        a
= SymMonomial()
        a
.thisptr = new SymbolicMonomial(mon3)
       
return a



I get the error, with a mark under operator*:
symbolic_wrap.pyx:44:45: Cannot convert Python object to 'SymbolicMonomial'

Second try I do this:
 cdef extern SymbolicMonomial mul_mm"operator*"(const SymbolicMonomial&, const SymbolicMonomial&)

However, now I get the following error:
 multiple definition of `metadiff::symbolic::operator*(metadiff::symbolic::SymbolicMonomial const&, metadiff::symbolic::SymbolicMonomial const&)'

The question is what am I doing wrong, and more importantly how can I achieve the goal of reusing the C++ implementation?



 

--

---
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.
Tom Swirly | 12 Jun 23:46 2016
Gravatar

setup.py question: Why does the Cythonize step always go off before anything starts?

Hello!  Too much of my time has gone into this tiny question...

Python 3.4, cross-platform compilation (currently on OS/X).

I have added other targets to my setuptools setup.py like "generate" that creates generated files.  So far so good, but the Cythonize step always goes off before the generated file step, when of course I want it to come after.

This is particularly annoying when there's a mistake in my scripts which generates a .pyx file that can't be compiled, because then it always tries and fails to cythonize the .pyx before it even tries to get to the code generation - so I can't even revert the change and fix.




--
     /t

http://radio.swirly.com - art music radio 24/7 366/1000

--

---
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.
Björn Häuser | 12 Jun 19:54 2016
Picon
Gravatar

python2 vs python3:

Hi there!

I currently try to compile libimobiledevice cython bindings with python 3.5. It compiles out of the box, but on runtime I get this error:

>>> import imobiledevice
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ImportError: dlopen(/usr/local/lib/python3.5/site-packages/imobiledevice.so, 2): Symbol not found: _PyString_AsString
 Referenced from: /usr/local/lib/python3.5/site-packages/imobiledevice.so
 Expected in: flat namespace
in /usr/local/lib/python3.5/site-packages/imobiledevice.so


This works fine for python2.7.

The relevant code snippet is here: https://github.com/libimobiledevice/libimobiledevice/blob/master/cython/debugserver.pxi#L46-L54

Is there a way to make this code work on python2 and python3 easily? And what is the suggested fix for python3?

Thank you
Björn

--

---
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.
Joy merwin monteiro | 12 Jun 11:00 2016
Picon
Gravatar

compilation issue within virtualenv

Hello,

I have been developing a Cython wrapper to some fortran code, available here:
https://github.com/JoyMonteiro/pyGFS/blob/1ed7bf54d6478d0581c93899a57f80cd4925de24/gfs/_gfs_dynamics.pyx

I have been developing it in my normal Ubuntu environment, without much issues. (I used Cython 0.23.4)

For deployment, I thought of using virtualenv, and when I clone my repository and try to build, compilation
fails with the following error:

Error compiling Cython file:
------------------------------------------------------------
...
    PyTypeObject *Py_TYPE(obj)
    bint PyMapping_Check(obj)
    object PyErr_Format(exc, const char *format, ...)

<at> cname("__pyx_convert__from_py_npy_cdouble")
cdef npy_cdouble __pyx_convert__from_py_npy_cdouble(obj) except *:
    ^
------------------------------------------------------------

FromPyStructUtility:11:5: 'npy_cdouble' is not a type identifier

Error compiling Cython file:
------------------------------------------------------------
...
    bint PyMapping_Check(obj)
    object PyErr_Format(exc, const char *format, ...)

<at> cname("__pyx_convert__from_py_npy_cdouble")
cdef npy_cdouble __pyx_convert__from_py_npy_cdouble(obj) except *:
    cdef npy_cdouble result
        ^
------------------------------------------------------------

FromPyStructUtility:12:9: 'npy_cdouble' is not a type identifier
Traceback (most recent call last):
  File "setup.py", line 8, in <module>
    libraries=["gfsDycore","shtns_omp","lapack","fftw3_omp","fftw3","rt","m"] )
  File "/home/joymm/climt/MY_CLIMT/local/lib/python2.7/site-packages/Cython/Build/Dependencies.py", line 912, in cythonize
    cythonize_one(*args)
  File "/home/joymm/climt/MY_CLIMT/local/lib/python2.7/site-packages/Cython/Build/Dependencies.py", line 1034, in cythonize_one
    raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: _gfs_dynamics.pyx
make: *** [cython_wrapper] Error 1


As you can see in the source file, I have cimported numpy, so this error is puzzling.
Evidently, there is something installed on my system that is missing in the clean install in virtualenv, but I'm unable
to figure out what that is. I have pip installed cython and numpy in the virtualenv.

Any ideas?

TIA,
Joy

--

---
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.
kai jay | 12 Jun 01:02 2016
Picon

C++ Class convert to cython have error occur

The C++ code as below
using namespace std; template <class T> void mynamespace::LinkedList<T>::append (T value) { if (head == NULL) { head = new ListNode (value); tail = head; } else { tail->next = new ListNode (value); tail = tail->next; } length++; } template <class T> void mynamespace::LinkedList<T>::printList (void) { if (head != NULL) { ListNode * it = head; while (it != NULL) { cout << it->value << endl; it = it->next; } } } template <class T> int mynamespace::LinkedList<T>::getLength (void) { return length; } template <class T> int mynamespace::LinkedList<T>::tes () { return 10; } template <class T> void mynamespace::LinkedList<T>::remove (int idx) { if (head != NULL) { // invalid element to remove... if (idx < 0 || idx > length) return; if (idx == 0) { ListNode * tmp = head; head = tmp->next; delete tmp; length--; } else { ListNode * it = head; ListNode * parent = it; int i; bool found = false; for (i = 0; i < length; ++i) { if (i == idx) { found = true; break; } parent = it; it = parent->next; } if (found) { parent->next = it->next; delete it; length--; } } } }
The cython code as below
# distutils: language = c++ # distutils: sources = cppcode.cpp from libcpp.string cimport string cdef extern from "cppcode.h" namespace "mynamespace": cppclass LinkedList[T]: LinkedList () void append (T) int getLength () void remove (int) void printList () int tes () cdef class linklist: cdef LinkedList[string] * listt def __cinit__(self): self.listt = new LinkedList[string]() if self.listt == NULL: raise MemoryError() def __dealloc__(self): if self.listt != NULL: del self.listt cpdef int tests(self): return self.listt.tes() cpdef void appendlist(self,value): return self.listt.append(value)


I have try to create a class by cython , but when I compile it to .pyd file have an error occur. But when I delete the function (test) and (appendlist) it can pass the compile.

The error message as below:


cpycode.obj : warning LNK4197: export 'PyInit_cpycode' specified multiple times; using first specification Creating library build\temp.win-amd64-3.5\Release\cpycode.cp35-win_amd64.lib and object build\temp.win-amd64-3.5\Release\cpycode.cp35-win_amd64.exp cpycode.obj : error LNK2001: unresolved external symbol "public: int __cdecl mynamespace::LinkedList<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::tes(void)" (?tes <at> ?$LinkedList <at> V?$basic_string <at> DU?$char_traits <at> D <at> std <at> <at> V?$allocator <at> D <at> 2 <at> <at> std <at> <at> <at> mynamespace <at> <at> QEAAHXZ) C:\pptemplates\cpycode.cp35-win_amd64.pyd : fatal error LNK1120: 1 unresolved externals error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\amd64\\link.exe' failed with exit status 1120

--

---
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.
TrekkieByDay | 11 Jun 04:14 2016
Picon

How can I access the C++ complex math functions in Cython?

Hello everyone. I'm trying to use the math functions from "complex.h" in Cython. In my .pxd, I'm trying to use the definition:

    cdef extern from "complex.h":
        double complex I;
        double complex cexp(double complex z)

Alas, although that compiles, it fails if I try to actually call cexp. The message is just "gcc.exe failed with exit status 1."

Any ideas to point me in the right direction? Here is a stackoverflow question I posted on the same that has the code.

http://stackoverflow.com/questions/37757067/using-c-complex-functions-in-cython

--

---
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.
Marko Loparic | 7 Jun 23:19 2016
Picon

Announcing pyorcy 0.1

Hello Cython users,

This is the first release of pyorcy. Your input is more than welcome!

https://github.com/markolopa/pyorcy

Pyorcy has 2 purposes:

- Allow the mix of python and cython code in a single file. This can
also be done with cython's "pure python" mode, but with import
limitations. pyorcy gives you the full cython super-powers.
- Launch an automatic compilation, triggered by a function decorator.
This mechanism is similar to what numba offers.

So basically, you can develop and debug using the python mode. When
you are happy with your function, then you just annotate the variables
and add the decorator for automatic Cython code generation and
compilation. Simple.

Many thanks to the co-author Francesc Alted for turning a small script
into a full-featured package!

Related conversations:
https://groups.google.com/forum/#!searchin/cython-users/pyorcy/cython-users/HOhEAKURA90/nU7Vu9yQBgAJ
https://groups.google.com/forum/#!searchin/cython-users/pyorcy/cython-users/SX9-YCOPKSw/d8KTqo9JBgAJ

Enjoy!
Marko

--

-- 

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

Tom Swirly | 7 Jun 21:57 2016
Picon

Re: Re: cython.org down for several days - or just me?

Ouch.  Good luck!   

And thanks for this amazing project, which has been extremely effective for me, at least...

--

---
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 Froger | 6 Jun 23:07 2016

issue with PyErr_CheckSignals

Hello,

With cython 0.24 on GNU/Linux, I can't stop this script with CTRL-C when it
enters the "sleep forever" loop:

    import time
    from cpython.exc cimport PyErr_CheckSignals

    def main():
        while True:
            foo()
            PyErr_CheckSignals()

    cdef void foo():
        print("sleep forever")
        while True:
            time.sleep(1)

Why? Is it a bug?

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


Gmane