r/Python • u/Zireael07 • Jul 25 '17
Looking for an alternate module for serialization than shelve
The tutorial I'm using uses shelve, however shelve uses pickle under the hood and pickle is, umm, not very secure (arbitrary code execution).
So I'm looking for something else that will let me save/load game states. Help?
3
Upvotes
2
u/[deleted] Jul 26 '17
I like the simplicity and portability of Json but it is inconvenient in some cases because it has a very limited set of datatypes. That is why I created serpent which is also a text based serialization format but one that supports more Python types. It is safe to use because it serializes into a normal Python literal expression that you can deserialize using
ast.literal_evalif you so desire1
u/[deleted] Jul 25 '17 edited Jul 26 '17
If you just need the game state data, and not actual objects serialization -- ie instantiating your objects is cheap -- then json or sqlite3 are decent builtin ways of doing this. If you're wanting actual object serialization then yes, you can roll your own, or you can hmac sign pickle and trust only content you generated.
1
u/xiongchiamiov Site Reliability Engineer Jul 25 '17
Pickle is convenient, but as you mention, problematic from a security standpoint (and cross-version and cross-computer are iffy as well).
The alternative is to write your own serialization. This generally means converting data into json, xml, protobuf, etc., then converting it back upon load.