Encoding/decoding in base64 refactored.

This commit is contained in:
Daniele Varrazzo 2010-10-12 01:13:06 +01:00
parent 3312897e5d
commit ab8e145063

View File

@ -339,46 +339,43 @@ XidObject *xid_ensure(PyObject *oxid)
} }
/* Encode or decode a string in base64. */
static PyObject *
_xid_base64_enc_dec(const char *funcname, PyObject *s)
{
PyObject *base64 = NULL;
PyObject *func = NULL;
PyObject *rv = NULL;
if (!(base64 = PyImport_ImportModule("base64"))) { goto exit; }
if (!(func = PyObject_GetAttrString(base64, funcname))) { goto exit; }
rv = PyObject_CallFunctionObjArgs(func, s, NULL);
exit:
Py_XDECREF(func);
Py_XDECREF(base64);
return rv;
}
/* Return a base64-encoded string. */ /* Return a base64-encoded string. */
static PyObject * static PyObject *
_xid_encode64(PyObject *s) _xid_encode64(PyObject *s)
{ {
PyObject *base64 = NULL; return _xid_base64_enc_dec("b64encode", s);
PyObject *encode = NULL;
PyObject *rv = NULL;
if (!(base64 = PyImport_ImportModule("base64"))) { goto exit; }
if (!(encode = PyObject_GetAttrString(base64, "b64encode"))) { goto exit; }
if (!(rv = PyObject_CallFunctionObjArgs(encode, s, NULL))) { goto exit; }
exit:
Py_XDECREF(encode);
Py_XDECREF(base64);
return rv;
} }
/* decode a base64-encoded string */ /* Decode a base64-encoded string */
static PyObject * static PyObject *
_xid_decode64(PyObject *s) _xid_decode64(PyObject *s)
{ {
PyObject *base64 = NULL; return _xid_base64_enc_dec("b64decode", s);
PyObject *decode = NULL;
PyObject *rv = NULL;
if (!(base64 = PyImport_ImportModule("base64"))) { goto exit; }
if (!(decode = PyObject_GetAttrString(base64, "b64decode"))) { goto exit; }
if (!(rv = PyObject_CallFunctionObjArgs(decode, s, NULL))) { goto exit; }
exit:
Py_XDECREF(decode);
Py_XDECREF(base64);
return rv;
} }
/* Return the PostgreSQL transaction_id for this XA xid. /* Return the PostgreSQL transaction_id for this XA xid.
* *
* PostgreSQL wants just a string, while the DBAPI supports the XA standard * PostgreSQL wants just a string, while the DBAPI supports the XA standard