Hallöchen!

teth writes:

> Using str(pickle.dumps()) and the pickle.loads(str()) works
> perfectly.

Unfortunately, I had to realise that it is not so simple due to
<http://bugs.python.org/issue2980>.  Now it use the following
functions:

def ascii_pickle(python_object):
    u"""Converts an arbitrary Python object to an ASCII-only string to be
    written to the database.  Unfortunately, even protocol 0 of Python's
    ``pickle`` module may emit non-ASCII characters, see
    <http://bugs.python.org/issue2980>.  However, this is dangerous since the
    database expects Unicode strings and can't decode such octet strings.
    Therefore, this routine does another encoding step using base64.  This
    makes debugging slightly more difficult, but there we go.

    :Parameters:
      - `python_object`: the object instance to be pickled

    :type python_object: object

    :Return:
      the ASCII-only string representing the object instance

    :rtype: str
    """
    # FixMe: Maybe instead of base64 another encoder should be used that keeps
    # the string largely readable.
    return base64.b64encode(pickle.dumps(python_object, protocol=2))


def ascii_unpickle(string):
    u"""This is the inverse function of `ascii_pickle`.

    :Parameters:
      - `string`: the ASCII-only database string to be unpickled

    :type string: str

    :Return:
      the original Python object instance that was represented by the string

    :rtype: object
    """
    return pickle.loads(base64.b64decode(string))

Tschö,
Torsten.

-- 
Torsten Bronger, aquisgrana, europa vetus
                   Jabber ID: torsten.bron...@jabber.rwth-aachen.de


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to