summaryrefslogtreecommitdiff
path: root/psycopg
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2010-10-12 01:13:06 +0100
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2010-11-05 09:34:49 +0000
commitab8e1450633a76e8400cf65ca5f07ffba5fd7821 (patch)
treeca6e32a232ab4f21c89c9fa39a14c50d51ceb498 /psycopg
parent3312897e5dc17f9b49c4eebdd4a87c9ac3ab12dd (diff)
downloadpsycopg2-ab8e1450633a76e8400cf65ca5f07ffba5fd7821.tar.gz
Encoding/decoding in base64 refactored.
Diffstat (limited to 'psycopg')
-rw-r--r--psycopg/xid_type.c35
1 files changed, 16 insertions, 19 deletions
diff --git a/psycopg/xid_type.c b/psycopg/xid_type.c
index 39cbed8..dec8b80 100644
--- a/psycopg/xid_type.c
+++ b/psycopg/xid_type.c
@@ -339,46 +339,43 @@ XidObject *xid_ensure(PyObject *oxid)
}
-/* Return a base64-encoded string. */
+/* Encode or decode a string in base64. */
static PyObject *
-_xid_encode64(PyObject *s)
+_xid_base64_enc_dec(const char *funcname, PyObject *s)
{
PyObject *base64 = NULL;
- PyObject *encode = NULL;
+ PyObject *func = 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; }
+ if (!(func = PyObject_GetAttrString(base64, funcname))) { goto exit; }
+ rv = PyObject_CallFunctionObjArgs(func, s, NULL);
exit:
- Py_XDECREF(encode);
+ Py_XDECREF(func);
Py_XDECREF(base64);
return rv;
}
-/* decode a base64-encoded string */
+/* Return a base64-encoded string. */
static PyObject *
-_xid_decode64(PyObject *s)
+_xid_encode64(PyObject *s)
{
- PyObject *base64 = NULL;
- 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; }
+ return _xid_base64_enc_dec("b64encode", s);
+}
-exit:
- Py_XDECREF(decode);
- Py_XDECREF(base64);
+/* Decode a base64-encoded string */
- return rv;
+static PyObject *
+_xid_decode64(PyObject *s)
+{
+ return _xid_base64_enc_dec("b64decode", s);
}
+
/* Return the PostgreSQL transaction_id for this XA xid.
*
* PostgreSQL wants just a string, while the DBAPI supports the XA standard