Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'preferably': 0.03; 'python': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'tuple': 0.09; 'written': 0.12; '>>>': 0.12; 'extension': 0.14; 'wrote:': 0.14; '"can\'t': 0.16; '(1,': 0.16; '70,': 0.16; 'called,': 0.16; 'class:': 0.16; 'grep': 0.16; 'later:': 0.16; 'pyobject': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'self)': 0.16; 'static': 0.16; 'traceback': 0.16; '(most': 0.16; 'greatly': 0.20; 'seems': 0.21; "haven't": 0.23; 'last):': 0.23; 'objects': 0.24; 'define': 0.26; 'specifically': 0.27; 'raise': 0.29; 'problem': 0.29; 'skip:" 30': 0.29; 'stefan': 0.29; 'from:addr:web.de': 0.31; 'typeerror:': 0.31; 'does': 0.31; "can't": 0.31; 'to:addr:python-list': 0.32; 'source': 0.32; 'uses': 0.34; 'header:X-Complaints-To:1': 0.34; 'file': 0.35; 'returned': 0.35; '"",': 0.35; 'appreciated.': 0.36; 'should': 0.37; 'but': 0.38; 'received:org': 0.38; 'docs': 0.39; 'ok,': 0.39; 'to:addr:python.org': 0.39; 'header:Mime-Version:1': 0.39; 'would': 0.40; 'header:Received:5': 0.40; 'simple': 0.60; 'skip:1 10': 0.62; 'art': 0.62; 'else...': 0.84; 'subject:types': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Pickling extension types Date: Tue, 03 May 2011 19:39:27 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084a174.dip.t-dialin.net X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 40 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1304444368 news.xs4all.nl 81473 [::ffff:82.94.164.166]:56431 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:4562 Stefan Kuzminski wrote: > I have an extension type written in C, but I cannot get it to pickle, any > insights would be greatly appreciated. > > I see in the docs that I should define a __reduce__ method and that does > get called, but I don't know specifically the type of the 'callable > object' that should be the first thing in the tuple returned by > __reduce__. I haven't written a python extension in C, but I would approach the problem by looking for prior art in the stdlib, preferably a simple class: >>> 1j.__reduce__() Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.6/copy_reg.py", line 70, in _reduce_ex raise TypeError, "can't pickle %s objects" % base.__name__ TypeError: can't pickle complex objects OK, complex does something else... * One grep through the source later: >>> slice(1,2,3).__reduce__() (, (1, 2, 3)) And the implementation seems straightforward: static PyObject * slice_reduce(PySliceObject* self) { return Py_BuildValue("O(OOO)", Py_TYPE(self), self->start, self->stop, self->step); } (*) it uses __getnewargs__