PHP Unserialization in Python, and the Mysterious str.partition
At work we’re migrating our content to a Drupal install (it’s better than the ColdFusion piece of shit currently running). Since the interface is a piece of shit and the language is a piece of shit, I find myself doing a lot of things with Python, directly manipulating the database. Which means I effectively have ended up writing my own ORM layer for it.
Drupal, like many shit PHP “applications” stores serialized blobs of data in the database. To marshal the data back and forth they use PHP’s built-in functions which use an obtuse plaintext format (rather than say, JSON, though arguably it’s for historical reasons). Anyway so I found myself needing to unserialize this shit in Python and didn’t want to code my own. So I did what any time-starved person would do and .
The results were laughably . This one goes as far as using a fucking regex-based parser. They’re all horribly long and convoluted with absolutely massive functions which completely mask any scent of structure.
I think the grossest thing though, is that each of them (including the one on PyPI I found later, which is based off the first link above) goes through each individual character in the string looking for an instance of a specific character. They’re effectively implementing strchr, and in a very shitty way.
Python doesn’t have a strchr, but it has str.partition which is an even more useful. I can’t fatholm why none of the fucking implementations used it when it makes the code much cleaner, faster (presumably, as a built-in it’s implemented with strchr in CPython) and more concise.
Anyway tl;dr internet is filled with shit code so I wrote my own (edit: relink) which, although completely undocumented isn’t a massive fucking bloatfest.
Fuck.
13 comments