X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb-python/blobdiff_plain/b1f8a1a1b30987f472b67bf3a10bc6b1ab4170a1..54fd7594ee5df9dbc9745d98adaa01a5ed43b6e4:/catacomb/pwsafe.py diff --git a/catacomb/pwsafe.py b/catacomb/pwsafe.py index 5e296e3..944cfa7 100644 --- a/catacomb/pwsafe.py +++ b/catacomb/pwsafe.py @@ -28,21 +28,31 @@ from __future__ import with_statement +import binascii as _B import errno as _E import os as _OS -from cStringIO import StringIO as _StringIO +import sys as _SYS + +if _SYS.version_info >= (3,): from io import StringIO as _StringIO +else: from cStringIO import StringIO as _StringIO import catacomb as _C ###-------------------------------------------------------------------------- ### Python version portability. -def _iterkeys(dict): return dict.iterkeys() -def _itervalues(dict): return dict.itervalues() -def _iteritems(dict): return dict.iteritems() - -def _bin(text): return text -def _text(bin): return bin +if _SYS.version_info >= (3,): + def _iterkeys(dict): return dict.keys() + def _itervalues(dict): return dict.values() + def _iteritems(dict): return dict.items() + def _bin(text): return text.encode(errors = "surrogateescape") + def _text(bin): return bin.decode(errors = "surrogateescape") +else: + def _iterkeys(dict): return dict.iterkeys() + def _itervalues(dict): return dict.itervalues() + def _iteritems(dict): return dict.iteritems() + def _bin(text): return text + def _text(bin): return bin _NUL = _bin('\0') _CIPHER = _bin('cipher:') @@ -103,10 +113,10 @@ def _dec_metaname(name): def _b64(s): """Encode S as base64, without newlines, and trimming `=' padding.""" - return s.encode('base64').replace('\n', '').rstrip('=') + return _text(_B.b2a_base64(s)).replace('\n', '').rstrip('=') def _unb64(s): """Decode S as base64 with trimmed `=' padding.""" - return (s + '='*((4 - len(s))%4)).decode('base64') + return _B.a2b_base64(s + '='*((4 - len(s))%4)) def _enc_metaval(val): """Encode VAL as a metadata item value, returning the result."""