Nils Bruin | 22 Feb 10:00 2015
Picon

Improve introspection of cython functions

If you have a python function, you can query it and piece together how many positional arguments it expects and what keyword names it accepts:

sage: def g(a,b=0):pass
sage: g.__code__.co_argcount
2
sage: g.__code__.co_varnames
('a', 'b')
sage: g.func_defaults
(0,)

If the same function is compiled with cython, the restriction on the accepted argument formats is the same, but the data cannot be pried from the <type 'builtin_function_or_method'> object.
Would it be possible to expose this data either in the docstring or on some attributes on cython function objects?

--

---
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.
Luc Bourhis | 22 Feb 00:09 2015
Picon

Warning: Non-trivial type declarators in shared declaration

I can't find how to silence this warning.

--

---
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.
kloffy | 21 Feb 07:33 2015
Picon

How to remove module built "--inplace" from sys.path?

If I run 'setup.py build_ext --inplace', the directory of the module is added to the Python sys.path. How can I remove this again?

Cheers,
Peter

--

---
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.
Joe Kilner | 20 Feb 13:23 2015
Picon

Accessing PIL data efficiently in Cython

Hi everyone,

I am 18 hours into exploring Cython and trying to use it to speed up some Image processing I am doing. (Please note, I do not want to introduce a dependency on numpy, the whole reason I am interested in Cython is that it avoids needing to worry about distributing numpy alongside the tool I'm building).

Basically, is there a nice way to get at the underlying buffer representation of an image from PIL in Cython that avoids copying memory and ideally gives me [i, j] indexing to get a pixel value

I have two images - one input and one output. I had some code that looked something like this:

    cdef c_array.array in_array = array('i', in_image.getdata(0))
    cdef int* in_val = in_array.data.as_ints
    cdef c_array.array out_array = array('i', [default] * len(in_array))
    cdef int* out = out_array.data.as_ints

    pixel = in_val[x + (y * width)]

However it would seem to me that this is copying the data and also just looks like the wrong way to do things. I feel that something like the following should work:

    cdef int [:, :] in_memoryview = in_image
    cdef int [:, :] out_memoryview = out_image

as everything talking about the "buffer interface" says that "PIL uses it extensively" but without any associated code examples to show how you get to the buffer. Also nothing on whether it is possible to get a writeable memoryview on an image or whether I need to construct a writeable buffer and then map a PIL image on to that.

As I said, I'm just starting out with Cython, so maybe I've got my terminology wrong and am asking the wrong question. But does anyone know what specific piece of voodoo I am asking for and how to get it?

Thanks!

--

---
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.
Valcortez | 20 Feb 00:25 2015
Picon

In 0.22, overwriting base cdef method with except * and then calling base directly makes gcc fail

Hi,

This is an issue which was not a problem in 0.21 with identical code. If the base class (MyClassBase) has a cdef method (apply) which then gets overwritten and except * is added to the method in the derived class, then when calling the method on the base class directly (MyClassBase.apply(self)) then gcc will fail. Following is example code showing the issue:

a.pxd

cdef class MyClassBase(object):
    cdef
void apply(self)

cdef
class MyClassNext(MyClassBase):
    cdef
void apply(self) except *

a.pyx

cdef class MyClassBase(object):
    cdef
void apply(self):
       
return

cdef
class MyClassNext(MyClassBase):
    cdef
void apply(self) except *:
       
return

b.pxd

from a cimport MyClassNext

cdef
class MyClass(MyClassNext):
    cdef
void apply(self) except *

b.pyx

from a cimport MyClassNext

cdef
class MyClass(MyClassNext):
    cdef
void apply(self) except *:
       
MyClassNext.apply(self)

setup.py

from distutils.core import setup
from distutils.extension import Extension
import Cython.Compiler.Options
#Cython.Compiler.Options.annotate = True
from Cython.Distutils import build_ext
from os.path import join, sep, dirname, abspath

def expand(*args):
   
return abspath(join(dirname(__file__), 'src', *args))

def get_extensions_from_sources():
    ext_modules
= []
    pyx
= expand('b.pyx')
    ext_modules
.append(Extension('src.b', [pyx]))
    pyx
= expand('a.pyx')
    ext_modules
.append(Extension('src.a', [pyx]))
   
return ext_modules

ext_modules
= get_extensions_from_sources()

setup
(
    name
='Playground',
    version
='.1',
    author
='Matthew Einhorn',
    ext_modules
=ext_modules,
    cmdclass
={'build_ext': build_ext},
    packages
=['src']
     
)

output then is

python setup.py build_ext --inplace -f
running build_ext
cythoning G
:\Python\libs\Playground\src\cypp_playground.pyx to G:\Python\libs\Pl
ayground
\src\cypp_playground.c
building
'src.cypp_playground' extension
e
:\MinGW\bin\gcc.exe -mdll -O -Wall -Ie:\Python27\include -Ie:\Python27\PC -c G:
\Python\libs\Playground\src\cypp_playground.c -o g:\python\libs\playground\src\c
ypp_playground
.o
G
:\Python\libs\Playground\src\cypp_playground.c: In function '__pyx_f_3src_15cyp
p_playground_7MyClass_apply'
:
G
:\Python\libs\Playground\src\cypp_playground.c:664:3: warning: implicit declara
tion of
function '__pyx_f_12cyplayground_11MyClassNext_apply' [-Wimplicit-functi
on
-declaration]
   __pyx_f_12cyplayground_11MyClassNext_apply
(((struct __pyx_obj_12cyplayground_
MyClassNext *)__pyx_v_self)); if (unlikely(PyErr_Occurred())) {__pyx_filename =
__pyx_f
[0]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
   
^
writing g
:\python\libs\playground\src\cypp_playground.def
e
:\MinGW\bin\gcc.exe -shared -s g:\python\libs\playground\src\cypp_playground.o
g
:\python\libs\playground\src\cypp_playground.def -Le:\Python27\libs -Le:\Python
27\PCbuild -lpython27 -lmsvcr90 -o G:\Python\libs\Playground\src\cypp_playground
.pyd
g
:\python\libs\playground\src\cypp_playground.o:cypp_playground.c:(.text+0x3fe):
 
undefined reference to `__pyx_f_12cyplayground_11MyClassNext_apply'
collect2.exe: error: ld returned 1 exit status
error: command 'e:\\MinGW\\bin\\gcc.exe' failed with exit status 1
make: *** [force] Error 1


Note that for the issue to arise the inheritance needs to be at least two levels deep, like in the example. Also, removing the except * make the issue go away.

--

---
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.
Jeet Mehta | 19 Feb 21:25 2015
Picon

Inheritance: Passing Derived Class as Argument to Function expecting base class

I am using Cython to wrap a set of C++ classes, allowing a Python interface to them. Example Code is provided below:

inheritTest.pyx:

cdef extern from "BaseClass.h": cdef cppclass BaseClass: BaseClass() except + void SetName(string) float Evaluate(float) bool DataExists() cdef extern from "DerivedClass.h": cdef cppclass DerivedClass(BaseClass): DerivedClass() except + void MyFunction() float Evaluate(float) bool DataExists() SetObject(BaseClass *) cdef class PyBaseClass: cdef BaseClass *thisptr def __cinit__(self): self.thisptr = new BaseClass() def __dealloc__(self): del self.thisptr cdef class PyDerivedClass(PyBaseClass): def __cinit__(self): if(self.thisptr): del self.thisptr self.thisptr = new DerivedClass() def __dealloc__(self): del self.thisptr def Evaluate(self, time): return self.thisptr.Evaluate(self,time) def SetObject(self, PyBaseClass inputObject): # *** The issue is right here *** self.thisptr.SetObject(<BaseClass *>inputObject.thisptr)

I want to be able to call SetObject in Python similar to as shown below:

main.py:

from inheritTest import PyBaseClass as base from inheritTest import PyDerivedClass as der a = der() b = der() a.SetObject(b)

I thought it would work since the classes inherit from each other, but its giving me the following error:

Argument has incorrect type: expected PyBaseClass, got PyDerivedClass

Not specifying type in the function definition makes it think that the inputObject is a pure Python object (has no C-based attributes, which it does), in which case the error is:

*Cannot convert Python object to BaseClass *

A sort-of hacky workaround to this just to have Python functions with different names that expect different types of arguments (ex: SetObjectWithBase, SetObjectWithDerived), and then within their implementation, just call the same C-based function having type-casted the input. I know for a fact this works, but I would like to avoid having to do this as much as possible. Even if there is a way I can catch the Type Error within the function, and deal with it inside, I think that might work, but I wasn't sure exactly how to implement that.

Hope this question makes sense, let me know if you require additional information.

Just wondering if this is a Cython bug, if not, how can I get around this

StackOverflow Link: http://stackoverflow.com/questions/28573479/cython-python-c-inheritance-passing-derived-class-as-argument-to-function-e/28615663#28615663

--

---
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.
Tal Einat | 19 Feb 16:22 2015
Picon

testing coverage for Cython code

The latest mention of coverage reporting for Cython code I've found was on this list back in August 2014. According to that thread, the discussed development of this feature never took off and the OP wrote a workaround.

Has there been any progress on this front since? I'd really like this for my project, fuzzysearch. I definitely want to know more than which functions were or weren't called.

- Tal Einat

--

---
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.
Raniere Silva | 19 Feb 13:00 2015
Picon

Google Summer of Code and NumFOCUS

Hi,

NumFOCUS has promotes and supports the ongoing research and development of
open-source computing tools including Cython.

This year NumFOCUS want to try be a Google Summer of Code
"umbrella" mentoring organization,

    Umbrella organizations are mentoring organizations accepted into the Google
    Summer of Code program that have other open source organizations working
    "under" them. Sometime organizations that work very closely or have very
    similar goals or communities may get put together under an "umbrella."
    Google stills expects all organizations under the umbrella, whether accepted
    into the program under their title or not, to adhere to all the rules and
    regulations of the program.

    From https://www.google-melange.com/gsoc/document/show/gsoc_program/google/gsoc2015/help_page#umbrella_organization

To help promote and support Cython.

We encourage Cython to apply to Google Summer of Code under your own title
and will be very happy if you can also do with us.
If you are interested, please check https://github.com/swcarpentry/gsoc2015
and https://github.com/swcarpentry/gsoc2015/blob/master/CONTRIBUTING.md.

If you have any question, please email me directly.

Thanks in advance,
Raniere

--

-- 

--- 
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.
Jason Paryani | 19 Feb 07:25 2015
Picon

"Signature not compatible with previous declaration" problem with Cython 0.22

I'm running into a problem involving function declarations with an `except`. For example (also available up at https://github.com/jparyani/cython-except-signature-mismatch):

test.pxd:
cdef extern from "test.h":
    void reraise_exception()

cdef class TestClass:
    cpdef test_func(self) except +reraise_exception

test.pyx:
cdef class TestClass:
    cdef test_func(self) except +reraise_exception:
        print 'test'


When building, I get the following output:

Compiling test.pyx because it changed.
Cythonizing test.pyx

Error compiling Cython file:
------------------------------------------------------------
...
cdef class TestClass:
    cdef test_func(self) except +reraise_exception:
        ^
------------------------------------------------------------

test.pyx:2:9: Signature not compatible with previous declaration

Error compiling Cython file:
------------------------------------------------------------
...
cdef extern from "test.h":
    void reraise_exception()


cdef class TestClass:
    cpdef test_func(self) except +reraise_exception
                   ^
------------------------------------------------------------

test.pxd:6:20: Previous declaration is here


I saw in the release notes for v0.22 that "Mismatching 'except' declarations on signatures in .pxd and .pyx files failed to produce a compile error." was fixed, but I'm having trouble seeing any kind of mismatch in my declaration above.

--

---
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.
george.trojan | 18 Feb 20:06 2015
Picon

Fortran extensions and setup.py

Hi,

I am creating Cython extension to a FORTRAN library. I do follow advice from www.fortran90.org, that is write an interface using iso_c_binding. My issue is how to compile the code from distutils setup.py. I came up with the following solution:

In setup.py:

import build_ftn_ext    # works only with gcc

def get_extensions():
    if use_cython:
        ext_modules = [
            Extension('nceputils._funcphys',
                      ['nceputils/_funcphys.pyx', 'nceputils/physif.f90'],
                      libraries=['phys', 'gfortran'],
...

where physif.f90 is my interface to library libphys.so and build_ftn_ext is a monkey patch:

import distutils.command.build_ext

# Allows gcc to compile FORTRAN code
_tmp = distutils.command.build_ext.build_ext

class build_ftn_ext(_tmp):
    description = "As build_ext(), allows FORTRAN objects to be made by gcc"

    def build_extensions(self):
        self.compiler.src_extensions.extend(['.f90'])
        for ext in self.extensions:
            ext.extra_compile_args.extend(['-J', self.build_temp])
        _tmp.build_extensions(self)

distutils.command.build_ext.build_ext = build_ftn_ext

This works fine for me, I do not anticipate having to switch to a different compiler. But, is there a better solution?

George

--

---
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.
Giuseppe Attardi | 18 Feb 17:36 2015
Picon

extern C++ function undefined

I define a function in C++ extension, which I declare like this:

cdef extern void distance_matrix(float* m, int rows, int cols)

and define it in a file that is declared in the .pyx heading with:

# distutils: sources = distance_matrix.cpp

When I try to load the extension, the function distance_matrix is undefined.

Indeed, due to name mangling, the code produced by the compiler contains

0000000000010c50 T _Z15distance_matrixPfiiS_

One solution is to wrap the C++ function within
   extern "C" {  .. }

but I was wandering whether is the proper way to tell Cython about C++ functions.


--

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