Jeff Donner | 17 Apr 09:11 2014
Picon

AttributeError: 'dict' object has no attribute 'size' when fully qualified

Hi; I can get a C struct to show like I expect when I cimport the symbols into the global namespace, but not when I leave them needing to be fully qualified - can anyone point out what's likely wrong? 

This is with both Cython 19.x, and 20.1.

Thanks.


== Banana.pyx DOESN'T work when symbols have to be fully qualified
cimport cbanana

def add(i):
    cdef cbanana.Banana b
    b.size = i
    cbanana.print_banana(&b)
== test
~/banana> python -c 'import Banana ; Banana.add(5)'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "Banana.pyx", line 5, in Banana.add (Banana.c:708)
    b.size = i
AttributeError: 'dict' object has no attribute 'size'


== Banana.pyx WORKS but makes the symbols global
from cbanana cimport Banana, print_banana

def add(i):
    cdef Banana b
    b.size = i
    print_banana(&b)
== test
~/banana> python -c 'import Banana ; Banana.add(5)'
banana 5!


Remaining files:

== banana.h
typedef struct {
  int size;
} Banana;

void print_banana(Banana* b);

== banana-impl.c
#include "banana.h"
#include <stdio.h>

void print_banana(Banana* b) {
  printf("banana %d!\n", b->size);
}

== cbanana.pxd
cdef extern from "banana.h":
    ctypedef struct Banana:
        int size

    void print_banana(Banana* banana)

== setup.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

# build:
#   rm -rf build banana.c Banana.so && \
#   CFLAGS="-I." LDFLAGS="-Llib" python setup.py build_ext -i
# basic test
#   export PYTHONPATH=.
#   python -c 'import Banana ; Banana.add(5)'

setup(name='BananaExtension',
      version='0.1',
      packages=['Banana'],
      package_dir = {'Banana': 'Banana'},
      cmdclass = {'build_ext': build_ext},
      ext_modules=[Extension('Banana',
                             ['Banana.pyx',
                              'banana-impl.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.
Ondřej Čertík | 16 Apr 01:07 2014
Picon

vector[RCP[const Basic]] doesn't compile

Hi,

The following compiles:

RCP[const Basic]

as well as:

vector[RCP[Basic]]

but not:

vector[RCP[const Basic]]

Cython 0.19.2 gives an error:

Error compiling Cython file:
------------------------------------------------------------
...
        string get_name() nogil
        RCP[const Basic] get_arg() nogil

    cdef cppclass Derivative(Basic):
        RCP[const Basic] get_arg() nogil
        vector[RCP[const Basic]] get_symbols() nogil
                        ^
------------------------------------------------------------

/nh/nest/u/ondrej/repos/csympy/csympy/lib/csympy.pxd:131:25: Expected
']', found 'Basic'

It is possible to fool Cython via the old trick:

https://github.com/cython/cython/wiki/FAQ#how-do-i-use-const

But it fails later. Here is code that actually needs the above:

    def _sympy_(self):
        cdef RCP[const csympy.Derivative] X = \
            csympy.rcp_static_cast_Derivative(self.thisptr)
        arg = c2py(deref(X).get_arg())._sympy_()
        cdef RCP[const csympy.Basic] Y
        s = []
        for i in range(deref(X).get_symbols().size()):
            Y = <RCP[const csympy.Basic]>(deref(X).get_symbols()[i])
            s.append(c2py(Y)._sympy_())
        import sympy
        return sympy.Derivative(arg, *s)

This compiles and works. But it's ugly, because I need to explicitly
do the <RCP[const csympy.Basic]> cast. Here is the code that it
generates for the line Y = .... :

    __pyx_v_Y = ((CSymPy::RCP<CSymPy::Basic const
>)((CSymPy::RCP<CSymPy::Basic const
>)((*__pyx_v_X).get_symbols()[__pyx_v_i])));

As you can see, it is doing exactly the same cast twice.... But it
compiles and works. I think double casting is innocent from the C++
standpoint. Except that it's fragile, as Cython can't check the types.

So let's see if there is a way to fix this.

If I remove the <RCP[const csympy.Basic]> cast, it fails with:

Error compiling Cython file:
------------------------------------------------------------
...
            csympy.rcp_static_cast_Derivative(self.thisptr)
        arg = c2py(deref(X).get_arg())._sympy_()
        cdef RCP[const csympy.Basic] Y
        s = []
        for i in range(deref(X).get_symbols().size()):
            Y = (deref(X).get_symbols()[i])
                                      ^
------------------------------------------------------------

/nh/nest/u/ondrej/repos/csympy/csympy/lib/csympy_wrapper.pyx:319:39:
Cannot assign type 'RCP[Basic] &' to 'RCP[Basic const ]'

if I change the definition of Y to "cdef RCP[csympy.Basic] Y" then it
fails with:

Error compiling Cython file:
------------------------------------------------------------
...
        arg = c2py(deref(X).get_arg())._sympy_()
        cdef RCP[csympy.Basic] Y
        s = []
        for i in range(deref(X).get_symbols().size()):
            Y = (deref(X).get_symbols()[i])
            s.append(c2py(Y)._sympy_())
                          ^
------------------------------------------------------------

/nh/nest/u/ondrej/repos/csympy/csympy/lib/csympy_wrapper.pyx:320:27:
Cannot assign type 'RCP[Basic]' to 'RCP[Basic const ]'

So there is no way to avoid the cast. I tried to use the old way of
doing const stuff, but it's impossible to fool.

I think this is a bug in Cython, isn't it?

Ondrej

P.S. I originally sent this message to Cython-dev
<cython-dev <at> codespeak.net>, but I think this list is now defunct, so I
resent it here.

--

-- 

--- 
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 Melvin | 15 Apr 18:38 2014
Picon

Calling .NET via C++ wrapper

Hi There,

I am trying to call an .NET API from Python by wrapping it with a simple C++ wrapper.  The functionality I need is all in a .NET assembly (QAConnectionManager.dll), and I wrote a C++ "bridge" dll that I intend on wrapping and calling from with Cython.  It all built fine (both my bridge .dll and Cython wrapper), but when I call my bridge dll's function (which calls the .NET API function), I get this error:

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'QAConnectionManager, Version=1.0.
0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
   at QATester.LaunchApp()

The API dll in question (QAConnectionManager.dll) is present in the same directory as my Cython-ized module and test code, and I have tried manually adding the local folder to the PATH, but no luck.  The weird thing is my bridge .dll includes a reference to QAConnectionManager.dll and that builds just fine in Visual Studio.  Am I missing something simple?  Is what I am trying to do even possible?

Thanks,
Mark.

--

---
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 Dahlgren | 13 Apr 11:17 2014
Picon

state.pyx not valid name of extension

Hi!

It took me a little while to figure it out, but naming my cython extension module: state.pyx
creates a name clash with initstate():
/usr/include/stdlib.h:336:14: error: previous declaration ‘char* initstate(unsigned int, char*, size_t)’ here

Maybe Cython could print an error if one tries to use ${name}.pyx where init${name} is already reserved?

Regards
/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.
Stefan Behnel | 12 Apr 18:45 2014
Picon

Support for type declarations in Python signature annotations

Hi,

I implemented initial support for reading type declarations from signature
annotations.

https://github.com/cython/cython/commit/c036607abaec88cf28cb757efc73496e738b6ce7

This is (supposed to be) supported:

    def func(plain_python_type: dict,
             named_python_type: 'dict',
             explicit_python_type: {'type': dict},
             explicit_named_python_type: {'type': 'dict'},
             explicit_c_type: {'ctype': 'int'}):
        ...

Here's a test:

https://github.com/cython/cython/commit/e753d13ced7d0b8c61449cddd78c76461d31d362

The tests are far (far!) from complete and there are definitely bugs and
further room for improvements. If you are interested in this feature,
please give it a try. I would be happy about more tests (especially in form
of code) and user feedback on this.

Stefan

--

-- 

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

Stefan Behnel | 12 Apr 09:52 2014
Picon

Re: changes from 0.19 to 0.20

Robert Bradshaw, 08.04.2014 07:05:
> On Mon, Mar 17, 2014 at 8:43 AM, Eric Frederich
> <eric.frederich <at> gmail.com> wrote:
>> I have been playing and trying to find a way to make it work at all in
>> 0.20.1 but I cannot find a way.
>> I can not call cython on the .pyx file.
>> Here is an example that doesn't include any typedef and the cython command
>> fails on it.
>>
>> cdef extern from "example.h":
>>
>>     cdef struct my_date_t:
>>         int year
>>         int month
>>         int day
>>
>>     cdef void print_date(my_date_t d)
>>     cdef my_date_t give_me_a_date()
>>
>> def prnt_date(d):
>>     print_date(d)
>>
>> def gimmie():
>>     return give_me_a_date()
>
> https://github.com/cython/cython/commit/d914e150afcd96b0ec561decf4b66b3f64d39bae

No, that's not it (not by itself, at least). I tried that last December and
it broke all sorts of conversions (just like it did now). The thing is that
only the create_from/to_py_utility_code() methods themselves know when they
have to do something, so testing "type.to_py_function" isn't enough.
Instead, you should always be able to safely call
"type.create_from/to_py_utility_code()" to test if conversion works. Only
that can make sure the current env knows about the necessary utility code.

Stefan

--

-- 

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

GB | 11 Apr 20:18 2014
Picon

is Cython basic tutorial wrong?

I'm trying to follow this guidelines:
http://docs.cython.org/src/tutorial/cython_tutorial.html#the-basics-of-cython

but they don't work. I get this error:
 
C:\Users\Gabriele\Documents\università\TESI\code\test>python setup.py build_ext
--inplace
Compiling helloworld.pyx because it changed.
Cythonizing helloworld.pyx
C:\Anaconda\lib\site-packages\Cython\Utils.py:14: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
  res = cache.get(args, uncomputed)
C:\Anaconda\lib\site-packages\Cython\Utils.py:16: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
  res = cache[args] = f(*args)
running build_ext
building 'helloworld' extension
C:\Anaconda\Scripts\gcc.bat -DMS_WIN64 -mdll -O -Wall -IC:\Anaconda\include -IC:\Anaconda\PC -c helloworld.c -o build\temp.win-amd64-2.7\Release\helloworld.o
gcc.exe: error: helloworld.c: No such file or directory
gcc.exe: fatal error: no input files
compilation terminated.
error: command 'gcc' failed with exit status 1

What can I do?

thanks

Gabriele

--

---
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.
GB | 11 Apr 19:51 2014
Picon

speed up with C extension

Hi,

I have a program in Python that is processing 3000000 data doing 200 operations on each one of them.

I would like to speed it up using a C routine.

In particular I'm using a scipy.interpolate function called interp1d.

How can I use Cython to write a routine like this?

Thanks

Gabriele

--

---
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.
GB | 11 Apr 19:46 2014
Picon

speed up python routine writing C extension: can Cython be useful?

Hi,

I have a program that analyze 3000000 data and for each of them it makes 200 calculations (an interpolation with interp1d from scipy.interpolate).

I would like to speed up the process because now it takes near 12 hours...

I know that the bottleneck is this interp1d because I have profiled the program for different (small) numebers of data and this is the process that increase in time.

Do you think that using a C extension could speed up the process? How can I do that (it's the first time I try something like this)?

thanks

GB

--

---
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.
sparrow2867 | 11 Apr 14:22 2014
Picon

Any progress on standalone dll's?

There has been a discussion about this on cython.devel on May 12th 2009, but 
apparently there are no examples on the web about how to use Cython to build 
dll's that can be loaded by, say, C++ code without embedding Python.
Stefan Behnel's reply at that time was "I never tried.".
The point I would like to make here that it would be a very appealing option 
if Cython could build dll's like C++ can, so callable without loading Python 
runtime.
My question is: has anyone tried, is there any example available?
I have been protoyping in Python/Cython, building modules to be contained
in a dll. 
Ultimately I had to translate everything to C++ since the application calling 
the dll did not have Python runtime and I could not persuade its owners to add 
Python runtime.

--

---
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.
Christopher Jordan-Squire | 8 Apr 07:06 2014
Picon

recarray and typed memory views

I'm writing cython code to glue together some C code and python. The C code takes in many different struct types, such as

typdef struct{
char[16] name
int id
double value
} Container;

In total there are around 10 different types like that. So in the cython code I have many declarations like

ctypedef struct Container:
char[16] name
int id
double value

But I'd also like to create arrays of Container in python and pass those into the C code via Cython. Ideally without extension types that glom together Python objects to put into an array of strut at the cython level. The obvious thing to try was numpy record arrays. So in addition to the cython boilerplate I have numpy boiler plate for these types, e.g. 

container_t = np.dtype( '|S16,i4,f8', align=True). 

This works. But I'm repeating myself a lot. Not only do I create the cython type, but I also create a numpy dtype containing essentially the same information. Is there any way to get around this while keeping the same functionality? 

Thanks,
Chris

--

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