Julian Rüth | 28 Jul 11:10 2014
Picon

Unhashable cdef class

Hi,

in Python I can define a type which is not hashable by writing

class A(object):
    __hash__ = None

This has two effects:
a) hash(A()) raises a TypeError
b) isinstance(A(),collections.Hashable) returns False

My question is: Is there a way to have this behaviour for a cdef class?

Setting __hash__ = None seems to have no effect for a cdef class (in cython 0.20.1). I can get (a) by implementing __hash__ to raise a TypeError, but what about (b)?

Thanks,

julian

PS: I guess I could somehow set tp_hash to PyObject_HashNotImplemented (https://docs.python.org/2/c-api/typeobj.html#PyTypeObject.tp_hash) but I could not figure out how to do this.

--

---
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 | 26 Jul 12:48 2014
Picon

"Jedi typer" to find an initial set of type declarations

Hi,

meeting one of the authors of Jedi at EuroPython got me into writing a
little script that runs Jedi on a Python code file and injects static
Cython type declarations that Jedi infers for it. I attached two very
simple examples.

https://github.com/cython/cython/commit/4f015d5f7addaff9fcef80721087323379874cce

It's really just at a proof of concept level for now, but I hope that
someone out there finds it useful. The main purpose is as a one shot helper
script to provide an initial set of static types for a file that users can
build on.

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.
Attachment (numbench.py): text/x-python, 339 bytes
Attachment (numbench.py_typed.py): text/x-python, 454 bytes
stonebig34 | 23 Jul 22:30 2014
Picon

cython windows mingw compilers

Hello,

Sorry if the question has been asked recently.

For Python3.4 32 bit under windows (32bit or 64 bit) + what is the recommanded solution, today  :
- mingW (http://sourceforge.net/projects/mingw/files/) ?
- mingW64 in 32 bit mode (http://mingw-w64.sourceforge.net/) ?

My criterias are :
- stability first,
- performance on 32bit (and old computers) second,
- 64 bit third.

Thanks in advance for your advice

nota : I'm a cython newbye, not even sure mingW64 can work on 32 bit, so that choice exists

--

---
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.
Hamann, Michael (ITI | 23 Jul 10:10 2014

Containers of libc.stdint types cannot be converted from Python

Hi,

I have a problem using the integer definitions like uint64_t from 
libc.stdint as template arguments for c++ container types. When using 
something like vector[uint64_t] as return value, there is no problem. 
The vector is automatically converted to its Python equivalent. However 
when using the same type as argument value in a function, this 
conversion doesn't work. Instead (here for a set instead of a vector) I 
get an error message like

Error compiling Cython file:
------------------------------------------------------------
...
cdef extern from *:
     ctypedef uint64_t X 'uint64_t'
             ^
------------------------------------------------------------

set.from_py:110:13: 'uint64_t' is not a type identifier

The exactly same code works when using "unsigned long" instead of 
"uint64_t".

For a concrete example refer to

https://algohub.iti.kit.edu/parco/NetworKit/NetworKit/files/7b8e87d3688afc9a225185206000dcf91a6354ad/src/python/_NetworKit.pyx#L808 
- we would like to use "uint64_t" (or rather a ctypedef that refers to 
uint64_t) as argument here. The problem persists when the conversion is 
done explicitly in the cdef class (using <node[uint64_t]> 
degreeSequence) and also when the argument type is already declared in 
the method of the cdef class.

A similar problem was already mentioned 8 months ago in this topic: 
https://groups.google.com/forum/#!topic/cython-users/CFlfbOB-wjM

Is this a bug in Cython or is there anything we can do in order to avoid 
this problem? I am using Cython version 0.20.2.

Thank you!

Michael

--

-- 

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

Emil Lerner | 21 Jul 20:35 2014
Picon

ImportError while trying to use "cdef cppclass" in another extension

Hi!

I'm trying to import a cpp class in one extension and then use it in other extension.

I have two extensions, namely ext1 and ext2. I've declared a cpp class MyClass in a h file in ext1, and declared it as cdef cppclass in ext1.pxd . I've checked that I'm able to use it in ext1. Now I've copied ext1.so, ext1.pxd and myclass.h to ext2 folder. But when I'm trying to use MyClass in ext2, it compiles ok, but raises ImportError when importing ext2 in python.

So, I have two folders, ext1/ and ext2/, and files is following:

ext1/myclass.h:
class MyClass {
   
public:
       
int b;
       
MyClass(int b);
       
int sum(int a);
};



ext1/myclass.cpp:
#include "myclass.h"
MyClass::MyClass(int b) {
   
this->b = b;
}

int MyClass::sum(int a) {
   
return a + this->b;
}



ext1/ext1.pxd:
cdef extern from "myclass.h":
    cdef cppclass
MyClass:
       
MyClass(int)
       
int sum(int)



ext1/ext1.pyx:
def test1():
    cdef
:
       
MyClass* x = new MyClass(2)
   
print x.sum(2)



ext1/setup.py:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
from Cython.Distutils import build_ext

setup
(
    ext_modules
= cythonize([Extension("*", ["ext1.pyx", "myclass.cpp"], language="c++")]),
)


ext2/ext1.pxd is the same as ext1/ext1.pxd
ext2/myclass.h is the same as ext1/myclass.h
ext2/ext1.so is compiled ext1.so
(actually, previous 3 files are symlinks)

ext2/ext2.pxd:
from ext1 cimport MyClass

ext2/ext2.pyx:
def test2():
    cdef
:
       
MyClass* x = new MyClass(2)
   
print x.sum(2)


ext2/setup.py:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
from Cython.Distutils import build_ext

setup
(
    ext_modules
= cythonize([
   
Extension("*", ["ext2.pyx"], language="c++")
   
]),
)


And when I use ext1, it is all ok:
$ python -c 'import ext1; ext1.test1();'
4


And when I use ext2, It doesn't work:
$ python -c 'import ext2; ext2.test1();'
Traceback (most recent call last):
 
File "<string>", line 1, in <module>
ImportError: ./ext2.so: undefined symbol: _ZN7MyClass3sumEi

What I'm doing wrong? I don't want to use wrapper extension class as I'm going to have very large arrays of the classes, so I need to be memory efficient.


--

---
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.
Grzegorz Kowzan | 21 Jul 14:13 2014
Picon

winsock2.h include

Hello,

I have a C++ library which I'd like to work both on Windows and on Linux. The only significant difference between these platforms is location of ntoh procedure, for conversion between big endian and little endian number format, so in my header file I have following code:

#ifdef WIN32
#include <winsock2.h>
#elif LINUX
#include <netinet/in.h>
#endif

For building this library with cython, I have created a distutils setup file, where relevant part is:

    sourcefiles = ["libvipa.pyx",
                   r"<Path to file>\libVIPA.cpp"]
    ext_modules = [
    Extension("libvipa", sourcefiles,
              include_dirs=[r'<path to directory>\vipadll', np.get_include()],
              define_macros=[('WIN32', 1), ('VIPA_EXPORTS', 1)],
              libraries=['ws2_32'],
              extra_compile_args=['-DWIN32'],
              language='c++')]
    setup(ext_modules = cythonize(ext_modules, define_macros=[('WIN32', 1)]))

Unfortunately, cython insist of placing a conditional block of
#ifdef WIN32
...
#include "netinet/in.h"
...
#endif
so everything works fine on Linux, but on Windows it obviously doesn't compile. Should I configure my build system somewhat differently or is this a bug in cython?

Grzegorz

--

---
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.
Jerome Kieffer | 21 Jul 10:53 2014

MacOSX issue

Dear Cython users,

I guess I am not the only one facing some issue with the Clang compiler
provided by default in MacOSX due to the lack of support of OpenMP.

My code fails stupidly with "omp.h" not found.

What solution do you have to "work around" ?
- enforce the use of another compiler ?
- provide a different branch without cython.parallel.prange ?

Do you have another solution because none of them look satisfactory to me.

Cheers,

-- 
Jerome Kieffer <google <at> terre-adelie.org>

--

-- 

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

Kurt Smith | 20 Jul 22:38 2014
Picon

cygdb -- still maintained?

Hello,

Is Cython's gdb support still maintained?  I'm trying to get a simple demo going, and I get (cython) compile-time exeptions.  If I remove the `gdb_debug=True` from `cythonize()`, everything compiles and works fine.

For example:

$ cat setup.py
from distutils.core import setup
from Cython.Build import cythonize

setup(name="debug_me",
      ext_modules=cythonize("debug_me.pyx",
                            gdb_debug=True)
     )

$ python setup.py build_ext -i
Compiling debug_me.pyx because it changed.
Cythonizing debug_me.pyx
Traceback (most recent call last):
  File "setup.py", line 4, in <module>
    setup(name="debug_me", ext_modules=cythonize("debug_me.pyx", gdb_debug=True))
  File "/Users/ksmith/Devel/cython/Cython/Build/Dependencies.py", line 812, in cythonize
    cythonize_one(*args[1:])
  File "/Users/ksmith/Devel/cython/Cython/Build/Dependencies.py", line 914, in cythonize_one
    result = compile([pyx_file], options)
  File "/Users/ksmith/Devel/cython/Cython/Compiler/Main.py", line 624, in compile
    return compile_multiple(source, options)
  File "/Users/ksmith/Devel/cython/Cython/Compiler/Main.py", line 602, in compile_multiple
    result = run_pipeline(source, options, context=context)
  File "/Users/ksmith/Devel/cython/Cython/Compiler/Main.py", line 436, in run_pipeline
    pipeline = Pipeline.create_pyx_pipeline(context, options, result)
  File "/Users/ksmith/Devel/cython/Cython/Compiler/Pipeline.py", line 231, in create_pyx_pipeline
    options.output_dir)
  File "/Users/ksmith/Devel/cython/Cython/Debugger/DebugWriter.py", line 45, in __init__
    self.output_dir = os.path.join(output_dir, 'cython_debug')
  File "/Users/ksmith/Devel/virtualenvs/venv-py2.7-cython-dev/bin/../lib/python2.7/posixpath.py", line 68, in join
    elif path == '' or path.endswith('/'):
AttributeError: 'NoneType' object has no attribute 'endswith'

--

---
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 | 17 Jul 00:25 2014
Picon

Dave Beazley is messing with my brain (again)

Does Cython support this?

https://twitter.com/dabeaz/status/489528443181088768

>>> x = float("nan")
>>> x == x
False
>>> [x] == [x]
True
>>>

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.

Irwin Zaid | 14 Jul 00:17 2014
Picon

Add dynamic_cast to libcpp?

Hi all,

I just have a small suggestion for an improvement to Cython.

Sometimes the only way to downcast through an inheritance hierarchy is with dynamic_cast. While dynamic_cast isn't available in Cython automatically, it does work simply by doing:

cdef extern from *:
    cdef T dynamic_cast[T](void *) except +

Why don't we add this to Includes/libcpp/__init__.pxd, or some other appropriate place? It is useful just to have it around, as it can be necessary sometimes. We could also add the other casts (static, const, and reinterpret) analogously, although I'm not sure if they have any real use in Cython.

Making dynamic_cast available like this also means anyone else that needs it doesn't need to sort out how to do it.

Cheers,

Irwin

--

---
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.
Valcortez | 13 Jul 18:13 2014
Picon

memoryview leak and bytes object leak with master

Hi,

I've discovered memory leaks in two different circumstances, one involves creating a bytes object in cython, the other involves a memoryview. The test code below demonstrates the issue.

pip show cython
---
Name: Cython
Version: 0.21dev

with python 2.7


in cyplayground.pyx:

from cpython.ref cimport PyObject
from cpython.array cimport array, clone

cdef extern from "Python.h":
    int PyObject_IsTrue(PyObject *)
    PyObject* PyString_FromString(const char *)
    void Py_DECREF(PyObject *)


def get_str(bytes val, dec):
    cdef object buff
    buff = <object>PyString_FromString(val)
    if dec:
        Py_DECREF(<PyObject *>buff)
    return buff


def get_array(int arr_size, dec):
    cdef array ret_array
    cdef char [:] char_view

    ret_array = clone(array('b'), arr_size, True)
    char_view = ret_array
    if dec:
        Py_DECREF(<PyObject *>ret_array)
    return ret_array
 

in playground.py:

from cyplayground import get_array, get_str
import gc
import psutil


def get_mem():
    return psutil.virtual_memory().used / 1024 ** 2

size = 100 * 1024 ** 2
before = get_mem()
get_str('4' * size, True)
gc.collect()

mid = get_mem()
get_str('4' * size, False)
gc.collect()
after = get_mem()

print('string: w/ dec: {}, w/o dec: {}'.format(mid - before, after - mid))


size = 100 * 1024 ** 2
before = get_mem()
get_array(size, True)
gc.collect()

mid = get_mem()
get_array(size, False)
gc.collect()
after = get_mem()

print('array: w/ dec: {}, w/o dec: {}'.format(mid - before, after - mid))

Running it prints:

string: w/ dec: 4, w/o dec: 100
array: w/ dec: 0, w/o dec: 100
 
As you can see, in both situations, unless I manually call Py_DECREF, the objects are leaked.

Thanks

--

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