kasty.jose | 19 Jan 02:06 2015
Picon

weird behaviour with pyobjc

The following example runs fine on OSX 10.9 with Python 2.7.9. It just listens for click events and prints a message:


import sys
import Foundation
import AppKit
import Cocoa
import Quartz.CoreGraphics
from PyObjCTools import AppHelper
frozen = getattr(sys, 'frozen', '')
if frozen:
    foundation = Foundation.Foundation
    appkit = AppKit.AppKit
else:
    foundation = Foundation
    appkit = AppKit
cocoa = Cocoa
quartz = Quartz

class CocoaMonitor:
    def createAppDelegate(self):
        sc = self
        print "createAppDelegate"
        class AppDelegate(foundation.NSObject):
            def applicationDidFinishLaunching_(self, notification):
                print "applicationDidFinishLaunching"
                mask = (appkit.NSLeftMouseDownMask)
                appkit.NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask, sc.handler)
        return AppDelegate

    def run(self):
        appkit.NSApplication.sharedApplication()
        delegate = self.createAppDelegate().alloc().init()
        appkit.NSApp().setDelegate_(delegate)
        appkit.NSApp().setActivationPolicy_(appkit.NSApplicationActivationPolicyProhibited)
        self.workspace = appkit.NSWorkspace.sharedWorkspace()
        AppHelper.runEventLoop()

    def cancel(self):
        AppHelper.stopEventLoop()

    def handler(self, event):
        print "mouse click event"

def main():
    mon = CocoaMonitor()
    mon.run()

if __name__ == '__main__':
    main()

Output:

createAppDelegate
applicationDidFinishLaunching
mouse click event
mouse click event
...


However when I compile it with cython 0.21.1, I don't see any events. This is how I do it:

cython test.py --embed
/usr/bin/clang -fno-strict-aliasing -fno-common -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -arch x86_64 -g -O2 -DNDEBUG -g -O3 `python-config --ldflags` `python-config --libs` `python-config --includes` test.c -o test
./test

Output

createAppDelegate



I used dtruss to inspect system calls but I didn't see anything wrong. Am I missing anything?


--

---
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.
Colin | 13 Jan 21:47 2015
Picon

Possible Bug: Cythonmagic include directories requires explicit ending /

I noticed that for the IPython Notebook, cythonmagic requires an explicit "/" at the end of the directory path to work properly.

Not-working:
%%cythonmagic -I /path/to/directory

Working:
%%cythonmagic -I /path/to/directory/

I was following the numpy.get_include function to define my own custom function for generating the include path. On my system numpy.get_include does not follow the path with a ending "/".

Is the requirement of the ending "/" a bug?

--

---
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.
Skip Montanaro | 13 Jan 18:56 2015
Picon

float v. double

I'm still using 0.17.1 (not much I can really do about it in the short
term). I just discovered a bug in some Cython code. I had naively
declared a couple local variables as float:

        cdef float t = self._time
        cdef float v

thinking that "float" was the same as Python's float (e.g., a C
double). That proved not to be the case. When I changed the
declarations to specify "double", things started working.

Looking in the Language Basics documentation:

http://docs.cython.org/src/userguide/language_basics.html

I saw no mention of the distinction between Python float and Cython
float. It seems like it should be mentioned somewhere. If so, the
searching I did turned nothing up.

Skip Montanaro

--

-- 

--- 
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 Jan 16:49 2015
Picon

Passing Python callbacks to an external C library.

Hi all!

I am writing a partial wrapper around sundials (and ODE solver written in C) for a project of mine and
I wanted to be able to pass python callbacks to the C library using Cython (even though
I mainly pass C pointers to native code).

It was not immediately apparent how I should go about achieving this but I ended up using
a global variable in the Cython module to point to the user provided Python callback.

I implemented a minimal working example around a fictitious "library" which has two functions
for computing the derivative and gradient of functions using a simple forward difference formula.

If my implementation is sound (criticism is very welcome) maybe it could serve as a self contained example
(I could rewrite it for inclusion in the documentation if there is any interest)?

The code is available at:
https://github.com/bjodah/expose_pycallback_to_a_clib


--

---
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.
Max Klein | 12 Jan 04:49 2015
Picon

preserving complex namespaces of C++ libraries in Cython wrapper

I'm in the process of writing a Cython wrapper for a complex C++ library. I
think I've mostly figured out how to write the necessary .pxd and .pyx files.
My problem now is that although my C++ program has about 100 separate 
namespaces, the namespace of the Cython-compiled python library is totally 
flat. 

For example, if I have this in my .pxd file:

cdef extern from "lm/io/hdf5/SimulationFile.h" namespace "lm::io::hdf5":
    cdef cppclass CppHdf5File "lm::io::hdf5::Hdf5File":
        ...

and this in my .pyx file:

cdef class Hdf5File:
    cdef CppHdf5File* thisptr 
    ...

then the Cython-compiled Python library contains a class named Hdf5File. 
Ideally, I'd like the Python to contain a lm.io.hdf5.Hdf5File class (i.e. 
a Hdf5File class in a lm.io.hdf5 module). In other words, I'd like it if 
there was a way to translate the C++ :: scoping operator into the 
Python . dot operator.

Is there a way to get Cython to play nicely with my existing C++ namespaces?

--

-- 

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

Matthew Honnibal | 11 Jan 21:28 2015
Picon

A workaround for specifying compile-time dependencies in setuptools

Python packaging is quite a mess.  The standard library module, distutils, lacks a lot of features, but the replacement, setuptools, brought a lot of its own issues. For a long time setuptools seemed to go out of its way to break Cython compilation, by silently renaming .pyx files to .c files if Pyrex was not installed. This has since been fixed, but the word "Cython" still occurs exactly 0 times in the setuptools documentation.

A key feature that distutils lacks is dependency resolution: if someone wants to install your package, you want specified dependencies to be downloaded and installed automatically. This is unsupported by distutils. There's a "require" keyword argument to distutils.core.setup, but it does absolutely nothing.

The setuptools.setup function supports two keyword arguments for dependencies: setup_requires and install_requires. The packages you specify in install_requires will be installed, but the setup_requires packages will only be downloaded and inserted into sys.path, so that they can be imported during your setup.py script. They will not be available at runtime.

Frustratingly, if you specify the same package in setup_requires and install_requires, it will not be installed:

https://bitbucket.org/pypa/setuptools/issue/209
https://bitbucket.org/pypa/setuptools/issue/80/packages-listed-in-setup_requires-are

I'm shipping a package written in Cython, and it requires certain C header files to be present at compile time. I'd like my users to be able to write "pip install mylibrary". I don't want them to have to write "pip install numpy && pip install mylibrary". If the dependency resolution system is broken, it's broken all the way down the tree. Any library that imports my library will also suffer from this problem.

In the absence of a good solution, here's *a* solution:

https://github.com/syllog1sm/headers_workaround

I uploaded a separate package to PyPi, which distributes the headers I require. I then specify this package as the setup_requires dependency, and add some code to my setup.py file telling the headers_workaround module which headers to install.

Obviously, this is unideal, but it's been working for me. If anyone has better ideas, I'd love to hear them!

Best,
Matt.

--

---
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.
Sturla Molden | 11 Jan 01:33 2015
Picon

Is pickling Cython classes broken on Python 2?


Is pickling of Cython cdef classes broken on Python 2?

Indications:

- I get strange errors when running my old Cython code.
- It still works on Python 3

- New Cython code fail whenever I try to use cPickle on Python 2
- It too works on Python 3

- I find all sorts or complaints on the net:

http://stackoverflow.com/questions/12646436/pickle-cython-class

http://tiku.io/questions/943579/pickle-cython-class

http://stackoverflow.com/questions/18503466/cython-pickling-in-package-not-found-as-error

- Some say it helps to put the constructor in a Python function outside 
the Cython module. But obviously it does not help for classes that have 
C members. But it is weird that a top level def function in a Cython 
module is not found by pickle on Python 2.

Here are some details from a Travis build. You can guess which one
are Python 2 and which one are Python 3:

https://travis-ci.org/scipy/scipy/builds/46581633

Is it just me, or is there something strange going on?

Sturla

--

-- 

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

Alastair McKinley | 8 Jan 14:01 2015

prange reduction "local variable referenced before assignment"

Dear all,

I am sorry for this simple question, I have scoured this group and google for a solution but have come up short.

How can I make the following snippet properly perform the reduction on y in its outer scope?
I have tried placing "global y" in each scope with no results.

from cython.parallel import prange

cdef double y
cdef int k
y=0
with nogil:
        for k in prange(100000):
                y = y + 1

The compile error from cython is "local variable 'y' referenced before assignment".

Best regards,

Alastair




--

---
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 Booth | 8 Jan 02:23 2015
Picon

setting language_level for an imported .pxd file

I'm trying to import a .pxd file from pysam (https://code.google.com/p/pysam/), but I'm running into a problem. The script I'm writing has language_level set to 3, and when I import the pysam .pxd file, I get compile errors. The .pxd file I'm trying to import is written in language_level 2, but the .pxd file doesn't have the language_level set as a directive at the top of the file. Here are some things I tried that didn't work:

cimport cython
with cython.language_level(2):
    cimport pysam.calignmentfile

I also tried:

cimport cython
<at> cython.language_level(2)
def test:
    cimport pysam.calignmentfile

which didn't work. It seems that the only way to set the language_level that actually works is to write a cython directive comment at the top of the file.

One other idea I had was to write a wrapper .pxd file that explicitly sets the language_level to 2, then cimports all the symbols from the .pxd file I need. Then I could cimport the wrapper file and the .pxd file would be interpreted as language_level 2.  

Is there any simpler workaround to solve this problem? I would like to write cython code in language_level 3, but currently this problem is forcing me to use language_level 2.

It seems strange to me that the language_level gets inherited by cimported .pxd files. I think it would make more sense to have every .pxd file default to language_level 2 unless language_level 3 is explicitly set. Or at least provide some way of setting the language_level for cimported files explicitly, such as using a "with" statement like the first example I posted.

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.
Gregorio Bastardo | 7 Jan 11:29 2015
Picon

pointer to constant issue

Hi,

While interfacing an external c lib I ran into a problem, that cython
compilation failed with a trace like:

int bar(Foo_t const* a)

Expected ')', found '*'

---foo.pxd---
cdef extern from "foo.h":
  ctypedef struct Foo_t
  int bar(Foo_t const* a)
---

First I merely copied the function signature from the header, but
after the error changed to the more common format of pointer to
constant in the pxd:

int bar(const Foo_t *a)

and the cython compilation was ok. Did not find any reason for this in
the docs or in the list archives. Anyone knows the background of it?

Thanks,
Gregorio

--

-- 

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

Sturla Molden | 6 Jan 22:35 2015
Picon

Borrowed reference to cdef class?


Basically, how do I do that? I cannot find it in the documentation.

Say I have a cdef class like this:

     cdef class FooBar:
         cdef:
             double x
             double y

I can get a borrowed reference to this object by doing:

     from cpython.ref cimport PyObject
     cdef FooBar foobar = Foobar()
     cdef PyObject *foobar_ptr = <PyObject*> foobar
     <...>

This is all well except I cannot access the fields x and y from 
foobar_ptr because it is the wrong pointer type.

So how can I do this? Typecast to FooBar*? Or does Cython support this 
at all?

Casting back too FooBar does not help,

     cdef FooBar foofoo = <FooBar> foobar_ptr

because now foofoo is not a borrowed reference.

Are there other tricks that can be used?

Is it e.g. possible to infer the C name of the C struct with which 
FooBar is implemented?

This is by the way for scipy.spatial.cKDTree. It still has numerous 
possibilities for leaking memory, and reference counting is not 
acceptable in the algorithmic code. Currently it uses C structs which 
are allocated by malloc and this is the source of the problem. If I 
could make borrowed references from a cdef class, then the allocations 
could be replaced by refcounted Python objects, but the algorithmic code 
would still run without reference counting happening everywhere.

Sturla

--

-- 

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