Eric Reynolds | 30 Jul 09:52 2015
Picon

Cdef methods don't get bound to self

Hello, it's me again.

I apologise if this question has already been asked, I tried searching the docs, the UG and the internet for answers but didn't find anything.

Let's start with the code:

cdef class X:
def def_method(self):
print "Hello from a def method"

cpdef cpdef_method(self):
print "Hello from a cpdef method"

cdef cdef_method(self):
print "Hello from a cdef method"

cdef X x = X()

# def methods get bound to self like Python
f = x.def_method
f()

# cpdef methods get bound to self like Python
f = x.cpdef_method
f()

# cdef methods work differently
f = x.cdef_method
f(x)

# this fails
f()

So my question is, why this design choice, if it is indeed intentional? After all creating automatically creating a bound cdef method wrapper object wouldn't be too difficult I would have thought, and access the unbound function could still be available from the class object (i.e. X.cdef_method).

Thanks in advance for all your support, kind regards,

Eric.

--

---
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 | 29 Jul 20:02 2015
Picon

[Cython] Cython 0.23 beta 2 released

Hi everyone,

I just uploaded a second beta for the upcoming Cython 0.23. Please give it
some more testing as there were substantial code changes since the first beta.

You can get the signed release from here:

http://cython.org/release/

http://cython.org/release/Cython-0.23b2.tar.gz

http://cython.org/release/Cython-0.23b2.zip

SHA1 sums:
1d12e335116ba3fcbe731d83ccc5ee5f0f6e5371  Cython-0.23b2.tar.gz
6f063e6b08b6dbecbadd44ed6c6150bc799fa230  Cython-0.23b2.zip

Changes since beta 1 (in addition to general bug fixes):

* The builtin ``type`` type is now declared as PyTypeObject in source,
  allowing for extern functions taking type parameters to have the correct
  C signatures.  Note that this might break code that uses ``type`` just
  for passing around Python types in typed variables.  Removing the type
  declaration provides a backwards compatible fix.

* Support operator bool() for C++ classes so they can be used in if
  statements.

* Installation under CPython 3.3+ no longer requires a pass of the
  2to3 tool.  This also makes it possible to run Cython in Python
  3.3+ from a source checkout without installing it first.
  Patch by Petr Viktorin.

* Coverage analysis and async/await were adapted to recent changes in
  coverage.py and CPython.

Complete changelog follows below.

Since we're in beta phase now, new features should not generally be added
for the release any more, but we can make exceptions for currently
existing pull requests if you can provide a good motivation. If you think
we forgot something, sending a timely reply to this email or reviving an
existing mailing list thread or pull request is a good idea.

Have fun,

Stefan

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

* PEP 492 (async/await) was implemented.
  See https://www.python.org/dev/peps/pep-0492/

* PEP 448 (Additional Unpacking Generalizations) was implemented.
  See https://www.python.org/dev/peps/pep-0448/

* Support for coverage.py 4.0+ can be enabled by adding the plugin
  "Cython.Coverage" to the ".coveragerc" config file.

* Annotated HTML source pages can integrate (XML) coverage reports.

* Tracing is supported in ``nogil`` functions/sections and module init
  code.

* When generators are used in a Cython module and the module imports the
  modules "inspect" and/or "asyncio", Cython enables interoperability by
  patching these modules during the import to recognise Cython's internal
  generator and coroutine types. This can be disabled by C compiling the
  module with "-D CYTHON_PATCH_ASYNCIO=0" or "-D CYTHON_PATCH_INSPECT=0"

* When generators or coroutines are used in a Cython module, their types
  are registered with the ``Generator`` and ``Coroutine`` ABCs in the
  ``collections`` or ``collections.abc`` stdlib module at import time to
  enable interoperability with code that needs to detect and process Python
  generators/coroutines.  These ABCs were added in CPython 3.5 and are
  available for older Python versions through the ``backports_abc`` module
  on PyPI.  See https://bugs.python.org/issue24018

* Adding/subtracting/dividing/modulus and equality comparisons with
  constant Python floats and small integers are faster.

* Binary and/or/xor/rshift operations with small constant Python integers
  are faster.

* When called on generator expressions, the builtins ``all()``, ``any()``,
  ``dict()``, ``list()``, ``set()``, ``sorted()`` and ``unicode.join()``
  avoid the generator iteration overhead by inlining a part of their
  functionality into the for-loop.

* Keyword argument dicts are no longer copied on function entry when they
  are not being used or only passed through to other function calls (e.g.
  in wrapper functions).

* The ``PyTypeObject`` declaration in ``cpython.object`` was extended.

* The builtin ``type`` type is now declared as PyTypeObject in source,
  allowing for extern functions taking type parameters to have the correct
  C signatures.  Note that this might break code that uses ``type`` just
  for passing around Python types in typed variables.  Removing the type
  declaration provides a backwards compatible fix.

* ``wraparound()`` and ``boundscheck()`` are available as no-ops in pure
  Python mode.

* Const iterators were added to the provided C++ STL declarations.

* ``NULL`` is allowed as default argument when embedding signatures.
  This fixes ticket 843.

* When compiling with ``--embed``, the internal module name is changed to
  ``__main__`` to allow arbitrary program names, including those that would
  be invalid for modules.  Note that this prevents reuse of the generated
  C code as an importable module.

* External C++ classes that overload the assignment operator can be used.
  Patch by Ian Henriksen.

* Support operator bool() for C++ classes so they can be used in if
  statements.

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

* Calling "yield from" from Python on a Cython generator that returned a
  value triggered a crash in CPython.  This is now being worked around.
  See https://bugs.python.org/issue23996

* Language level 3 did not enable true division (a.k.a. float division)
  for integer operands.

* Functions with fused argument types that included a generic 'object'
  fallback could end up using that fallback also for other explicitly
  listed object types.

* Relative cimports could accidentally fall back to trying an absolute
  cimport on failure.

* The result of calling a C struct constructor no longer requires an
  intermediate assignment when coercing to a Python dict.

* C++ exception declarations with mapping functions could fail to compile
  when pre-declared in .pxd files.

* ``cpdef void`` methods are now permitted.

* ``abs(cint)`` could fail to compile in MSVC and used sub-optimal code
  in C++.  Patch by David Vierra, original patch by Michael Enßlin.

* Buffer index calculations using index variables with small C integer
  types could overflow for large buffer sizes.
  Original patch by David Vierra.

* C unions use a saner way to coerce from and to Python dicts.

* When compiling a module ``foo.pyx``, the directories in ``sys.path``
  are no longer searched when looking for ``foo.pxd``.
  Patch by Jeroen Demeyer.

* Memory leaks in the embedding main function were fixed.
  Original patch by Michael Enßlin.

* Some complex Python expressions could fail to compile inside of finally
  clauses.

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

* Changed mangling scheme in header files generated by ``cdef api``
  declarations.

* Installation under CPython 3.3+ no longer requires a pass of the
  2to3 tool.  This also makes it possible to run Cython in Python
  3.3+ from a source checkout without installing it first.
  Patch by Petr Viktorin.

--

-- 

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

Sam Amini | 29 Jul 16:10 2015
Picon

error in installing cython

Dear,

I am trying to install cython on my home directory without root access.

The python version I have is Python-2.7.10. It's also installed locally.

Here is what I get from typing in: python setup.py install


Cython/Parser/Grammar: No such file or directory
Traceback (most recent call last):
 File "setup.py", line 288, in <module>
   compile_cython_modules(cython_profile, cython_compile_more, cython_with_refnanny)
 File "setup.py", line 134, in compile_cython_modules
   os.path.join(parser_dir, 'graminit.c'),
 File "/tools/python/Python-2.7.10/Lib/subprocess.py", line 540, in check_call
   raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['/tools/python/Python-2.7.10/Include/../Parser/pgen',
'Cython/Parser/Grammar', 'Cython/Parser/graminit.h', 'Cython/Parser/graminit.c']' returned non-zero exit status 1


Could you please let me know what went wrong here?

Kinds regards,
Sam

--

---
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.
Damian Vuleta | 28 Jul 08:12 2015
Picon

Unable to Cythonise .pyx file

I apologise in advance if this is an old topic. I'm new to Cython and am probably overlooking something basic, but I haven't been able to find help elsewhere.

I have a short Python module I wish to Cythonise:

# Define Jumble
def Jumble(TestList[], SequenceX):
    TLen = len(TestList)
    J = 0
    for I in range(TLen):
        J = (J + int(SequenceX[I:I+6])) % TLen
        TestList[I], TestList[J] = TestList[J], TestList[I]
    return TestList

# Define De-Jumble
def DeJumble(TestList, SequenceX):
    TLen = len(TestList)
    J = 0
    for I in range(TLen):
        J = (J + int(SequenceX[I:I+6])) % TLen
    for I in reversed(range(TLen)):
        TestList[I], TestList[J] = TestList[J], TestList[I]
        J = (J - int(SequenceX[I:I+6])) % TLen
    return TestList

(TestList is a list of integers and SequenceX is a text string of numbers.)

I create a basic setup.py file:

from distutils.core import setup
from Cython.Build import cythonize

setup(
    ext_modules = cythonize("JumDe.pyx")
)

and run python3 setup.py build_ext --inplace from Terminal. It produces a .so file (albeit with a lot of warning messages) and I can successfully import it and use it in my main program, with a noticeable speed increase.

When I rename the module with a .pyx suffix, change the setup file and run it again, up comes this error message:

Traceback (most recent call last):
  File "setup.py", line 5, in <module>
    ext_modules = cythonize("JumDe.pyx")
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/Cython/Build/Dependencies.py", line 754, in cythonize
    aliases=aliases)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/Cython/Build/Dependencies.py", line 649, in create_extension_list
    for file in nonempty(extended_iglob(filepattern), "'%s' doesn't match any files" % filepattern):
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/Cython/Build/Dependencies.py", line 103, in nonempty
    raise ValueError(error_msg)
ValueError: 'JumDe.pyx' doesn't match any files

This happens whether I put C type declarations in the module or not. So what am I overlooking?

Using Python 3.4.3, Cython 0.22.1, Mac OS X 10.10.4

--

---
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.
slonik | 27 Jul 23:16 2015
Picon

Compiler crash in AnalyseExpressionsTransform

Hi All,
the following (arguably not very useful code snippet) causes cython to crash. Both git master (0987482...) and 0.22.x seem to be affected:

# foo.pyx
from libcpp.vector cimport vector

def foo(vector[int] iv):
    if  iv is None:
        pass

$ cython -o foo.cc --cplus -2 -f -v foo.pyx

Error compiling Cython file:
------------------------------------------------------------
...
# foo.pyx
from libcpp.vector cimport vector

def foo(vector[int] iv):
    if  iv is None:
          ^
------------------------------------------------------------

foo.pyx:5:11: Invalid types for 'is' (vector[int], Python object)

Error compiling Cython file:
------------------------------------------------------------
...
# foo.pyx
from libcpp.vector cimport vector

def foo(vector[int] iv):
    if  iv is None:
          ^
------------------------------------------------------------

foo.pyx:5:11: Compiler crash in AnalyseExpressionsTransform

ModuleNode.body = StatListNode(foo.pyx:2:0)
StatListNode.stats[0] = DefNode(foo.pyx:4:0,
......
.....[snip]...
.....
Compiler crash traceback from this point on:
  File "/Users/leor/dev/Python/Cython/cython/Cython/Compiler/ExprNodes.py", line 11365, in coerce_to_boolean
    if self.is_pycmp:
AttributeError: 'PrimaryCmpNode' object has no attribute 'is_pycmp'
make: *** [foo.cc] Error 1

--Leo


--

---
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.
Ian Henriksen | 27 Jul 22:28 2015
Picon

cdef-only global variables don't work as expected

Module-level variables declared using cdef are only
declared and used within the functions that use them.
This can be a problem when these variables are used
to store a global value needed in different functions.

For example:

# global_test.pyx
cdef int l = 1
def set_l(int val):
    l = val
def get_l():
    return l

#test.py
import global_test as g
g.set_l(2)
print g.get_l()

Running this test script, strangely enough, prints 1
since the local variable is declared and initialized to 1
inside each function where it is used, rather than being
stored as a module-level value. Even when the variable
is uninitialized, it is set by default to 0 and re-declared
within each function.

This same behavior is preserved, even when the global
variable is declared as extern.

Was this a deliberate design decision to make
Cython's scoping behave more like Python's, or is this
a bug? This strikes me as a case where practicality
beats purity, but I'd be happy to hear other
opinions.

Thanks!
-Ian Henriksen

--

---
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.
Topi Wala | 27 Jul 20:40 2015
Picon

pyinstaller

I have a multi-package python tree, that I've cythonized, and am successfully using with the python interpreter. I'd now like to package this whole tree for delivery, and am looking at pyinstaller.

#1. Could anyone point me to a project that has successfully used pyinstaller to package a multi-directory cython tree (along with a few python, some data files)

#2. Short of that, what do the .so files qualify as, pure python, or binaries?


--

---
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.
houbaastef . | 27 Jul 03:19 2015
Picon

segmentation faults on mac osx 10.10

today i ran into a segfault when wrapping a C++ library. Anything i tried to put into a C++ output streams just triggered a segfault.

the problem disappeared when i added this in setup.py:

os.environ["MACOSX_DEPLOYMENT_TARGET"] = "10.9"4 hours lost ;)

Cheers

--

---
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.
Tim Hopper | 24 Jul 22:02 2015

AnalyseExpressionsTransform when reordering arguments.

I am wrapping C++11 with Cython in a fairly large project. I have [this initialization method in C++](https://github.com/datamicroscopes/lda/blob/0f27d5d79caeecef8e76c61eb244f32f838c971f/include/microscopes/lda/model.hpp#L43-L48) that I am trying to expose to Python via Cython. I'm doing `cdef extern` [here](https://github.com/datamicroscopes/lda/blob/0f27d5d79caeecef8e76c61eb244f32f838c971f/microscopes/lda/_model_h.pxd#L30-L35) then calling that from [here](https://github.com/datamicroscopes/lda/blob/0f27d5d79caeecef8e76c61eb244f32f838c971f/microscopes/lda/_model.pyx#L46-L52). 

This function call is as follows:

```
        self._thisptr = c_initialize(defn=defn._thisptr.get()[0],
                                     alpha=dish_hps['alpha'],
                                     beta=vocab_hp,
                                     gamma=dish_hps['gamma'],
                                     initial_dishes=initial_dishes,
                                     docs=_data,
                                     rng=r._thisptr[0])
```

I'm using named arguments, but they are in the same order that `c_initialize` specifies them.

However, if I switch the order of any two of the named arguments, I get an `microscopes/lda/_model.pyx:48:51: Compiler crash in AnalyseExpressionsTransform` error. Here is the full (I think) error generated:

```
    Error compiling Cython file:
    ------------------------------------------------------------
    ...
    
    
            cdef vector[vector[size_t]] _data = deepcopy(data)
            self._thisptr = c_initialize(defn=defn._thisptr.get()[0],
                                         beta=vocab_hp,
                                         alpha=dish_hps['alpha'],
                                                      ^
    ------------------------------------------------------------
    
    microscopes/lda/_model.pyx:48:51: Compiler crash in AnalyseExpressionsTransform
    
    ModuleNode.body = StatListNode(_model.pyx:2:0)
    StatListNode.stats[3] = CClassDefNode(_model.pyx:6:5,
        as_name = u'state',
        class_name = u'state',
        doc = u'The underlying state of an HDP-LDA\n    You should not explicitly construct a state object.\n    Instead, use :func:`initialize`.\n    Notes\n    -----\n    This class is not meant to be sub-classed.\n    ',
        module_name = u'',
        visibility = u'private')
    CClassDefNode.body = StatListNode(_model.pyx:7:4)
    StatListNode.stats[0] = DefNode(_model.pyx:14:4,
        modifiers = [...]/0,
        name = u'__cinit__',
        num_required_args = 4,
        py_wrapper_required = True,
        reqd_kw_flags_cname = '0',
        used = True)
    File 'Nodes.py', line 421, in analyse_expressions: StatListNode(_model.pyx:16:8)
    File 'Nodes.py', line 4695, in analyse_expressions: SingleAssignmentNode(_model.pyx:46:36)
    File 'Nodes.py', line 4804, in analyse_types: SingleAssignmentNode(_model.pyx:46:36)
    File 'ExprNodes.py', line 5233, in analyse_types: GeneralCallNode(_model.pyx:46:36,
        result_is_used = True,
        use_managed_ref = True)
    File 'ExprNodes.py', line 5356, in map_to_simple_call_node: GeneralCallNode(_model.pyx:46:36,
        result_is_used = True,
        use_managed_ref = True)
    File 'ExprNodes.py', line 2853, in is_simple: IndexNode(_model.pyx:48:51,
        is_subscript = True,
        result_is_used = True,
        use_managed_ref = True)
    
    Compiler crash traceback from this point on:
      File "/home/travis/miniconda/envs/build/lib/python2.7/site-packages/Cython/Compiler/ExprNodes.py", line 2853, in is_simple
        if self.is_buffer_access or self.memslice_index:
    AttributeError: 'IndexNode' object has no attribute 'is_buffer_access'
```

I'm fairly new to Cython. Perhaps I'm trying to do something unsupported? 

I'm using `Cython version 0.22.1` on Yosemite.

--

---
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.
slonik | 24 Jul 18:51 2015
Picon

[Q] how to use C++ object in a boolean context?

Hi All,

Quite a few classes in the C++ standard library (std::uniq_ptr, std::shared_ptr, std::function, io-streams, etc) define "operator bool()" so that their objects can be used in a boolean context (e.g. conditionals):

#include <memory>
std::unique_ptr<int> uptr;
if(uptr) {do something if uptr contains non-null ptr}

I am curious how to use such C++ objects in Cython conditionals ??
To the best of my knowledge Cython does not support "operator bool()" for C++ types.
Instead, there is support for the C++ "operator!()".

In Cython I can do the following:

cdef extern from "<memory>" namespace "std" nogil:
    cdef cppclass unique_ptr[T]:
        unique_ptr()
        bint operator!()
        # remaining declarations ...

Now I can use conditionals together with the 'not' logical operation:

if not uptr:  # do something if uptr is false

Unfortunately, a simple Cython construction " if uptr: "
results in Cython compile-time error: "Type 'unique_ptr[int]' not acceptable as a boolean"

An ugly work around would be to use double "not":

if not not uptr: # this works but is ugly!

Does anyone know a cleaner way to use C++ objects in a boolean context in Cython ??

To keep the discussion focused I would like to reiterate that the question is about C++ objects and *NOT* python extension types. Suggestions to use "def __bool__(self)" or "def __nonrezo__()" would not work in the cython's C++ context.

Thanks,
--Leo

--

---
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.
slonik | 22 Jul 20:41 2015
Picon

Re: c++ operator bool() and cython if statement


On Wednesday, July 22, 2015 at 1:47:22 PM UTC-4, Hai Nguyen wrote:
how's about wrapping it in __bool__ magic method?

Hai

On Wed, Jul 22, 2015 at 12:28 PM, slonik <slon... <at> gmail.com> wrote:
Hi All,
I have a C++ class Foo that defines operator bool() to check the state of an object.
How can I make use of it within Cython conditionals?  Ideally I would like to type something like this:

if  foo:
    # do something
else:
    raise Error ....

So far I was only able to come up with an ugly workaround:
####
cdef extern from "foo.h":
    cdef cppclass Foo:
         bint operator!(Foo&)
####

and then within my Cython code:
if not foo: # testing for False
....
if not not foo: # testing for True
....

Is there a simpler and less ugly way to use c++ bool conversion operator from within Cython?

--Leo

--

---
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... <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


I am talking about exposing C++ class with 'cdef  cppclass' not about creating extension class with  'cdef class'. I don't think you can define __bool__() at cppclass level.

--Leo
 

--

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