Yuxiang Wang | 6 Jun 17:34 2014
Picon

Cython: use pyximport in Win7 64 bit with MSVC

In this wiki (https://github.com/cython/cython/wiki/64BitCythonExtensionsOnWindows) it very clearly explained that we need to run the following command to use `distutils` to use Cython in Win 64 bits:

    set DISTUTILS_USE_SDK=1
    setenv /x64 /release

This works with the setup.py method perfectly. However, I was wondering whether there is a way to use `pyximport` as well. I tried to run this in Python before I import pyximport, but it didn't work:

    import sys
    import os
    if sys.version_info.major == 3:
        os.system('set DISTUTILS_USE_SDK=1')
        os.system('C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.Cmd /x64 /release')

Any ideas?

--

---
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.
1989lzhh | 6 Jun 12:23 2014
Picon

Ideas about create numba.jit like decoration emit cython code as a backend

Recently, I learned there is a decoration function named cython.compile can compile the decorated
function on fly. 
It just like numba.jit except it can not set function signature and local variable type and call other
compiled function quickly.
In fact, it is possible to create a numba.jit like function using cython as backend. Now I am working on
prototype. I want the jitted function can be called by other jitted function quickly. For now what come up
in my mind is like this:
 <at> jit # haven't think up a good name, the decorate will collect function source and change the function def to
cdef and write to pyx file
def f1():
    pass
 <at> jit
def f2():
    f1()
build()# here compile, put compiled function into global scope. 
The advantage of this approach is that the extension is only needed to import once, and the implementation
is relative simple. It don't need to scan the code. The disadvantage is that it need to compile the whole
source although only one function is changed.
So any suggestion? How to fast call a jitted function imported from other module?

Ps: apologize for my pool English ~ 

                                   Liu Zhenjiang

--

-- 

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

Noob Saibot | 5 Jun 19:53 2014
Picon

When and How does cython create cdef classes?

#foo.pyx

cdef 
class Foo(object):
    
pass

When cython parses this .pyx file, how is the Foo class then created for use in python? I assume it generates C-API code like this for the classes, but does cython include it's own magic spells in-between? Where can i see this process in the source?

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.
Brian Knapp | 5 Jun 02:53 2014
Picon

Micropython Header for Compiling Cython Modules

The author has some strong opinions regarding exception handling and appropriateness, but I believe that getting Cython working with Micropython has a large value to application libraries like Kivy and in addition provides us with a perhaps workable path to having a Cpython alternative implementation that isn't someone else's VM.  I've subscribed to reply emails, but don't have much to say besides calling attention to how developing Micropython headers might help the community.

https://github.com/micropython/micropython/issues/658

--

---
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.
Noob Saibot | 3 Jun 20:05 2014
Picon

How do i use external extension types in cython to cdef metaclasses?

Note: This is a re-write of a question i had posted earlier. I deleted it because have since simplified what it was i was trying to do.

I am trying to make cdefined cython classes that inherit from a cdefined metatype. Like so:

cdef class MetaType(type):
    
def __init__(self, *etc):
        
super(MetaType, self).__init__(*etc)
        do_something
()

cdef 
class A = MetaType('A', (Whatever,), {})

cdef 
class B(A):
    
pass

...but since cython 0.20 only supports metatyping python classes, i thought of making the MetaType class in Python's C-API...

/* metatypeobject.h */
typedef struct {
    PyObject_HEAD
} MetaTypeObject;

static PyTypeObject MetaTypeType = {
    PyObject_HEAD_INIT(NULL)
    ...
    &PyType_Type, /* tp_base */
    my_type_init, /* tp_init */
};

...then extern from what i need in cython:

cdef extern from "Python.h":
    pass
cdef extern from "metatypeobject.h":
    ctypedef 
class __builtin__.type [object MetaTypeObject, type MetaTypeType]:
        pass

cdef 
class A = MetaTypeObject('A', (Whatever,), {})

But i can't get the syntax right. The compiler doesn't seem to want to recognize A as a type of any kind--let alone class. Can anyone please help me discern what exactly is it is the compiler wants?

--

---
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.
Uwe Schmitt | 2 Jun 15:18 2014
Picon

Issue with clang compiler

Dear all,

although commit https://github.com/cython/cython/commit/d360c927d8c2c7027e54b5e600d337f936b3d155 fixed some issues with
the current clang compiler, the C++ code generated from

from cython.operator cimport dereference as deref, preincrement as preinc
from libcpp.list cimport list as cpplist


cdef class IterTest:

    cdef cpplist[int] * inst

    def __iter__(self):
        assert self.inst != NULL
        it = self.inst.begin()
        while it != self.inst.end():
            yield <int> deref(it)
            preinc(it)

 can not be compiled with clang. The generated code fragment which causes the problem is

static void __pyx_tp_dealloc_8itertest___pyx_scope_struct____iter__(PyObject *o) {
   struct __pyx_obj_8itertest___pyx_scope_struct____iter__ *p = (struct __pyx_obj_8itertest___pyx_scope_struct____iter__ *)o;
   PyObject_GC_UnTrack(o);
   p->__pyx_v_it.std::list<int>::iterator::~iterator();
   Py_CLEAR(p->__pyx_v_self);
   if ((__pyx_freecount_8itertest___pyx_scope_struct____iter__ < 8) & (Py_TYPE(o)->tp_basicsize == sizeof(struct                                                                     __pyx_obj_8itertest___pyx_scope_struct____iter__))) {
     __pyx_freelist_8itertest___pyx_scope_struct____iter__[__pyx_freecount_8itertest___pyx_scope_struct____iter__++] = ((struct                                                     __pyx_obj_8itertest___pyx_scope_struct____iter__ *)o);
   } else {
     (*Py_TYPE(o)->tp_free)(o);
   }
}

clang reports for the highlighted line:

itertest.cpp:1034:44: error: expected the class name after '~' to name a destructor
  p->__pyx_v_it.std::list<int>::iterator::~iterator();
                                           ^ 
This looks somehow similar to the bug which the commit I mentioned above tries to fix.

Kind Regards,

Uwe
 

--

---
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 Liebkies | 31 May 16:21 2014
Picon

Wrapping a Tree structure: Refcounting and GC issues

Hi everyone,

In this specific case I'm not trying to generalize my problem, as I'm not sure if this would result in information loss. So, what I'm trying to wrap is red black tree (simple tree, basically) from the ldns library. For the original source:

    .h: http://git.nlnetlabs.nl/ldns/tree/ldns/rbtree.h
    .c: http://git.nlnetlabs.nl/ldns/tree/rbtree.c

My .pxd files doesn't contain any really important stuff, I'm using the structs as they are defined in the rbtree.h, explicitly using members. My cdef classes look like this:

cdef class Node:
    def __cinit__(self, *args, **kwargs):
        self._node = <ldns_rbnode_t*>malloc(sizeof(ldns_rbnode_t))
        self._node.parent = NULL
        self._node.left = NULL
        self._node.right = NULL
        self._node.data = NULL
        self._node.key = NULL
        self._node.color = Node.BLACK

    def __dealloc__(self):
        # No dealloc. Doesn't work at the moment.
        pass

cdef class RedBlackTree:
    def __cinit__(self, *args, **kwargs):
        self._tree = <ldns_rbtree_t*>malloc(sizeof(ldns_rbtree_t))
        if self._tree is NULL:
            raise MemoryError()

    def __init__(self, cmpfunc, *args, **kwargs):
        if not hasattr(cmpfunc, '__call__'):
            raise TypeError("Expected callable")

        global _CMPFUNC
        _CMPFUNC = cmpfunc
        ldns_rbtree_init(self._tree, &callback_cmp)

    def __dealloc__(self):
        ldns_rbtree_free(self._tree)

    def insert(self, Node node):
        cdef ldns_rbnode_t* ret = ldns_rbtree_insert(self._tree, node._node)

        if ret == NULL:
            raise RedBlackTreeError("Node already present in tree.")

        return Node_create(ret)

Then there's my callback function mapping:
# The Python function passed in by the user to be used in the cdef'ed callback
# just right below.
_CMPFUNC = None
cdef int callback_cmp(const void* key1, const void* key2):
    global _CMPFUNC
    if _CMPFUNC is None:
        raise RedBlackTree("The compare function for the tree may not be None")

    cdef char* _key1 = <char*>key1
    cdef char* _key2 = <char*>key2

    # get the result of the python function for the comparison.
    ret = _CMPFUNC(<bytes?>_key1, <bytes?>_key2)

    return <int?>ret

Up to this point, everything kind of works. I can insert nodes, delete them, traverse the tree etc, but _only_ up to a certain amount of nodes. For example take the following function to create a linear tree:

def linear_tree(count):
    tree = RedBlackTree(cmp)
    for i in range(count):
        tree.insert(Node(key=str(i), data=str(count)))

    return tree

Calling linear_tree(10) works great in my case, no errors no segfaults

linear_tree(40) on the other hand either segfaults (frame 0: Py_ForgetReference) or just seems to be "forgetting" nodes. Meaning: at some point the root node/any other node just vanishes, and the rbtree C implementation replaces it with some other node on the next insert() call in linear_tree. The behaviour is completely random and leaves me with a garbage tree.
I tried adding a dict in RedBlackTree to hold all Nodes (as references of some sort) but then I get a SIGABRT with "Fatal Python error: Objects/dictobject.c:1009 object at 0xc11240 has negative ref count -1"

I'm guessing this has something to do with the way Python does Refcounting and garbage collection (it seems to think the Nodes aren't needed anymore and tries to gc them).

How would I solve this problem? 

Thanks in advance and cheers,

Max

--

---
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.
Noob Saibot | 1 Jun 05:07 2014
Picon

Python C-API Metaclass + Cython = "Variable type `PyTypeObject` is Incomplete"

I'm tying to make cdef-metatyped classes for use in cython. Since cython 0.20 currently doesn't support them, i thought of building the metaclass from scratch using python's C-API, including it in cython, and proceeding as normal; but everytime i try to compile, it fails with many errors. I believe the principle culprit is a Variable type `PyTypeObject` is Incomplete error.

Attached is what i came up with (It's not long at all). What am i missing? ANY help would be greatly appreciated.

Machine: 64-Bit Windows
Python Version: 2.7
Cython Version: 0.20

--

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

C:\Python27\Lib\site-packages\kivy\uix>python setup.py build_ext --inplace
Compiling widget.pyx because it changed.
Cythonizing widget.pyx

Error compiling Cython file:
------------------------------------------------------------
...

cdef extern from "_metakivy.h":
    object initmetaclass(object type, object op, object subtype, object args, ob
ject kwargs, object factory)
    ctypedef extern struct KvTypeType:
        pass
    cdef extern PyTypeObject KvType_Type
                            ^
------------------------------------------------------------

widget.pxd:11:29: Variable type 'PyTypeObject' is incomplete

Error compiling Cython file:
------------------------------------------------------------
...
        pass
    cdef extern PyTypeObject KvType_Type

cdef object WidgetBase = initmetaclass(KvTypeType, KvType_Type, <PyTypeObject *>
EventDispatcher, NULL, NULL, Factory)

cdef class Widget(WidgetBase):
    ^
------------------------------------------------------------

widget.pxd:15:5: 'WidgetBase' is not a type name

Error compiling Cython file:
------------------------------------------------------------
...
    object initmetaclass(object type, object op, object subtype, object args, ob
ject kwargs, object factory)
    ctypedef extern struct KvTypeType:
        pass
    cdef extern PyTypeObject KvType_Type

cdef object WidgetBase = initmetaclass(KvTypeType, KvType_Type, <PyTypeObject *>
EventDispatcher, NULL, NULL, Factory)
                                                ^
------------------------------------------------------------

widget.pxd:13:49: 'KvTypeType' is not a constant, variable or function identifie
r
Compiler crash in AnalyseExpressionsTransform

File 'Nodes.py', line 388, in analyse_expressions: StatListNode(widget.pxd:1:0)
File 'Nodes.py', line 4511, in analyse_expressions: SingleAssignmentNode(widget.
pxd:13:5)
File 'Nodes.py', line 4616, in analyse_types: SingleAssignmentNode(widget.pxd:13
:5)
File 'ExprNodes.py', line 4416, in analyse_types: SimpleCallNode(widget.pxd:13:3
8,
    analysed = True,
    result_is_used = True,
    use_managed_ref = True)
File 'ExprNodes.py', line 4522, in analyse_c_function_call: SimpleCallNode(widge
t.pxd:13:38,
    analysed = True,
    result_is_used = True,
    use_managed_ref = True)
File 'ExprNodes.py', line 1622, in coerce_to: NameNode(widget.pxd:13:62,
    cf_maybe_null = True,
    initialized_check = True,
    is_name = True,
    name = u'KvType_Type',
    result_is_used = True,
    use_managed_ref = True)
File 'ExprNodes.py', line 755, in coerce_to: NameNode(widget.pxd:13:62,
    cf_maybe_null = True,
    initialized_check = True,
    is_name = True,
    name = u'KvType_Type',
    result_is_used = True,
    use_managed_ref = True)
File 'ExprNodes.py', line 10681, in __init__: CoerceToPyTypeNode(is_temp = 1,
    result_is_used = True,
    use_managed_ref = True)

Compiler crash traceback from this point on:
  File "C:\Python27\lib\site-packages\Cython\Compiler\ExprNodes.py", line 10681,
 in __init__
    if not arg.type.create_to_py_utility_code(env):
  File "C:\Python27\lib\site-packages\Cython\Compiler\PyrexTypes.py", line 2881,
 in create_to_py_utility_code
    for member in self.scope.var_entries:
AttributeError: 'NoneType' object has no attribute 'var_entries'

Error compiling Cython file:
------------------------------------------------------------
...
cdef class Widget(WidgetBase):
    ^
------------------------------------------------------------

widget.pyx:1:5: 'WidgetBase' is not a type name
Traceback (most recent call last):
  File "setup.py", line 4, in <module>
    ext_modules = cythonize('widget.pyx'),)
  File "C:\Python27\lib\site-packages\Cython\Build\Dependencies.py", line 785, i
n cythonize
    cythonize_one(*args[1:])
  File "C:\Python27\lib\site-packages\Cython\Build\Dependencies.py", line 902, i
n cythonize_one
    raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: widget.pyx

C:\Python27\Lib\site-packages\kivy\uix>
Attachment (_metakivy.c): text/x-csrc, 2658 bytes
Attachment (_metakivy.h): text/x-chdr, 355 bytes
Attachment (widget.pxd): application/octet-stream, 561 bytes
Attachment (widget.pyx): application/octet-stream, 40 bytes
刘振海 | 30 May 15:32 2014
Picon

directives like wraparound, boundscheck etc is not supported in pure python model

here is the code:
test.py
def f(arr):
  return arr[0]

test.pxd
import cython
 <at> cython.wraparound(False)
 <at> cython.boundscheck(False)
cdef f(double[:,::1] arr) 

I try to fix this, but I get no cule.
this can be solved by using cmd options like cython -X wraparound=False to overcome.

--

-- 

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

Robert Bradshaw | 30 May 03:06 2014
Picon

Cython 0.20.2 beta

I've pushed a beta for the 0.20.2 can be found at
http://cython.org/release/Cython-0.20rc1.tar.gz . This is a
bugfix-only release, more development is going on for 0.21 which will
also be released soon. Note, however, that the 0.20.x branch is the
last to support older versions of Python (pre 2.6 or 3.2).

Please try it out and report back.

- Robert

Features added

Some optimisations for set/frozenset instantiation.
Support for C++ unordered_set and unordered_map.

Bugs fixed

Access to attributes of optimised builtin methods (e.g.
[].append.__name__) could fail to compile.
Memory leak when extension subtypes add a memory view as attribute to
those of the parent type without having Python object attributes or a
user provided dealloc method.
Compiler crash on readonly properties in "binding" mode.
Auto-encoding with c_string_encoding=ascii failed in Py3.3.
Crash when subtyping freelist enabled Cython extension types with
Python classes that use __slots__.
Freelist usage is restricted to CPython to avoid problems with other
Python implementations.
Memory leak in memory views when copying overlapping, contiguous slices.
Format checking when requesting non-contiguous buffers from
cython.array objects was disabled in Py3.
C++ destructor calls in extension types could fail to compile in clang.
Buffer format validation failed for sequences of strings in structs.
Docstrings on extension type attributes in .pxd files were rejected.

--

-- 

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

Yuxiang Wang | 29 May 18:45 2014
Picon

Use Cython with OpenMP in Windows

Dear all,

I have a question: how do I use Cython with OpenMP in Windows?

According to this link https://github.com/cython/cython/wiki/64BitCythonExtensionsOnWindows   I would be better off using MSVC. However, with VC++ 2010 Express Edition, OpenMP support is not included. I also tried to install Windows SDK, but the same error (no omp.h) pops up again.

I don't think I am the first one who ran into this... Could anyone give me a hint?

Thank you!

Shawn

--

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