Nathann Cohen | 20 Apr 10:54 2015
Picon

can cimport'ed functions be inlined?

Hello everybody,

I just wondered if some cdef function with an 'inline' flag can actually be inlined if I cimport it from another module. Or is the first moduled compiled independently and called as a library?

THaaaaaaaaanks,

Nathann

--

---
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.
Hans Müller | 19 Apr 21:52 2015
Picon

How best to wrap C++ classes which uses Eigen?

Hi,

I try to write a wrapper for C++ library which uses Eigen heavily, e.g.

surface.hpp:
#include <Eigen/Core>
#include <Eigen/Geometry>
#include <Eigen/StdVector>

class Surface
{
public:
    std
::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> > nodes;
   
virtual void transform(const Eigen::Matrix3d &transformation);
}

What would be the best way to wrap this code? I managed to write the wrapper for the rest of the class, but this eludes me. I googled "cython eigen" but did not find any answer.

Can anyone of you experts point me in the right direction? Thanks.

Hans

--

---
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 | 18 Apr 08:12 2015
Picon

PEP 492 - Coroutines with async and await syntax

Hi!

Looks like there's a new PEP coming up which receives rather strong support:

https://www.python.org/dev/peps/pep-0492/

It proposes new sytax constructs like

    async with EXPR as VAR:
        BLOCK

    async for TARGET in ITER:
        BLOCK

that substantially simplify asynchronous code. Here's the current
discussion thread on python-ideas:

http://comments.gmane.org/gmane.comp.python.ideas/33009

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.

Johannes Kulick | 17 Apr 10:44 2015
Picon

typing python functions to use them as callback

Hi all,

I'm quite new to cython, so apologies for stupid questions...

Here is my problem:

We have a C++ library (which optimizes robot trajectories if that matters), which gets std::function objects as callback. Let's say, they look like this:

class Optimizer {
   
public:
       
void optimize(std::function<double(double)> cost_fnc);
};

Giving it a cython function is easy (as function_pointers get casted to std::function and I can specify the desired signature in cython etc.).

cdef double my_cost_fnc(double x):
   
return x*3

# ...

o
= Optimizer()
o
.optimize(my_cost_fnc)

But now, we would like to give people the possibility to implement pure python functions without rebuilding the cython module and that's where my problem starts. Python functions do of course have not the right signature. The first thought was to make a local cdef and use that:

cdef void optimize_wrapper(func):
    cdef
double func_wrapper(double d):
       
return func(d)
    o
.optimize(func_wrapper)

But local cdef is not allowed. Ok, I thought, than I move it out and use functools.partial

cdef double func_wrapper(double d, func):
   
return func(d)

cdef
void optimizer_wrapper(func):
    wrapped
= functools.partial(func_wrapper, func=func)
    o
.optimize(wrapped)

But then functools.partial returns of course a python object.

In the end I did the closure in C++, and wrapped everything with a void pointer and function pointers:

void optimize_wrapper(void* python_function, double (*cost) (void*, double)) {
  optimize
([](double x) { return cost(python_function, x); });  
}

But then I have to touch the original library (or add another cpp file). Although it works, it feels a bit wrong. Is there any other way to achieve, what I want?

Cheers,
Johannes

--

---
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.
Hai Nguyen | 16 Apr 23:32 2015
Picon

weird shape for memory view

Hi all,

I have been using cython memory view for my projects and so far it's terrific. 

However I still don't understand about the shape of the memory view. Please see the example below

from cpython.array cimport array as pyarray
from cpython cimport array

cdef int w=100, h=100
cdef pyarray arr0 = array.clone(pyarray('d', []), w*h, zero=True)
cdef double[:] myview_0 = arr0
cdef double[:, :] myview_1 = <double[:w, :h]> &myview_0[0]

print (myview_1.shape)

I was expecting to get shape=(100, 100) but I got [100, 100, 0, 0, 0, 0, 0, 0]

info:
python 2 (and 3), cython 0.22pre (and 0.22), Windows (and Linux too).

Did I miss anything?
thanks

Hai

--

---
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.
Mori B | 16 Apr 15:44 2015
Picon

Using Cython with Asyncio

Did someone managed to make Cython work with an Asyncio coroutine ? I have a very trivial example that works well in Python 3.4, and refuses to work in Cython 0.22 : it's the following single file that I compile with Cython and execute.

The execution starts correctly but fails to run the coroutine. It seems like Cython modifies the type of the "sometask" coroutine, which is then not treated as usual by Asyncio and Inspect.


#!/usr/bin/env python import asyncio <at> asyncio.coroutine def sometask(): counter = 0 while True: print(counter) yield from asyncio.sleep(1) counter += 1 def runloop(): loop = asyncio.get_event_loop() asyncio.async(sometask()) try: print('Start loop') loop.run_forever() except KeyboardInterrupt: print('Aborted by user') loop.close()

--

---
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.
yann che | 16 Apr 16:05 2015
Picon

pure python declaration of typed memoryviews

Hi guys,

Thanks for the great job you're doing with cython !!!

I'd like to declare memory views in pure python, using
<at> cython.locals(...) or cython.declare(...)

Instead, right now I'm stuck with the cython syntax:
cdef int [::1] T

Anyone has a clue ?

thanks in advance,
yann

--

---
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 | 16 Apr 11:38 2015
Picon

Missing --embed in the cythonize command

Hi,

The cythonize CLI tool is missing an --embed option which, much like
cython’s option of the same name, would generate a main() function, the
only difference would be that --build wouldn’t generate a shared object
but a normal executable file.

Thank you,

-- 
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.
bndp | 16 Apr 05:53 2015
Picon

calling or wrapping a function from cpp api

hello I wanted to use openimageio cpp library for some basic image writing with cython.

I quickly realized I was over my head, here is a basic example from the documentation.
 
//

#include <OpenImageIO/imageio.h>

int main()
{

  const char *filename = "foo.jpeg";
  const int xres = 640, yres = 480;
  const int channels = 3; // RGB
  unsigned char pixels[xres*yres*channels];
  OpenImageIO::ImageOutput *out = OpenImageIO::ImageOutput::create (filename);
    if (! out)
        return false;
  OpenImageIO::ImageSpec spec (xres, yres, channels, OpenImageIO::TypeDesc::UINT8);
  out->open (filename, spec);
  out->write_image (OpenImageIO::TypeDesc::UINT8, pixels);
  out->close ();
  delete out;
}

//

I have spent an embarrassing amount of time trying to work out how to duplicate this example with cython/python. They do provide a python api which works well, but as I only want to write images with it I thought i would try to wrap the image writing part. I would welcome any examples untested even :) . My attempts have led me in circles 

--

---
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.
Jacob Kovac | 15 Apr 07:13 2015
Picon

Cython Equivalent of offsetof doesn't work for some struct members

I am getting a segmentation fault when attempting to find the offset of a non-array member of a struct, the code I am using:

from cython cimport Py_ssize_t
cdef
extern from "Python.h":
    ctypedef
int Py_intptr_t


ctypedef
struct test:
   
float[2] val_1
   
float val_2

cdef test
* tmp = <test*>NULL

offset_1
= <Py_ssize_t> (<Py_intptr_t>(tmp.val_1) - <Py_intptr_t>(tmp))
offset_2
= <Py_ssize_t> (<Py_intptr_t>(tmp.val_2) - <Py_intptr_t>(tmp))

whereas treating val_2 as an array of length 1 returns the expected results:

from cython cimport Py_ssize_t
cdef
extern from "Python.h":
    ctypedef
int Py_intptr_t


ctypedef
struct test:
   
float[2] val_1
   
float[1] val_2

cdef test
* tmp = <test*>NULL

offset_1
= <Py_ssize_t> (<Py_intptr_t>(tmp.val_1) - <Py_intptr_t>(tmp))
offset_2
= <Py_ssize_t> (<Py_intptr_t>(tmp.val_2) - <Py_intptr_t>(tmp))

The segmentation fault for the first code occurs in both cython version 0.22 and 0.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.
Thouis (Ray) Jones | 14 Apr 15:13 2015
Picon

Unnecessary GIL grab in nogil functions

In the code below, the c_sqrt_safe() function grabs the GIL at the end of the function, even if there was no exception raised.  This seems unnecessary, unless I'm misunderstanding something about nogil functions that contain raise statements.

If this behavior is actually necessary, it would be nice for Cython to warn when a nogil function might implicitly grab the gil.

Best,
Ray Jones


Two files below:

//--------------------------------------------------
//  cython_count_gil_grabs.pyx
//--------------------------------------------------
from libc.math cimport sqrt

cdef
extern from "gil_counter.h":
     
int gil_access_count


cdef
float c_sqrt_safe(float value) nogil:
   
if value < 0:
       
with gil:
           
raise ValueError
   
return sqrt(value)

cdef
float c_sqrt(float value) nogil:
   
return sqrt(value)

cpdef test_c_sqrt
():
    cdef
int i
   
print("BEFORE SAFE {}".format(gil_access_count))
   
with nogil:
       
for i in range(100):
            c_sqrt_safe
(i + 1)
   
print("AFTER SAFE {}".format(gil_access_count))


   
print("BEFORE UNSAFE {}".format(gil_access_count))
   
with nogil:
       
for i in range(100):
            c_sqrt
(i + 1)
   
print("AFTER UNSAFE {}".format(gil_access_count))

if __name__ == '__main__':
    test_c_sqrt
()

// --------------------------------------------------
// gil_counter.h
// --------------------------------------------------
static int gil_access_count = 0;

PyGILState_STATE PyGILState_Ensure_Count()
{
    gil_access_count
++;
   
return PyGILState_Ensure();
}

#ifdef WITH_THREAD
#define PyGILState_Ensure PyGILState_Ensure_Count
#endif



--

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