Carl Vondrick | 1 Feb 21:01 2011
Picon

Cython + Pickle: Can't pickle <type ...> it's not found as ...

I'm trying to pickle one of my Cython classes. It's defined like:

cdef class Box(object):
   def __init__(self, a):
      self.a = a

   def __reduce__(self):
       return (Box, (self.a,))

But, when I pickle this object, I get the error:

a = Box(1)
pickle.dump(a, open("foo", "w"))

I get the error: Can't pickle <type 'module.Box'>: it's not found as
module.Box

For what it's worth, the Box class is defined in an external module
installed via setuptools.

Any idea what's going on? As far as I can tell, the __reduce__ method
isn't even being called.

Thanks,

Lisandro Dalcin | 1 Feb 22:31 2011
Picon

Re: how to use Cython to use Python from C++

On 21 January 2011 15:47, Ondrej Certik <ondrej <at> certik.cz> wrote:
> Hi,
>
> previously my approach to this was to use Cython "api" directive and
> compile a .pyx into an .so, and then import it from C++ using
> import_my_module() api call, that Cython exports.
>
> This has one big problem, in fact a show stopper problem, that one
> needs to know the PYTHONPATH to this .so module at runtime, and I have
> not figured out how to solve it, thus causing quite a rejection of
> using Python from C++ in our group. In practice, people were
> constantly reporting, that the import of the .so fails (module not
> found).
>
> Couple days ago, I have figured out an excellent solution. So I wanted
> to share it with you, if there are any potential pitfalls. I compile
> the generated .cpp file into the C++ application directly, thus there
> is no .so generated.

Of course.

> Then, in the application, I initialize Python
> using C Python API, and call the "initmy_module()" function, that is
> always present in the genreated .cpp file. And then I simply import it
> in Python (that I execute using Python C/API calls) by "from my_module
> import some_stuff". In fact, in practice, I don't call any Python C /
> API directly, I use Cython for everything. Here is the code:
>
> https://github.com/hpfem/hermes/blob/master/hermes_common/python/python_api.cpp#L26
>
(Continue reading)

Lisandro Dalcin | 1 Feb 22:50 2011
Picon

Re: Accessing pyx globals from another pyx

On 30 January 2011 08:00, Robert Bradshaw <robertwb <at> math.washington.edu> wrote:
> On Sat, Jan 29, 2011 at 2:30 PM, Eli Stevens (Gmail)
>
>> Does anyone have any suggestions about what am I doing wrong?  I'm currently
>> using 0.13 under OSX (hoping the 14.1 will fix the issues on Windows so I
>> can upgrade).
>
> OS X to Windows is an upgrade?
>

I think the OP is using 0.13 in both OS X and Win because 0.14 have
issues in Windows.

--

-- 
Lisandro Dalcin
---------------
CIMEC (INTEC/CONICET-UNL)
Predio CONICET-Santa Fe
Colectora RN 168 Km 472, Paraje El Pozo
3000 Santa Fe, Argentina
Tel: +54-342-4511594 (ext 1011)
Tel/Fax: +54-342-4511169

Robert Bradshaw | 1 Feb 23:42 2011

Re: Accessing pyx globals from another pyx

On Tue, Feb 1, 2011 at 1:50 PM, Lisandro Dalcin <dalcinl <at> gmail.com> wrote:
> On 30 January 2011 08:00, Robert Bradshaw <robertwb <at> math.washington.edu> wrote:
>> On Sat, Jan 29, 2011 at 2:30 PM, Eli Stevens (Gmail)
>>
>>> Does anyone have any suggestions about what am I doing wrong?  I'm currently
>>> using 0.13 under OSX (hoping the 14.1 will fix the issues on Windows so I
>>> can upgrade).
>>
>> OS X to Windows is an upgrade?
>>
>
> I think the OP is using 0.13 in both OS X and Win because 0.14 have
> issues in Windows.

Ah.

FYI, the 0.14.1 release candidate is out and we'll be releasing soon.
Please  try it out to see if it does resolve your issues.

- Robert

Robert Bradshaw | 2 Feb 02:55 2011

Re: Cython + Pickle: Can't pickle <type ...> it's not found as ...

On Tue, Feb 1, 2011 at 12:01 PM, Carl Vondrick <cvondrick <at> gmail.com> wrote:
> I'm trying to pickle one of my Cython classes. It's defined like:
>
> cdef class Box(object):
>   def __init__(self, a):
>      self.a = a
>
>   def __reduce__(self):
>       return (Box, (self.a,))
>
> But, when I pickle this object, I get the error:
>
> a = Box(1)
> pickle.dump(a, open("foo", "w"))
>
> I get the error: Can't pickle <type 'module.Box'>: it's not found as
> module.Box
>
> For what it's worth, the Box class is defined in an external module
> installed via setuptools.
>
> Any idea what's going on? As far as I can tell, the __reduce__ method
> isn't even being called.

No idea, that works fine for me (well, inserting "cdef object a" into
Box's definition first).

- Robert

-------------------------------------------------------------------
(Continue reading)

Carl Vondrick | 2 Feb 04:50 2011
Picon

Re: Cython + Pickle: Can't pickle <type ...> it's not found as ...

It appears I had something funny going on with setuptools/distutils/
distribute/easy_install/pip. Moreover, my version control shows no
changes and it works now. :-)

On Feb 1, 5:55 pm, Robert Bradshaw <rober... <at> math.washington.edu>
wrote:
> On Tue, Feb 1, 2011 at 12:01 PM, Carl Vondrick <cvondr... <at> gmail.com> wrote:
> > I'm trying to pickle one of my Cython classes. It's defined like:
>
> > cdef class Box(object):
> >   def __init__(self, a):
> >      self.a = a
>
> >   def __reduce__(self):
> >       return (Box, (self.a,))
>
> > But, when I pickle this object, I get the error:
>
> > a = Box(1)
> > pickle.dump(a, open("foo", "w"))
>
> > I get the error: Can't pickle <type 'module.Box'>: it's not found as
> > module.Box
>
> > For what it's worth, the Box class is defined in an external module
> > installed via setuptools.
>
> > Any idea what's going on? As far as I can tell, the __reduce__ method
> > isn't even being called.
>
(Continue reading)

Robert Bradshaw | 2 Feb 11:32 2011

Re: Demos Makefile small bug / typo

Thanks for the report. Those are way out of date, perhaps they should
just be removed?

- Robert

On Mon, Jan 31, 2011 at 5:39 AM, Sebastian Haase <seb.haase <at> gmail.com> wrote:
> Hi.
>
> in Demos/Makefile:
>
> s/Setup/setup/
>
> -*- mode: compilation; default-directory: "/home/shaase/qq/cython/Demos/" -*-
> Compilation started at Mon Jan 31 14:35:00
>
> make -k
> python Setup.py build_ext --inplace
> python: can't open file 'Setup.py': [Errno 2] No such file or directory
>
>
>
> Demos/Makefile.nodistutils  should also be updated to mention
> something else than Python 2.2.
> e.g.
> change
>
> PYHOME = $(HOME)/pkg/python/version
> PYINCLUDE = \
>        -I$(PYHOME)/include/python2.2 \
>        -I$(PYHOME)/$(ARCH)/include/python2.2
(Continue reading)

Robert Bradshaw | 2 Feb 11:36 2011

Re: possible to check python version on import

On Mon, Jan 31, 2011 at 2:22 AM, Sebastian Haase <seb.haase <at> gmail.com> wrote:
> Hi,
>
> I am trying to do my first steps with cython.
> By accident I compiled with python2.6 while I'm otherwise using Python 2.5.
> So the error I got was:
>
>>>> primes.primes(4)
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
>  File "primes.pyx", line 3, in primes.primes (primes.c:443)
>    def primes(int kmax):
> TypeError: __int__ returned non-int (type int)
>
>
> Is there any way to check python versions of a module - at least to
> give a warning... ?

That's a good idea, can anyone think of a reason this should not be done?

http://trac.cython.org/cython_trac/ticket/657

> shaase <at> iris:~/qq/cython: cd Demos/
> shaase <at> iris:~/qq/cython/Demos: python setup.py build_ext --inplace
> Compiling primes.pyx because it changed.
> Compiling spam.pyx because it changed.
> Compiling numpy_demo.pyx because it changed.
> running build_ext
> building 'primes' extension
> C compiler: gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2
(Continue reading)

Jason | 2 Feb 19:30 2011
Picon

Simple Cython question regarding passing vectors to a constructor

Hi everyone,

I've got a C++ library that I'm trying to wrap using Cython. I've got
a basic question that I'm sure has been answered many times before,
but I can't seem to find a solution. Anyway, this is hopefully all of
the relevant code from my .pyx file:

from libcpp.vector cimport vector
...
ctypedef unsigned char WordLength
ctypedef unsigned long long int HashIntoType
...
cdef extern from "../lib/hashbits.hh" namespace "khmer":
   cdef cppclass Hashbits:
      Hashbits(WordLength, vector[HashIntoType])
...
cdef class new_hashbits:
   cdef Hashbits *thisptr
   def __cinit__(self, WordLength k, vector[HashIntoType] vec):
      self.thisptr = new Hashbits(k, vec)

When I try to compile, I get the following:

/home/u/jason/khmer/python2/_khmermodule.pyx:270:37: Cannot convert
Python object argument to type 'vector<HashIntoType>'

What do I need to do in order to get this working? Any help that you
can provide would be greatly appreciated.

Sincerely,
(Continue reading)

Robert Bradshaw | 2 Feb 20:25 2011

Re: Simple Cython question regarding passing vectors to a constructor

On Wed, Feb 2, 2011 at 10:30 AM, Jason <jason.pell <at> gmail.com> wrote:
> Hi everyone,
>
> I've got a C++ library that I'm trying to wrap using Cython. I've got
> a basic question that I'm sure has been answered many times before,
> but I can't seem to find a solution. Anyway, this is hopefully all of
> the relevant code from my .pyx file:
>
> from libcpp.vector cimport vector
> ...
> ctypedef unsigned char WordLength
> ctypedef unsigned long long int HashIntoType
> ...
> cdef extern from "../lib/hashbits.hh" namespace "khmer":
>   cdef cppclass Hashbits:
>      Hashbits(WordLength, vector[HashIntoType])
> ...
> cdef class new_hashbits:
>   cdef Hashbits *thisptr
>   def __cinit__(self, WordLength k, vector[HashIntoType] vec):
>      self.thisptr = new Hashbits(k, vec)
>
> When I try to compile, I get the following:
>
> /home/u/jason/khmer/python2/_khmermodule.pyx:270:37: Cannot convert
> Python object argument to type 'vector<HashIntoType>'
>
> What do I need to do in order to get this working? Any help that you
> can provide would be greatly appreciated.

(Continue reading)


Gmane