Recommend bytes-check to deserialize_dbobjs example

This commit is contained in:
Griatch 2022-07-14 12:50:01 +02:00
parent 2fd8afa2fa
commit 60296a8554

View file

@ -325,14 +325,15 @@ values into a string representation before storing it to the database. This is d
### Storing single objects ### Storing single objects
With a single object, we mean anything that is *not iterable*, like numbers, strings or custom class With a single object, we mean anything that is *not iterable*, like numbers,
instances without the `__iter__` method. strings or custom class instances without the `__iter__` method.
* You can generally store any non-iterable Python entity that can be _pickled_. * You can generally store any non-iterable Python entity that can be _pickled_.
* Single database objects/typeclasses can be stored, despite them normally not being possible * Single database objects/typeclasses can be stored, despite them normally not
to pickle. Evennia will convert them to an internal representation using theihr classname, being possible to pickle. Evennia will convert them to an internal
database-id and creation-date with a microsecond precision. When retrieving, the object representation using theihr classname, database-id and creation-date with a
instance will be re-fetched from the database using this information. microsecond precision. When retrieving, the object instance will be re-fetched
from the database using this information.
* If you 'hide' a db-obj as a property on a custom class, Evennia will not be * If you 'hide' a db-obj as a property on a custom class, Evennia will not be
able to find it to serialize it. For that you need to help it out (see below). able to find it to serialize it. For that you need to help it out (see below).
@ -389,6 +390,8 @@ class Container:
def __deserialize_dbobjs__(self): def __deserialize_dbobjs__(self):
"""This is called after deserialization and allows you to """This is called after deserialization and allows you to
restore the 'hidden' dbobjs you serialized before""" restore the 'hidden' dbobjs you serialized before"""
if isinstance(self.mydbobj, bytes):
# make sure to check if it's bytes before trying dbunserialize
self.mydbobj = dbserialize.dbunserialize(self.mydbobj) self.mydbobj = dbserialize.dbunserialize(self.mydbobj)
# let's assume myobj is a db-object # let's assume myobj is a db-object
@ -396,6 +399,14 @@ container = Container(myobj)
obj.db.mydata = container # will now work fine! obj.db.mydata = container # will now work fine!
``` ```
> Note the extra check in `__deserialize_dbobjs__` to make sure the thing you
> are deserializing is a `bytes` object. This is needed because the Attribute's
> cache reruns deserializations in some situations when the data was already
> once deserialized. If you see errors in the log saying
> `Could not unpickle data for storage: ...`, the reason is
> likely that you forgot to add this check.
### Storing multiple objects ### Storing multiple objects
This means storing objects in a collection of some kind and are examples of *iterables*, pickle-able This means storing objects in a collection of some kind and are examples of *iterables*, pickle-able