Marko Grdinic | 8 Feb 14:58 2015
Picon

Trouble getting Cython to run

Hello again. A few weeks ago I tried running Cython on crappy eight year old Athlon and ran into an error where it could not allocate C++ classes correctly, but that was because it had all sorts of broken dlls in the system.

Right now, I am trying to get it to run on my new very good computer and it works even worse than before. At first I tried the 0.21 version that came bundled with Anaconda3-2.1.0 (64-bit) and it would compile, but then freeze on even the simplest program. I did some searching, found this, and noted that I am getting the same failure to create process error. Updated it to 0.21.2 as per instructions and Cython did not give an error when I run it in command prompt, but any program I try to run still crashes.

I then manually updated to 0.22 beta1, but now it works even worse. Whereas before it just froze, now it crashes even when I type 'cython' into the command prompt without any error messages.

I am trying to run it on Windows 8.1 and have an i5-4690k with 8GB of RAM. Any advice?

--

---
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.
Emmanuel Gil Peyrot | 8 Feb 14:47 2015
Picon

Recursive import difference between cpython and cython

Hi,

I have a module “module” importing itself, and executing code:

module.py:
 import module
 print('something')

Under cpython 2.7, 3.4, and latest pypy (2 and 3) it prints “something”
two times, but under cython it prints it only once.

I’m not sure what the error is, but it introduces a difference of
behaviour between cpython and cython, so I guess it is a bug.

Thanks. :)

-- 
Emmanuel Gil Peyrot

--

-- 

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

Alistair Muldal | 7 Feb 18:41 2015
Picon

Is it possible to make C++ style internal typedefs in Cython?

In C++ it's possible to declare type aliases that are members of a class or struct. For example:

struct FooDataset
{
   
// internal type alias
   
typedef int DataType;
};

template <class Dataset>
class Bar{

   
Bar(Dataset& input_data);

   
// locally define return type T according to "DataType" member of Dataset
   
typedef typename Dataset::DataType T;

    T getDataItem
(/* etc */);
};

// maybe also use Bar<FooDataset>::DataType somewhere else

Is it possible to do the same thing in Cython? I tried what seemed like the most obvious approach:

# distutils: language=c++


cdef
extern from "example.hpp":
   
struct FooDataset:
        ctypedef
DataType

    cdef cppclass
Bar[Dataset]:

       
Bar(Dataset& input_data)
       
Dataset.DataType T
        T getDataItem
()


This doesn't work:

Error compiling Cython file:
------------------------------------------------------------
...
# distutils: language=c++


cdef
struct Foo:
    ctypedef
int DataType
   
^
------------------------------------------------------------


internal_typedefs_example
.pyx:4:4: Expected an identifier, found 'ctypedef'

Is this a current limitation of Cython (I'm using v0.21.2)?

--

---
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.
Jim Rutt | 3 Feb 18:21 2015
Picon

Job: C++ Python Cython Contractor Needed on OSS Project

I'm looking to hire a highly capable contractor to work on the Opencog Open Source AI framework project ( http://opencog.org).

This is a "market rates" opportunity. I'm not looking for a volunteer.

Best current estimate for the length of the engagement is 3 to 4 months full time, with some tapering hours thereafter for awhile.  A part time arrangement would be considered, but would require at least a 10 hour per week level of commitment.

The issue that needs some serious work is the Opencog Python API, which is implemented as a Cython wrapper around the C++ API.  It's more than a bit creaky. In places it suffers from slow throughput, some parts don't work at all, while other parts suffer from annoying bugs.  It also is far from complete as compared to the C++ and Scheme APIs.  

While  the "old timey" opencogers use the better supported C++ and Scheme APIs, a lot of people that are learning about the project now, would like to use the Python APIs, but have to proceed with caution due to performance issues, the only semi-supported state, the incompleteness, and the set of known bugs.  The known bugs are documented on the github project issues:  https://github.com/opencog/opencog/issues  There is a "python" label.  

The key skill set is somebody who has real experience interfacing complex C++ libraries to Python via Cython. I'm NOT looking for a person who has only used Cython to statically declare variables in Python to speed things up.
A top candidate would also have enough Python experience to be capable of "pythonic design", because some of the current APIs are lacking in such, and could be improved, and we may well want to add some new functionality to the API and would want to make those additions as pythonic as possible.

There are C++ API experts on the Opencog team, and a small number of Python API users who can and should be used as resources for the project.

Because most of the C++ functionality to be surfaced is accessing the Opencog custom hypergraph database, any exposure to graph databases of any sort (such as Neo4J orNetworkX) would be helpful but not essential. Any kind of database API experience would be relevant.  Knowledge of AI or Natural Language Processing would  be a small  plus, but my no means essential.

Location is irrelevant, the Opencog team is spread around the world.

If interested please send a resume or inquiry note to: jimrutt at memetic dot us 

--

---
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.
Pat Nystrom | 3 Feb 03:02 2015
Picon

Extension class attributes, compiler check

Hello,

In an extension class (cdef class MyClass..), all class attributes must be defined at compile time. I noticed today that the Cython compiler (0.21.2) is OK with me assigning self.xyz even if no xyz was defined in the class definition. Does this seem right? It seems to me the compiler should complain about this and not make me wait until runtime to find a simple typographical error. Am I missing a directive or something? This seems so basic I'm feeling like I must be missing something obvious.

Best Regards,
Pat

--

---
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.
RWolst | 2 Feb 18:08 2015

Cython with Cygwin 'undefined reference to ...'

I have the following code which compiles fine in Ubuntu but fails under Cygwin with the error:
    undefined reference to `__imp_c_multiply'

Note that to compile I ran:
    $ python setup.py build_ext -i


Is there an easy fix or is it a bug?

-------------
| CODE |
-------------

There are 3 files, 'c_multiply.c', 'multiply.pyx'  and 'setup.py'

-----------------------------------------------------------------------------------------------------------------------
                                       c_multiply.c
-----------------------------------------------------------------------------------------------------------------------
#include <stdlib.h>

void **convert2Pointer2Pointer(float **P2P, float *P, int rows, int columns) {
  int i, j;
  for (i=0; i<rows; i++) {
    for (j=0; j<columns; j++) {
      P2P[i][j] = P[i*columns + j];
    }
  }
}

void **convert2Pointer(float **P2P, float *P, int rows, int columns) {
  int i, j;
  for (i=0; i<rows; i++) {
    for (j=0; j<columns; j++) {
      P[i*columns + j] = P2P[i][j];
    }
  }
}

void c_multiply(float *matrix, float multiplier, int rows, int columns) {
  // Multiply all values of matrix by multiplier
  int i,j;

  float **matrixP2P = (float **)malloc(rows*sizeof(float *));
  for (i=0; i<rows; i++) matrixP2P[i] = (float *)malloc(columns*sizeof(float));

  convert2Pointer2Pointer(matrixP2P, matrix, rows, columns);

  for (i=0; i<rows; i++) {
    for (j=0; j<columns; j++) {
      matrixP2P[i][j] = matrixP2P[i][j] * multiplier;
    }
  }

  convert2Pointer(matrixP2P, matrix, rows, columns);
  for (i=0; i<rows; i++) free(matrixP2P[i]); free(matrixP2P);
}
-----------------------------------------------------------------------------------------------------------------------





-----------------------------------------------------------------------------------------------------------------------
                                       multiply.pyx
-----------------------------------------------------------------------------------------------------------------------
import cython
import numpy as np
cimport numpy as np

cdef extern void c_multiply(float *matrix, float multiplier, int rows, int columns)

<at> cython.boundscheck(False)
<at> cython.wraparound(False)
def multiply(np.ndarray[float, ndim=2, mode="c"] input not None, float multiplier):
    cdef int rows, columns
    rows, columns = input.shape[0], input.shape[1]

    c_multiply(&input[0,0], multiplier, rows, columns)

    return None

-----------------------------------------------------------------------------------------------------------------------





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

import numpy

setup(
    cmdclass = {'build_ext': build_ext},
    ext_modules = [Extension("multiply",
                             sources=["multiply.pyx", "c_multiply.c"],
                             include_dirs=[numpy.get_include()])],
    )

-----------------------------------------------------------------------------------------------------------------------

--

---
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.
Ben Ruijl | 2 Feb 15:03 2015
Picon

Local variable 'v' referenced before assignment (bug?)

Hi,

In the following minimal working example cython fails to compile:

a = {v: min(y for y in range(v)) for v in range(10)}

with the error: local variable 'v' referenced before assignment

The following variations do compile:

a = {v: min([y for y in range(v)]) for v in range(10)} # no generator anymore!
a = [min(y for y in range(v)) for v in range(10)]
a = {v: [y for y in range(v)] for v in range(10)}

Is this a bug?

Regards,

Ben

--

---
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.
FiReaNG3L | 2 Feb 00:45 2015
Picon

Access memoryview of classes without gil?

Consider the following code (heavily simplified):

           #some toy class, the actual one contains more variables of various types
cdef class item:
    cdef readonly str chr
 
    def __init__(self, str chr):
        self.chr = chr

#the memoryview item_list has been initiated previously, the actual array contain items
def some_function(item[::1] item_list):
    cdef item b
    b = item_list[0]
 
That last line annotation (--annotate):

__pyx_t_2 = 0;
  __pyx_t_1 = (PyObject *) *((struct __pyx_obj_46_cython_magic_5c74f0d8a6567eda879c1440270bb658_item * *) ( /* dim=0 */ ((char *) (((struct __pyx_obj_46_cython_magic_5c74f0d8a6567eda879c1440270bb658_item * *) __pyx_v_item_list.data) + __pyx_t_2)) ));
  __Pyx_INCREF((PyObject*)__pyx_t_1);
  __pyx_v_b = ((struct __pyx_obj_46_cython_magic_5c74f0d8a6567eda879c1440270bb658_item *)__pyx_t_1);
  __pyx_t_1 = 0;

Is it possible to get rid of the Pyx_INCREF somehow? I need to access the memoryview within a prange (nogil) loop and thats the last thing I need to fix.

--

---
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.
Jim Rutt | 30 Jan 13:46 2015
Picon

Is it kosher to post a contractor job opening that is cython centric in this Group?

Is it kosher to post a contractor job opening that is cython centric in this Group?

Groups differ on the their culture and/or rules....

If not here, could someone suggest the right online venue for finding top flight cython<->C++ people?


--

---
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.
Richard Hansen | 30 Jan 08:17 2015
Picon

typed memoryviews: support for read-only buffer objects?

Hi all,

How can I write a Cython function that takes a byte string object (a
normal byte string, a bytearray, or another object that follows the
buffer protocol [1]) as a typed memoryview [2]?  I'm using Python 2.7.

According to the Unicode and Passing Strings Cython tutorial page [3],
the following should work:

    cpdef object printbuf(unsigned char[:] buf):
        chars = [chr(x) for x in buf]
        print repr(''.join(chars))

Unfortunately it doesn't work for normal byte strings and other
read-only buffer objects:

    $ python -c 'import test; test.printbuf("test\0ing")'
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "test.pyx", line 1, in test.printbuf (test.c:1417)
      File "stringsource", line 614, in
View.MemoryView.memoryview_cwrapper (test.c:6795)
      File "stringsource", line 321, in
View.MemoryView.memoryview.__cinit__ (test.c:3341)
    BufferError: Object is not writable.

But it does work for bytearrays and other writable buffers:

    $ python -c 'import test; test.printbuf(bytearray("test\0ing"))'
    'test\x00ing'

Looking at the generated C code, Cython is always passing the
`PyBUF_WRITABLE` flag to `PyObject_GetBuffer()`, which explains the
exception for read-only buffer objects.

I can access read-only buffers via PyObject_GetBuffer():

    from cpython.buffer cimport \
        PyBUF_SIMPLE, PyBUF_WRITABLE, \
        PyObject_CheckBuffer, PyObject_GetBuffer, PyBuffer_Release

    cpdef object printbuf(object buf):
        if not PyObject_CheckBuffer(buf):
            raise TypeError("argument must follow the buffer protocol")
        cdef Py_buffer view
        PyObject_GetBuffer(buf, &view, PyBUF_SIMPLE)
        try:
            chars = [chr((<unsigned char *>view.buf)[i])
                     for i in range(view.len)]
            print repr(''.join(chars))
        finally:
            PyBuffer_Release(&view)

It's not as convenient, but it at least behaves as desired:

    $ python -c 'import test; test.printbuf(bytearray("test\0ing"))'
    'test\x00ing'
    $ python -c 'import test; test.printbuf("test\0ing")'
    'test\x00ing'

Am I doing something wrong, or does Cython not support coercing
read-only buffer objects (such as normal strings) into typed memoryview
objects?

[1] https://docs.python.org/2/c-api/buffer.html
[2] http://docs.cython.org/src/userguide/memoryviews.html
[3]
http://docs.cython.org/src/tutorial/strings.html#accepting-strings-from-python-code

Thanks,
Richard

--

-- 

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

Toni Barth | 29 Jan 17:23 2015

Accessing python functions in callbacks with return values

Hello Cython users,

i'm currently facing a quite interesting problem.
I already wrote a callback function (declared as cdef) which gets called 
by a binary c file I linked to. This callback, without a return value 
(means void) can access python functions, means it can call a user 
defined python function, so it can be used as callback bridge to call a 
python function as real callback, just like the cython example.
But when this C callback function gets a return value, things get much 
stranger. Since the cython source doesn't contain a demo with return 
value, I wasn't able to check it out.
Each time the C binary wants to execute my c function which now has a 
return value (which is compatible to the binary's definitions), the code 
causes a segfault at the point where a pythonic function is called.
I also tried commenting this step out and try to find a solution, but 
then the code crashes at the next pythonic function call, so it seems to 
have problems calling into the python namespace when called by the 
binary callback thread.
I also read that a c library which wants to call cython code needs to 
execute Py_Initialize() first, but since it's a binary i'm unable to do so.
It's also curious that a cython callback without return value is able to 
call into the python namespace, but a cython callback with return value 
isn't.
Since my c callback with return value needs to call a python function 
(since it's a callback bridge again) there is no way to get around 
calling a pythonic function.

Did anyone of you face that problem before and maybe found a, if 
possible, cython-python-only solution?

Best Regards.

Toni Barth

--

-- 

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