Josh Ayers | 19 May 2013 02:01
Picon

for loop with variable step not optimized

I just noticed that a for-loop using the standard Python range construct isn't optimized to C-code if the step is a run-time variable.  It is optimized if the step parameter is a compile-time constant, or if the old Pyrex style for-loop syntax is used.  I tested with Cython 0.18 and 0.19.1 and both behaved the same way.

Here's a minimal example.

DEF STEP = 2

def loops():
    cdef int i, start = 0, stop = 10, step = 2

    for i in range(start, stop, step): # generates Python loop
        print(i)

    for i in range(start, stop, STEP): # generates C loop
        print(i)

    for i from start <= i < stop by step: # generates C loop
        print(i)

Is this a bug?

Thanks,
Josh Ayers

--
 
---
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/groups/opt_out.
 
 
Shriramana Sharma | 18 May 2013 08:37
Picon

Spiro Python interface using Cython

Spiro is an algorithm for producing smooth splines given a set of
on-curve control points. I am working on polishing the library. I
wrote a Python interface using Cython (thanks to all those who develop
it!).

The Spiro library uses callbacks, hence my recent in doing that in
Cython. I am happy to say I have been successful. The attached code
demonstrates it to whoever can spend the time to read it. The code is
under the GPLv3+. (Note: you don't need to worry about the internals
of spiro.c -- just look at spiro.h and bezctx.h to understand the
library being wrapped.)

I have one question: Clang generates the warning:

spiro_cy.c:653:1: warning: declaration does not declare anything
[-Wmissing-declarations]
spiro_cp;
^~~~~~~~
1 warning generated.

Why is this and what should I do to fix it? Thanks.

-- 
Shriramana Sharma ஶ்ரீரமணஶர்மா श्रीरमणशर्मा

--

-- 

--- 
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/groups/opt_out.

Attachment (spiro-cython.tar.gz): application/x-gzip, 9 KiB
Justin Israel | 18 May 2013 03:55
Picon
Gravatar

OSX 10.8: setuptools generates gcc command that wants to use to use .c instead of .cpp

This is a strange issue that happens for my project only when someone with OSX 10.8 (mountain lion) tries to build the extension. I don't have 10.8 so I am still waiting on a colleague to forward me a dump of the literal failure, but I figured I could start the question now and provide more info if needed.


The issue is pretty straight forward. On <=10.7.x, when building the extension in place, it properly respects the language="c++" Extension attribute. It cythonizes the pyx => cpp, and runs the proper gcc command to build.

On 10.8.x, it seems it is cythonizing the cpp file just fine, but then it generates a gcc command that is trying to compile a .c that does not exist, obviously. The way I can get it to succeed is if I modify my setup file to not use cython. Then I manually cythonize the pyx => cpp, and have it use the cpp source directly in the setuptools Extension (I have one of those common use_cython style flag setups that I just force to False). 

Does anyone offhand know what might be an issue on osx 10.8? I know apple mucked with the compiler setups a whole bunch, and have seen my fair share of posts containing "problems compiling XYZ on 10.8", requiring some special adjustment. I know that the person who encountered this error had freshly installed the xcode command-line tools to get their compilers, and had no previous setup for building from source code until they were trying to build my extension.

Example setup.py code looks like:

...
cmdclass = {}
if use_cython:
    SRC = 'foo.pyx'
    cmdclass['build_ext'] = build_ext
else:
    SRC = 'foo.cpp'
...
ext = Extension('plow.client.plow',
    [SRC],
    language="c++",
    libraries=[...], 
    extra_compile_args=cflags,
    extra_link_args=ldflags,
    define_macros=[...]
)
...
setup(
   ...
    ext_modules = [ext],
    cmdclass=cmdclass,
   ...
)

--
 
---
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/groups/opt_out.
 
 
Pedro Camargo | 17 May 2013 02:06
Picon

Sorting algorithm

xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">

Hi,

                I’m currently working on a cdef function inside a cython (indexing numpy arrays) module and I need to sort a numpy array (actually need the argsort). How can I proceed?

 

Can I just call array_index=np.argsort(array). Wouldn’t it defeat the purpose of array indexing?  Does anybody have a sorting algorithm in Cython they could share? Couldn’t find anything online.

 

Thanks,

Pedro

--
 
---
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/groups/opt_out.
 
 
Nils Bruin | 16 May 2013 19:18
Picon

cython's property and nondata descriptors

Two questions about cython's "property:" directive.

1] In python, a nondata descriptor is a descriptor that doesn't have
its '__set__' slot filled. Such descriptors can be overridden by
entries in the instance '__dict__'. One might expect that specifying a
cython "property" without giving a "__set__" method would lead to such
an entry, but it doesn't: It leads to a datadescriptor that cannot be
overridden by instance attributes:

%cython
cdef class T(object):
    property B:
        def __get__(self):
            return 1

>>> class S(T): pass
>>> s=S()
>>> s.B=10
AttributeError: attribute 'B' of 'T' objects is not writable

This mirrors the  <at> property decorator in python but it makes it hard to
specify nondata descriptors. Are there thoughts about how to make it
more convenient to declare nondata descriptors

2] Using the  <at> property decorator in a cdef class works, but leads to
less efficient code:

%cython
cdef class T(object):
    property B:
        def __get__(self):
            return 1
     <at> property
    def C(self):
        return 1

>>> t=T()
>>> timeit("t.B")
625 loops, best of 3: 78.6 ns per loop
>>> timeit("t.C")
625 loops, best of 3: 275 ns per loop

would it be desirable to automatically transcribe " <at> property" to
"property .."? Perhaps not, since the semantics of the two are
different and both are valid, but I expect it's a common pitfall for
people who try to translate python to cython code.

--

-- 

--- 
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/groups/opt_out.

Ecython | 16 May 2013 03:54
Picon

static typing of mpf type variables in cython

Hello,


I am a new user of cython. I am working with mpf variables in python
but would like to know its static version in cython for speed up.

For example I was looking for something similar like the following:

python: N=10                                     static typing via cython: cdef int N=10

Is there a similar way to do this with mpf variables? something like:

python:                                              What would be the equivalent static
                                                         version in cython?
from mpmath import mpf
N=mpf(10)                                          cdef mpf_t N=10 ? (but this doesn't work?)

Thank you,


--
 
---
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/groups/opt_out.
 
 
Picon
Gravatar

build error using cython on OS X and Python 3.3 -- __time_t appearing from nowhere

Hi everyone,


I'm having an error building a Cython module on OS X, using Python 3.3.

The code I'm trying to build is https://github.com/esplorio/couchbase-python-client/tree/pylibcouchbase -- it's a fairly simple wrapper for a C library, using an autogenerated .pxd

The output from `python setup.py build_ext --inplace` is: http://pastebin.com/aPv93K5N

I used to get the same error on Python 2.7 but hacked around it using a few lines like: https://github.com/esplorio/couchbase-python-client/blob/pylibcouchbase/couchbase/libcouchbase.pxd#L68 -- so it compiles with Python 2.7 now.

The erroring C typedef is `__time_t`, which is `__darwin_time_t` on OS X, but I have no idea where the `__time_t` is appearing from, or why it's being emitted in the first place (since `time_t` is available on both platforms and doesn't need to be typedef'd).

I'd be grateful for any comments or suggestions.

Thanks,
Rami


--
 
---
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/groups/opt_out.
 
 
Alexander Eisenhuth | 13 May 2013 11:50
Picon

Function pointer as part of a C struct

Hello everybody,

I want to use a callback as part of a C struct with read/write access. 
Is this possible? The following describes the problem. And if you can 
help, how do I connect a python function to the callback?

Any help welcome!

Regards
Alexander

---- stderr of compiling Device.pyx ---
Error compiling Cython file:
------------------------------------------------------------
...
     def __setattr__(self, name, value):
         print "DeviceDescriptor_.__setattr__", name, value
         if name == "deviceNumber":
             self._device_descriptor.deviceNumber = <Uint8> value
         elif name == "deviceMeasurement":
             self._device_descriptor.deviceMeasurement = 
<DeviceMeasureFunc> value
                                                        ^
------------------------------------------------------------
Device.pyx:55:56: Python objects cannot be cast to pointers of primitive 
types

----- Device.h ----
typedef ErrorStatus (*DeviceMeasureFunc)(Uint32*);

typedef struct DeviceDescriptorTag
{
	Uint8 deviceNumber;
	DeviceMeasureFunc deviceMeasurement;

} DeviceDescriptor;
------- Device.pyx ---
cdef extern from "Device.h":

     ctypedef ErrorStatus (*DeviceMeasureFunc)(Uint32*)

     ctypedef struct DeviceDescriptor_c "DeviceDescriptor":
         Uint8 deviceNumber
         DeviceMeasureFunc deviceMeasurement

cdef class DeviceDescriptor:

     cdef DeviceDescriptor_c _device_descriptor

     def __getattr__(self, name):
         ret_val = None
         if name == "deviceNumber":
             ret_val = self._device_descriptor.deviceNumber
         else:
             self.__raise_attrib_not_found(name)
         return ret_val

     def __setattr__(self, name, value):
         if name == "deviceNumber":
             self._device_descriptor.deviceNumber = <Uint8> value
         elif name == "deviceMeasurement":
             self._device_descriptor.deviceMeasurement = 
<DeviceMeasureFunc> value
         else:
             self.__raise_attrib_not_found(name)

     def __raise_attrib_not_found(self, name):
         raise AttributeError("Attribute %s not in %s" % (name, type(self)))

--

-- 

--- 
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/groups/opt_out.

Shriramana Sharma | 12 May 2013 09:24
Picon

TypeError: can't apply this __setattr__

For practice, I am implementing a subclass of builtins.list in Cython
which will store only str-s and has one data member "approved" which
should take only a boolean. It's entirely Python syntax except that it
has a cdef at the head.

However I am getting a TypeError saying "can't apply this __setattr__"
when I try to initialize an object of this type:

>>> from strlist import strlist
>>> a=strlist()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "strlist.pyx", line 24, in strlist.strlist.__init__ (strlist.c:1215)
    self.approved = False
  File "strlist.pyx", line 33, in strlist.strlist.__setattr__ (strlist.c:1417)
    super().__setattr__ ( name, value )
TypeError: can't apply this __setattr__ to strlist.strlist object

OTOH when I try the same using the pure Python implementation of the
class (i.e. just removed cdef) I have no problem:

>>> from strlist_py import strlist
>>> a=strlist()
>>> a
strlist(<super: <class 'strlist'>, <strlist object>>,approved=False)

The code is attached. Can anyone please help me figure out what I'm
doing wrong? Thanks.

-- 
Shriramana Sharma ஶ்ரீரமணஶர்மா श्रीरमणशर्मा

--

-- 

--- 
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/groups/opt_out.

Attachment (subclass-pyclass.tar.gz): application/x-gzip, 1201 bytes
Alok Singhal | 11 May 2013 22:52
Picon
Gravatar

if condition transformation to switch-case

Consider the following code:


cdef extern from "errno.h":
    enum:
        EWOULDBLOCK
        EAGAIN

cdef int is_eagain(int i):
    return i != EWOULDBLOCK and i != EAGAIN

The code checks for EWOULDBLOCK and EAGAIN both, although they could be the same (on linux for example).  This is mostly done for portability (for example, see the "Portability note" at http://www.gnu.org/savannah-checkouts/gnu/libc/manual/html_node/Error-Codes.html#index-EAGAIN-97).

Cython transforms the above code to:

  switch (__pyx_v_i) {
    case EWOULDBLOCK:
    case EAGAIN:
    __pyx_t_1 = 0;
    break;
    default:
    __pyx_t_1 = 1;
    break;
  }

If EWOULDBLOCK == EAGAIN, this results in compilation error since the two case labels are the same.

For my immediate use, I changed my code to only test for EAGAIN, but is there a better way to write the code so that I can still use both EWOULDBLOCK and EAGAIN?

Thanks,
Alok

--
 
---
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/groups/opt_out.
 
 
Stefan Behnel | 11 May 2013 11:01
Picon
Favicon

Cython 0.19.1 released

Hi all,

Cython 0.19.1 is following the course of the recent 0.19 release. This is
mainly a bug fix release that adds only minor new features and fixes some
bugs and regressions in the latest release. Upgrading is generally recommended.

You can get it from here:

http://cython.org/release/Cython-0.19.1.tar.gz

http://cython.org/release/Cython-0.19.1.zip

Release notes:
https://github.com/cython/cython/blob/8f07f8d8265799cada5046fe0b7c0807d7b41fb3/CHANGES.rst

Documentation: http://docs.cython.org/

Major changes include:

* A warning is emitted when negative literal indices are found inside of
  a code section that disables ``wraparound`` handling.  This helps with
  fixing invalid code that might fail in the face of future compiler
  optimisations.

* A regression in 0.19 was fixed that rejected valid C expressions from
  being used in C array size declarations.

* Testing large (> int) C integer values for their truth value could fail
  due to integer wrap-around.

* The automatic signature documentation tries to preserve more semantics
  of default arguments and argument types.  Specifically, ``bint``
  arguments now appear as type ``bool``.

Have fun,

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/groups/opt_out.


Gmane