Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #70475

Re: Recommended exception for objects that can't be pickled

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; '"""': 0.07; 'explicit': 0.07; 'skip:` 10': 0.07; 'socket': 0.07; 'string': 0.09; 'defines': 0.09; 'exception.': 0.09; 'indeed,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subtle': 0.09; 'to)': 0.09; 'python': 0.11; 'def': 0.12; 'bug': 0.12; 'jan': 0.12; '%r"': 0.16; '[2].': 0.16; 'grep': 0.16; 'hint': 0.16; 'implies': 0.16; 'importerror,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'schwarzer': 0.16; 'skip:/ 60': 0.16; 'subject:exception': 0.16; 'typeerror:': 0.16; 'violation.': 0.16; 'x()': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'library': 0.18; 'all,': 0.19; 'bit': 0.19; 'module': 0.19; 'ticket': 0.19; 'trying': 0.19; '(but': 0.19; 'stefan': 0.19; 'seems': 0.21; '>>>': 0.22; 'code,': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; "aren't": 0.24; 'question': 0.24; 'class.': 0.26; 'possibly': 0.26; 'skip:" 30': 0.26; 'skip:_ 20': 0.27; 'gets': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'idea': 0.28; 'skip:p 30': 0.29; '[1]': 0.29; 'am,': 0.29; 'raise': 0.29; '[2]': 0.30; 'returned': 0.30; 'skip:( 20': 0.30; 'gives': 0.31; 'code': 0.31; "skip:' 10": 0.31; 'usually': 0.31; '"",': 0.31; '(my': 0.31; 'exceptions': 0.31; 'explained': 0.31; 'object.': 0.31; 'pickle': 0.31; 'raised': 0.31; 'subject:that': 0.31; 'file': 0.32; 'class': 0.32; 'there.': 0.32; 'linux': 0.33; 'url:python': 0.33; '(most': 0.33; 'bugs': 0.33; 'skip:_ 10': 0.34; 'message.': 0.35; 'problem': 0.35; "can't": 0.35; 'common': 0.35; 'except': 0.35; 'objects': 0.35; 'usual': 0.35; 'there': 0.35; 'instances': 0.36; 'library.': 0.36; 'object,': 0.36; 'words,': 0.36; "didn't": 0.36; 'method': 0.36; 'web,': 0.36; 'hi,': 0.36; 'url:org': 0.36; 'should': 0.36; 'hat': 0.38; 'nov': 0.38; 'url:library': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'recent': 0.39; 'bad': 0.39; 'subject:can': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'called': 0.40; 'users': 0.40; 'even': 0.60; 'read': 0.60; 'most': 0.60; 'url:3': 0.61; 'received:173': 0.61; 'such': 0.63; 'refer': 0.63; 'more': 0.64; 'due': 0.66; 'url:4': 0.69; 'below.': 0.71; 'object:': 0.84; 'own)': 0.84; 'pickled': 0.84; 'received:fios.verizon.net': 0.84; 'skip:/ 30': 0.84; 'url:ticket': 0.84; 'url:trac': 0.84; '2013,': 0.91; 'mistake': 0.91; 'why?': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Recommended exception for objects that can't be pickled
Date Mon, 21 Apr 2014 17:08:17 -0400
References <53551BBA.8000205@sschwarzer.net>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-254-207.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0
In-Reply-To <53551BBA.8000205@sschwarzer.net>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.9418.1398114540.18130.python-list@python.org> (permalink)
Lines 132
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1398114540 news.xs4all.nl 2867 [2001:888:2000:d::a6]:33436
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:70475

Show key headers only | View raw


On 4/21/2014 9:23 AM, Stefan Schwarzer wrote:
> Hi,
>
> Recently, I got a request [1] to support pickling of
> `FTPHost` instances in my `ftplib` library.
>
> I explained in the ticket why I think it's a bad idea and
> now want to make explicit that `FTPHost` objects can't be
> pickled. The usual way to do this seems to be defining a
> `__getstate__` method and raise an exception there.
>
> Now the question is "which exception?" and my research left
> me a bit confused. I didn't find a recommendation for this
> on the web, not even in the Python documentation for the
> `pickle` module [2]. The only hint is that the documentation
> states:
>
> """
>    The pickle module defines three exceptions:
>
>    exception pickle.PickleError
>
>        Common base class for the other pickling exceptions. It
>        inherits Exception.
>
>    exception pickle.PicklingError
>
>        Error raised when an unpicklable object is encountered
>        by Pickler. It inherits PickleError.

I am going to read this as 'unpicklable as determined by Pickler', 
possibly due to a bug in the objects methods.

>        Refer to What can be pickled and unpickled? to learn
>        what kinds of objects can be pickled.
>
>    exception pickle.UnpicklingError
>
>        Error raised when there is a problem unpickling an
>        object, such as a data corruption or a security
>        violation. It inherits PickleError.
>
>        Note that other exceptions may also be raised during
>        unpickling, including (but not necessarily limited to)
>        AttributeError, EOFError, ImportError, and IndexError.
> """
>
> This sounds like unpicklable objects should raise a
> `PicklingError`. Indeed, if I do this, `pickle.dumps`
> gives me (my own) `PicklingError`:
>
>    Python 3.3.2 (default, Nov  8 2013, 13:38:57)
>    [GCC 4.8.2 20131017 (Red Hat 4.8.2-1)] on linux
>    Type "help", "copyright", "credits" or "license" for more information.
>    >>> import pickle
>    >>> class X:
>    ...   def __getstate__(self):
>    ...     raise pickle.PicklingError("can't pickle X objects")
>    ...
>    >>> x = X()
>    >>> pickle.dumps(x)
>    Traceback (most recent call last):
>      File "<stdin>", line 1, in <module>
>      File "<stdin>", line 3, in __getstate__
>    _pickle.PicklingError: can't pickle X objects
>
> What now confuses me is that most, if not all, objects from
> the standard library that aren't picklable raise a
> `TypeError` when I try to pickle them:
>
>    >>> fobj = open("/etc/passwd")
>    >>> pickle.dumps(fobj)
>    Traceback (most recent call last):
>      File "<stdin>", line 1, in <module>
>    TypeError: cannot serialize '_io.TextIOWrapper' object
>
>    >>> import socket
>    >>> s = socket.socket()
>    >>> pickle.dumps(s)
>    Traceback (most recent call last):
>      File "<stdin>", line 1, in <module>
>      File "/usr/lib64/python3.3/socket.py", line 116, in __getstate__
>        raise TypeError("Cannot serialize socket object")
>    TypeError: Cannot serialize socket object

I would just copy the socket.__getstate__, with a revised message.

> So the documentation for the `pickle` module (to me) implies
> I should raise a `PicklingError` while the standard library
> usually seems to use a `TypeError`. When I grep through the
> library files for `PicklingError`, I get very few hits, most
> of them in `pickle.py`:

This suggests that the exception is intended for use by Pickler, and not 
by user code. The reason is pretty subtle and not explained in the doc. 
See below.

>    $ find /usr/lib64/python3.3 -name "*.py" -exec grep -H PicklingError {} \;
>    /usr/lib64/python3.3/site-packages/numpy/numarray/session.py:        except (pickle.PicklingError, TypeError, SystemError):
>    /usr/lib64/python3.3/pickle.py:__all__ = ["PickleError", "PicklingError", "UnpicklingError", "Pickler",
>    /usr/lib64/python3.3/pickle.py:class PicklingError(PickleError):
>    /usr/lib64/python3.3/pickle.py:            raise PicklingError("Pickler.__init__() was not called by "
>    /usr/lib64/python3.3/pickle.py:                    raise PicklingError("Can't pickle %r object: %r" %
>    /usr/lib64/python3.3/pickle.py:            raise PicklingError("%s must return string or tuple" % reduce)
>    /usr/lib64/python3.3/pickle.py:            raise PicklingError("Tuple returned by %s must have "
>    /usr/lib64/python3.3/pickle.py:            raise PicklingError("args from save_reduce() should be a tuple")
>    /usr/lib64/python3.3/pickle.py:            raise PicklingError("func from save_reduce() should be callable")

Most of the above look like bugs in code intended to work with Pickle. 
In other words, if you user gets a TypeError, the user has made a 
mistake by trying to pickle your object. If you tried to make your 
object picklable, and users got PicklingError, that would indicate a bug 
in your class code, not the users' use of your class.

>    /usr/lib64/python3.3/pickle.py:                raise PicklingError(
>    /usr/lib64/python3.3/pickle.py:                raise PicklingError(
>    /usr/lib64/python3.3/pickle.py:            raise PicklingError(
>    /usr/lib64/python3.3/pickle.py:                raise PicklingError(
>    /usr/lib64/python3.3/pickle.py:                raise PicklingError(
>    /usr/lib64/python3.3/idlelib/rpc.py:        except pickle.PicklingError:
>
> Which exception would you raise for an object that can't be
> pickled and why?

TypeError, as explained above.

> [1] http://ftputil.sschwarzer.net/trac/ticket/75
> [2] https://docs.python.org/3.4/library/pickle.html

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Recommended exception for objects that can't be pickled Stefan Schwarzer <sschwarzer@sschwarzer.net> - 2014-04-21 15:23 +0200
  Re: Recommended exception for objects that can't be pickled Terry Reedy <tjreedy@udel.edu> - 2014-04-21 17:08 -0400

csiph-web