Nick Repole | 14 Sep 04:30 2014
Picon

C++ wrapping, maintaining namespaces, and submodules?

Is it possible to have Cython submodules? Lets say I have a module mymodule and classes/functions for that module are in mymodule.cpp/mymodule.h. Some utility functions are in utils.cpp/utils.h and are used by mymodule. Can I create a Cython module with the structure:

mymodule
  MyClass
  mymodule_function
  utils
    utils.utils_function

Where utils is a submodule of mymodule? If I were to compile a separate utils.pyx, this seems doable, but the result of that is double compiling (since mymodule requires utils). From a C++ perspective, I use namespaces such that mymodule::MyClass and mymodule::utils::utils_function are how such items are accessed, and I'd ideally like to be able to replicate that structure with Cython.

--

---
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.
Mikhail Korobov | 14 Sep 01:20 2014
Picon

c_string_type, std::string and explicit bytes

Hi,

I'm using Cython 0.21 with the following options:

# cython: c_string_type=str, c_string_encoding=ascii

My goal is to convert std::string to Python bytes. This code:

cdef string my_str
# ...
... <bytes>my_str

generates C++ code that uses __pyx_convert_string_to_py_ (the same as without "<bytes>"); it fails for non-ascii strings in Python 3.x.

<bytes>my_str.c_str()

works as expected - it uses __Pyx_PyBytes_FromString and works for non-ascii strings.

Is it a bug, or am I missing something?

--

---
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 | 12 Sep 22:04 2014
Picon

Compiling with py+pxd file

I asked a question on the code-quality list about a PyLint type thing
for Cython code. Mike Mueller referred me to the pxd file feature. I
have a couple questions about that.

* I'm using Cython 0.17.1. Does that support this feature or do I need
a more recent version?

* Consider this simple function to compute an exponentially weighted
moving average:

def _ewma(alpha, old, new):
    return (1 - alpha) * old + alpha * new

And this Cythonified version:

cdef _ewma(float alpha, float old, float new):
    return (1 - alpha) * old + alpha * new

Do I just duplicate the code, or declare it in the pxd file as something like:

cdef _ewma(float alpha, float old, float new):
    pass

What if the only change to a function is the declaration of the types
of the parameters and a small number of local variables? How much goes
in the pxd file? The examples here:

http://docs.cython.org/src/tutorial/pure.html

give no examples of defining local variables in the pxd file.

Thanks,

Skip

--

-- 

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

Antoine Martin | 12 Sep 16:06 2014
Picon

Cython 0.21 and ImportError raised in module causes segfault eventually

Hi,

There seems to be a bug in Cython 0.21 (or at least an undocumented
change in behaviour): I have some modules that perform some sanity
checks and raise an ImportError on some platforms to prevent the module
from loading.
In previous versions (up to and including 0.20.2), I could just catch
the ImportError and ignore the module, but with Cython 0.21 the
exception is not raised to the location that imports the module (I use
__import__ if that's relevant) and leaves the module in a
half-initialized state which crashes the Python interpreter as soon as
you try to use the module.

Thanks
Antoine

--

-- 

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

Alyson Deines | 11 Sep 18:50 2014
Picon

cimport sage modules

Hi!

I have a (really) basic question.  I want to be able to cimport Sage classes in a project setup like this:
Project/
setup.py
text.pyx

When I use Cython in the Sage notebook, I can cimport via:
 
%cython
from sage.rings.integer cimport Integer

and this auto generates come code. 

Right now my setup.py file is:

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

setup(   
    cmdclass = {'build_ext': build_ext},
    ext_modules = [Extension('test',['test.pyx'])]
)
and my text.pyx file is:

from sage.rings.integer cimport Integer

When I try to compile I get the following error:

~$ sage -python setup.py build_ext --inplace
running build_ext
cythoning test.pyx to test.c
 
Error compiling Cython file:
------------------------------------------------------------
...
from sage.rings.integer cimport Integer
^
------------------------------------------------------------
 
test.pyx:1:0: 'sage.rings.integer.pxd' not found
 
Error compiling Cython file:
------------------------------------------------------------
...
from sage.rings.integer cimport Integer
^
------------------------------------------------------------
 
test.pyx:1:0: 'Integer.pxd' not found
 
Error compiling Cython file:
------------------------------------------------------------
...
from sage.rings.integer cimport Integer
                               ^
------------------------------------------------------------
 
test.pyx:1:32: Name 'Integer' not declared in module 'sage.rings.integer'
building 'test' extension
gcc -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -I/usr/local/sage/sage-6.3.beta6/local/include/python2.7 -c test.c -o build/tem
p.linux-x86_64-2.7/test.o
test.c:1:2: error: #error Do not use this file, it is the result of a failed Cython compilation.
 #error Do not use this file, it is the result of a failed Cython compilation.
  ^
error: command 'gcc' failed with exit status 1

I've tried changing my setup.py file to:

import os
import sys

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

if not 'SAGE_ROOT' in os.environ:
    print " ERROR: The environment variable SAGE_ROOT must be defined."
    sys.exit(1)
else:
    SAGE_ROOT = os.environ['SAGE_ROOT']   
setup(   
    cmdclass = {'build_ext': build_ext},
    ext_modules = [Extension('test',
                    ['test.pyx'],
                    include_dirs = [
                    os.path.join(SAGE_ROOT, 'local/include'),
                    os.path.join(SAGE_ROOT, 'local/include/csage'),
                    os.path.join(SAGE_ROOT, 'devel/sage/sage/ext'),
                    os.path.join(SAGE_ROOT, 'src/sage/ext'),
                    os.path.join(SAGE_ROOT, 'devel/sage'),
                    os.path.join(SAGE_ROOT, 'src')],
                    library_dirs = [os.path.join(SAGE_ROOT, 'local/lib')],)]
)


but I still get the following error:

~$ sage -python setup.py build_ext --inplace
running build_ext
cythoning test.pyx to test.c
 
Error compiling Cython file:
------------------------------------------------------------
...
from sage.rings.integer cimport Integer
^
------------------------------------------------------------
 
test.pyx:1:0: 'sage.rings.integer.pxd' not found
 
Error compiling Cython file:
------------------------------------------------------------
...
from sage.rings.integer cimport Integer
^
------------------------------------------------------------
 
test.pyx:1:0: 'Integer.pxd' not found

Thanks!

Aly

--

---
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 | 10 Sep 18:40 2014
Picon

Cython 0.21 released

Hi all,

on behalf of the Cython dev team, I'm pleased to announce the release of
Cython 0.21, a major feature release. Thanks everyone who contributed code,
documentation improvements, test feedback, bug reports and/or otherwise
helpful insights for this release.

I copied the complete changelog of this release below, but here are the
major highlights, in addition to the many bug fixes and general
improvements in this release.

* Legacy code and support for old CPython versions was removed, baselines
are now Python 2.6 and 3.2.

* cdef functions finally support inner Python functions with closures.

* C enums can now be declared as "cpdef" to publish them to Python space.

* Python method calls, boolean "and/or" operators and cascaded assignments
were optimised to streamline calls and type coercions.

* The annotated HTML code output is syntax highlighted and beautified.

Have fun,

Stefan

Downloads:
https://pypi.python.org/pypi/Cython/0.21

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

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

Signatures:
http://cython.org/release/Cython-0.21.tar.gz.asc
http://cython.org/release/Cython-0.21.zip.asc

Changelog:
https://github.com/cython/cython/blob/c67e895414aac90dfe9f789530143cff5b2ec7ad/CHANGES.rst

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

0.21 (2014-09-10)
=================

Features added
--------------

* C (cdef) functions allow inner Python functions.

* Enums can now be declared as cpdef to export their values to
  the module's Python namespace.  Cpdef enums in pxd files export
  their values to their own module, iff it exists.

* Allow  <at> staticmethod decorator to declare static cdef methods.
  This is especially useful for declaring "constructors" for
  cdef classes that can take non-Python arguments.

* Taking a ``char*`` from a temporary Python string object is safer
  in more cases and can be done inside of non-trivial expressions,
  including arguments of a function call.  A compile time error
  is raised only when such a pointer is assigned to a variable and
  would thus exceed the lifetime of the string itself.

* Generators have new properties ``__name__`` and ``__qualname__``
  that provide the plain/qualified name of the generator function
  (following CPython 3.5).  See http://bugs.python.org/issue21205

* The ``inline`` function modifier is available as a decorator
  `` <at> cython.inline`` in pure mode.

* When cygdb is run in a virtualenv, it enables the same virtualenv
  inside of the debugger. Patch by Marc Abramowitz.

* PEP 465: dedicated infix operator for matrix multiplication (A  <at>  B).

* HTML output of annotated code uses Pygments for code highlighting
  and generally received a major overhaul by Matthias Bussonier.

* IPython magic support is now available directly from Cython with
  the command "%load_ext cython".  Cython code can directly be
  executed in a cell when marked with "%%cython".  Code analysis
  is available with "%%cython -a".  Patch by Martín Gaitán.

* Simple support for declaring Python object types in Python signature
  annotations.  Currently requires setting the compiler directive
  ``annotation_typing=True``.

* New directive ``use_switch`` (defaults to True) to optionally disable
  the optimization of chained if statement to C switch statements.

* Defines dynamic_cast et al. in ``libcpp.cast`` and C++ heap data
  structure operations in ``libcpp.algorithm``.

* Shipped header declarations in ``posix.*`` were extended to cover
  more of the POSIX API.  Patches by Lars Buitinck and Mark Peek.

Optimizations
-------------

* Simple calls to C implemented Python functions/methods are faster.
  This also speeds up many operations on builtins that Cython cannot
  otherwise optimise.

* The "and"/"or" operators try to avoid unnecessary coercions of their
  arguments.  They now evaluate the truth value of each argument
  independently and only coerce the final result of the whole expression
  to the target type (e.g. the type on the left side of an assignment).
  This also avoids reference counting overhead for Python values during
  evaluation and generally improves the code flow in the generated C code.

* The Python expression "2 ** N" is optimised into bit shifting.
  See http://bugs.python.org/issue21420

* Cascaded assignments (a = b = ...) try to minimise the number of
  type coercions.

* Calls to ``slice()`` are translated to a straight C-API call.

Bugs fixed
----------

* Crash when assigning memory views from ternary conditional expressions.

* Nested C++ templates could lead to unseparated ">>" characters being
  generated into the C++ declarations, which older C++ compilers could
  not parse.

* Sending SIGINT (Ctrl-C) during parallel cythonize() builds could
  hang the child processes.

* No longer ignore local setup.cfg files for distutils in pyximport.
  Patch by Martin Teichmann.

* Taking a ``char*`` from an indexed Python string generated unsafe
  reference counting code.

* Set literals now create all of their items before trying to add them
  to the set, following the behaviour in CPython.  This makes a
  difference in the rare case that the item creation has side effects
  and some items are not hashable (or if hashing them has side effects,
  too).

* Cython no longer generates the cross product of C functions for code
  that uses memory views of fused types in function signatures (e.g.
  ``cdef func(floating[:] a, floating[:] b)``).  This is considered the
  expected behaviour by most users and was previously inconsistent with
  other structured types like C arrays.  Code that really wants all type
  combinations can create the same fused memoryview type under different
  names and use those in the signature to make it clear which types are
  independent.

* Names that were unknown at compile time were looked up as builtins at
  runtime but not as global module names.  Trying both lookups helps with
  globals() manipulation.

* Fixed stl container conversion for typedef element types.

* ``obj.pop(x)`` truncated large C integer values of x to ``Py_ssize_t``.

* ``__init__.pyc`` is recognised as marking a package directory
  (in addition to .py, .pyx and .pxd).

* Syntax highlighting in ``cython-mode.el`` for Emacs no longer
  incorrectly highlights keywords found as part of longer names.

* Correctly handle ``from cython.submodule cimport name``.

* Fix infinite recursion when using super with cpdef methods.

* No-args ``dir()`` was not guaranteed to return a sorted list.

Other changes
-------------

* The header line in the generated C files no longer contains the
  timestamp but only the Cython version that wrote it.  This was
  changed to make builds more reproducible.

* Removed support for CPython 2.4, 2.5 and 3.1.

* The licensing implications on the generated code were clarified
  to avoid legal constraints for users.

--

-- 

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

Marc-Alexandre Côté | 9 Sep 04:05 2014
Picon

Fused types problems with class inheritance

Hi, I am using Cython 0.20.2 on LinuxMint 17 x64.

I created a basic example to reproduce the error:

Does someone encounter this problem before?

--
Marc

--

---
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.
Tom Tom | 8 Sep 23:14 2014
Picon

Install Cython into Python3X

Hi,
Can anyone tell me how to install cython on Python 3X series.

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/d/optout.
Stefan Behnel | 8 Sep 22:23 2014
Picon

Cython 0.21 release candidate

Hi all,

I uploaded a release candidate for 0.21. There were a couple of more
changes and cleanups since the last beta, so please give it another quick
round of testing.

http://www.cython.org/release/Cython-0.21rc1.tar.gz

http://www.cython.org/release/Cython-0.21rc1.tar.gz.asc

Complete changelog:

https://github.com/cython/cython/blob/8337b8aec2ecbb4480d551e12b1faf9b21ba2cb1/CHANGES.rst

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.

Joshua Landau | 8 Sep 13:54 2014

Suggested addition to the docs on memory allocation

I have this Stack Overflow post:

http://stackoverflow.com/a/21054369/1763356

People seem to find it useful and I recently got a comment saying that
this should be in the docs. Thus, I'm raising the idea here. It might
make sense to just have a link, but you could also properly integrate
it if you have an appropriate place. One advantage of a link is that I
can keep it up to date (you can, too, being on Stack Overflow).

--

-- 

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

Nathan Goldbaum | 4 Sep 22:05 2014
Picon

cython.parallel.prange num_threads keyword argument must be a constant

I have the following test code:

    from cython import parallel

    def test_function(num_threads=4):
        cdef int i

        with nogil:
            for i in parallel.prange(10, num_threads=num_threads):
                pass

When I cythonize and compile this, I get the following error from gcc 4.4.6:

    [goldbaum <at> eudora cython_test]$ python setup.py install
    running install
    running build
    running build_ext
    cythoning test.pyx to test.c
    building 'test' extension
    gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/pfs/goldbaum/anaconda/include/python2.7 -c test.c -o build/temp.linux-x86_64-2.7/test.o -fopenmp
    test.c: In function ‘__pyx_pf_4test_test_function’:
    test.c:765: error: expected integer expression before end of line
    error: command 'gcc' failed with exit status 1

The autogenerated c near line 765 looks like this:

 761            __pyx_t_2 = (10 - 0) / 1;
 762            if (__pyx_t_2 > 0)
 763            {
 764                #ifdef _OPENMP
 765                #pragma omp parallel num_threads(__pyx_v_num_threads)
 766                #endif /* _OPENMP */
 767                {
 768                    #ifdef _OPENMP
 769                    #pragma omp for firstprivate(__pyx_v_i) lastprivate(__pyx_v_i)


If I change the argument of the num_threads keyword argument to a constant, it builds and runs correctly.

I'm not sure whether this is a bug, a misunderstanding on my part, or simply an undocumented caveat of the way prange is transpiled to openmp pragmas. Optimally I'd like to be able to control the number of threads used in the prange from python.

Best,

Nathan Goldbaum

--

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