summaryrefslogtreecommitdiff
path: root/psycopg
diff options
context:
space:
mode:
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