gramoore | 24 Apr 21:10 2014
Picon

Help with exec and contexts in cython 0.20.1


Hi,

I am using cython 0.20.1 on ubuntu 14.04/

I have the following python that runs as expected on python 2.7.6

def doit(expr):
        exec(expr)
        return locals()['result']

z = doit("""
result = {}
""")

print dir(z)

Z appears as a dictionary. All good.

In cython I have the following .pyx file:


def doit(expression):
        exec(expression)
        return locals()['result']

This happily compiles to an .so object that can be imported into python.

I then have the following python runner in test.py:

import my_lib

g = my_lib.doit("""
result = {}
""")

print dir(g)

Which gives a keyError

Traceback (most recent call last):
  File "test.py", line 6, in <module>
    """)
  File "helloworld.pyx", line 4, in my_lib.doit (helloworld.c:757)
    return locals()['result']
KeyError: 'result'

I have also tried in the .pyx with

def doit(expression):
        lctx = {}
        exec expression in lctx
        return lctx['result']

But with the same result.

My goal with this pattern is to allow the exec to do a bunch of work and then pass out, return a result of that work.

Any pointers on this greatly appreciated. Please ask if I can clarify anything further.

Kind regards,

Graham



















--

---
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 | 24 Apr 20:39 2014
Picon

Bug with std::string destructor on OSX

I have a project that builds fine for windows+MSVC, windows+mingw, linux+gcc, and fails on OSX.  Cython 0.20.1 on all platforms.  The offending error is 

build/temp.macosx-10.5-x86_64-2.7/pyrex/CoolProp/CoolProp.cpp:25556:26: error: expected the class name after '~' to name a destructor


  p->Fluid.std::string::~string();


                         ^


build/temp.macosx-10.5-x86_64-2.7/pyrex/CoolProp/CoolProp.cpp:25557:26: error: expected the class name after '~' to name a destructor


  p->phase.std::string::~string();


which is caused by the offending destructor code:


static void __pyx_tp_dealloc_8CoolProp_8CoolProp_State(PyObject *o) {

  struct __pyx_obj_8CoolProp_8CoolProp_State *p = (struct __pyx_obj_8CoolProp_8CoolProp_State *)o;

  #if PY_VERSION_HEX >= 0x030400a1

  if (unlikely(Py_TYPE(o)->tp_finalize) && (!PyType_IS_GC(Py_TYPE(o)) || !_PyGC_FINALIZED(o))) {

    if (PyObject_CallFinalizerFromDealloc(o)) return;

  }

  #endif

  p->CPS.CoolPropStateClassSI::~CoolPropStateClassSI();

  p->Fluid.std::string::~string();

  p->phase.std::string::~string();

  (*Py_TYPE(o)->tp_free)(o);

}


This works fine on the other platforms

--

---
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.
Giovanni Luca Ciampaglia | 24 Apr 19:10 2014
Picon

Typed memoryview of struct dtype with char fields

Hi all,

with regards to this previous thread:

http://thread.gmane.org/gmane.comp.python.cython.user/10646

I am trying to do more or less the same as the OP but my struct dtype is composed only of strings:

```python
Timestamp = np.dtype([
    ('year', 'S4'),
    ('month', 'S2'),
    ('day', 'S2'),
    ('hours', 'S2'),
    ('minutes', 'S2'),
    ('seconds', 'S2')])
```

If I map this struct to the following dtype 

```python
cdef packed struct Timestamp_t:
     char[4] year
     char[2] month
     char[2] day
     char[2] hours
     char[2] minutes
     char[2] seconds
```

and pass it to a function that accepts a typed memoryview:

```python
cpdef Timestamp_t[:] parsetimestamp(Timestamp_t[:] a):
    cdef:
        Timestamp_t t
        int N = len(a), i
    for i in xrange(N):
        t = a[i]
        print 'year = {}'.format(t.year)
        print 'month = {}'.format(t.month)
        print 'day = {}'.format(t.day)
        print 'hours = {}'.format(t.hours)
        print 'minutes = {}'.format(t.minutes)
        print 'seconds = {}'.format(t.seconds)
        print '------------------------------'
    return a
```

and then run this test:

```python
import numpy as np
from dates import parsetimestamp, Timestamp

a = np.asarray(['19800712112359',
                '20120712112359'], dtype=Timestamp)

if __name__ == '__main__':
    print a
    b = np.asarray(parsetimestamp(a))
    print b
```

Then it looks like that Cython gets the offsets right but does not compute correctly the length of each field:

```
[('1980', '07', '12', '11', '23', '59')
 ('2012', '07', '12', '11', '23', '59')]
year = 19800712112359
month = 0712112359
day = 12112359
hours = 112359
minutes = 2359
seconds = 59
------------------------------
year = 20120712112359
month = 0712112359
day = 12112359
hours = 112359
minutes = 2359
seconds = 59
------------------------------
[('1980', '07', '12', '11', '23', '59')
 ('2012', '07', '12', '11', '23', '59')]
```

I am running Cython 0.21dev from github, so in principle the bug that the OP was lamenting should have been taken care of. 
Of course I can cast everything to uint8 and then do some trickery to get the strings back, but it would have been nice
to use just the attribute access syntax provided by Cython.  

(What I really want do actually is to convert those fields to int and compute the number of seconds since the unix epoch, and
possibly stick everything into a prange block, but that's not important here).

Any idea?

Cheers,

Giovanni

--

---
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.
Ephrones Sharp | 23 Apr 23:01 2014
Picon

Implicit declaration of function (looks perfectly fine to me)

I’m trying to create a wrapper for libotr and implementing the first few functions I thought it finally worked, but now I get this:

```
otr/otr.c:695:3: warning: implicit declaration of function ‘otrl_user_state_create’ [-Wimplicit-function-declaration]
   __pyx_v_self->_c_user_state = otrl_user_state_create();
 
and

otr/otr.c:749:5: warning: implicit declaration of function ‘otrl_user_state_free’ [-Wimplicit-function-declaration]
     otrl_user_state_free(__pyx_v_self->_c_user_state);
```

This is all code atm:

```libotr.pxd
cdef extern from "libotr/proto.h":
    ctypedef int gcry_error_t

    gcry_error_t otrl_init(unsigned int ver_major,
                           unsigned int ver_minor,
                           unsigned int ver_sub)


cdef extern from "libotr/userstate.h":
    ctypedef struct OtrlUserState:
        pass

    OtrlUserState* otrl_user_state_create()
    void otrl_user_state_free(OtrlUserState *user_state)
```

```otr.pyx
cimport otr.libotr as libotr

# def init():
#     cdef libotr.gcry_error_t res
#     res = libotr.otrl_init(4, 0, 0)
#     return res

cdef class UserState:
    cdef libotr.OtrlUserState *_c_user_state
    def __cinit__(self):
        self._c_user_state = libotr.otrl_user_state_create()
    def __dealloc__(self):
        #TODO TIMER?
        if self._c_user_state is not NULL:
            libotr.otrl_user_state_free(self._c_user_state)
```

When I uncomment the init() function (and comment out the rest) it compiles with no errors and I can import the module and execute init (and it returns 0 as it should).
I know I have a trivial error somewhere but can’t find it.

Side-note: Is there an official Cython IRC channel? I’m lurking on #cython on freenode, but not too much activity there.

--

---
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 | 23 Apr 01:18 2014
Picon

Loop optimizations

Hi!

I was rewriting some python code for speed, here is a minimal example of what I am doing:

Generated by Cython 0.20.1 on Wed Apr 23 01:09:55 2014

Raw output: loop_optimizations.c

1:
2: def pythonic():
3: cdef int i, j, d, dummy = 0
4: for i, j in ((1,0), (1,1), (0, 1), (-1, 1)):
5: for d in (-1, 1):
6: dummy += d*i*j # silly example, dummy will be zero
7: return dummy
8:
9:
10: def cythonic():
11: cdef int i, j, d, k, dummy = 0
12: cdef int * directions = [1, 0, 1, 1, 0, 1, -1, 1]
13: for k in range(4):
14: i, j = directions[k*2], directions[k*2+1]
15: for d in range(-1,2,2):
16: dummy += d*i*i*j
17: return dummy
18:
19: # def cythonic_const():
20: # cdef int i, j, d, k, dummy = 0
21: # cdef const int * directions = [1, 0, 1, 1, 0, 1, -1, 1]
22: # for k in range(4):
23: # i, j = directions[k*2], directions[k*2+1]
24: # for d in range(-1,2,2):
25: # dummy += d*i*j
26: # return dummy
27:

I have two short questions:

  • Is my cythonic version the best we can get as for readability when we want to avoid Python overhead? (I run those loops millions of times - so it matters)
  • I tried to indicate to the compiler that directions never change, in C I would have done: "const int * const directions", the second const isn't recognized by cython and the first one causes compilation errors (hence, cythonic_const is commented out here). Is this is a bug in Cython or undefined behaviour?

It would have been nice if Cython would have been able to turn "pythonic" to efficient C-code but I guess it's a quite complex task.

Best 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.
J3 Remy | 22 Apr 21:07 2014
Picon

Segmentation Errors

Hi all, new to the group and I have been working with Cython in the form of using it as a dependency for Dionaea (dionaea.carnivore.it).  The instructions for installing it are as follows:

wget http://cython.org/release/Cython-0.15.tar.gz tar xfz Cython-0.15.tar.gz cd Cython-0.15 /opt/dionaea/bin/python3 setup.py install cd ..
After I initially ran the command I cd'd into the /opt/dionaea/bin/python3 directory and ran the "setup.py install" command, I received the following error:

/opt/dionaea/bin/python3: error while loading shared libraries: libpython3.2m.so.1.0: cannot open shared object file: No such file or directory

I found that libpython3.2m.so.1.0 resided in the /opt/dionaea/lib/ directory, I copied it out and moved it into the /opt/dionaea/bin/python3 directory believing that the command did not know where or if the file existed.  Once I moved the file into /opt/dionaea/bin/python3 directory I received this error:

Segmentation fault (core dumped)

Not sure what needs to be done to get Cython to work properly.  Any help would be greatly appreciated.

Thank you,
J3 Remy



--

---
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.
MSAR | 22 Apr 14:15 2014

error building Cython first example

When trying to build the first plain example (hello) from link: http://docs.cython.org/src/quickstart/build.html using distutils, I get the error gcc: error: unrecognized command line option '-mno-cygwin'. Below the complete description:

C:\tmp>python setup.py build_ext --inplace
running build_ext
building 'hello' extension
C:\MinGW\bin\gcc.exe -mno-cygwin -mdll -O -Wall -IC:\Users\msn\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.1.0.1371.win-x86_64\include -IC:\Users\msn\AppData\Local\Enthought\Canopy\User\PC -c
hello.c -o build\temp.win-amd64-2.7\Release\hello.o
gcc: error: unrecognized command line option '-mno-cygwin'
error: command 'gcc' failed with exit status 1

Probably, it is an issue with my gcc toolchain, but I don't remember how to fix it :-(
Any clues?

Tks, 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.
equaeghe | 22 Apr 11:50 2014
Picon

How to fix compiler warning "useless type name in empty declaration"

Hi,


I use Cython 0.20.1; I get multiple of the following when cythonizing my little project:

warning: useless type name in empty declaration

I would like to get rid of them, or have somebody say, with authority, that I should ignore it and that it is fine to ignore it. ;-)

In the c file, I find (first line is offending one):

glp_smcp;
static glp_smcp __pyx_convert__from_py_glp_smcp(PyObject *);

In the pxd file, I say:

ctypedef struct SimplexCP "glp_smcp":
    int ...

Referring to a header file containing:

typedef struct {
    int ...
} glp_smcp;

The code from the pxd file is used in a pyx file as follows:

cdef class FirstClass:

    cdef glpk.SimplexCP _smcp

    def __cinit__(self):
        glpk.init_smcp(&self._smcp)

    ...


cdef class SecondClass(SomeBaseClass):

    ...

    def some_method(self, controls=FirstClass()):
        cdef glpk.SimplexCP smcp = controls._smcp
        ...

    ...


I guess that this may be as simple as changing my pxd line, but I do not know how. In case it is not quite simple and I have not provided enough information, do not hesitate to ask for more.


Thanks,

Erik

--

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

Gmane