diff options
| -rw-r--r-- | psycopg/connection_int.c | 8 | ||||
| -rw-r--r-- | psycopg/xid.h | 2 | ||||
| -rw-r--r-- | psycopg/xid_type.c | 28 |
3 files changed, 13 insertions, 25 deletions
diff --git a/psycopg/connection_int.c b/psycopg/connection_int.c index 2f2a48d..00e8fa4 100644 --- a/psycopg/connection_int.c +++ b/psycopg/connection_int.c @@ -939,18 +939,20 @@ conn_tpc_command(connectionObject *self, const char *cmd, XidObject *xid) { PGresult *pgres = NULL; char *error = NULL; - char *tid = NULL; + PyObject *tid = NULL; + const char *ctid; int rv = -1; Dprintf("conn_tpc_command: %s", cmd); /* convert the xid into PostgreSQL transaction id while keeping the GIL */ if (!(tid = xid_get_tid(xid))) { goto exit; } + if (!(ctid = PyString_AsString(tid))) { goto exit; } Py_BEGIN_ALLOW_THREADS; pthread_mutex_lock(&self->lock); - if (0 > (rv = pq_tpc_command_locked(self, cmd, tid, + if (0 > (rv = pq_tpc_command_locked(self, cmd, ctid, &pgres, &error, &_save))) { pthread_mutex_unlock(&self->lock); Py_BLOCK_THREADS; @@ -962,7 +964,7 @@ conn_tpc_command(connectionObject *self, const char *cmd, XidObject *xid) Py_END_ALLOW_THREADS; exit: - PyMem_Free(tid); + Py_XDECREF(tid); return rv; } diff --git a/psycopg/xid.h b/psycopg/xid.h index 0c0d3bd..017f3b4 100644 --- a/psycopg/xid.h +++ b/psycopg/xid.h @@ -49,7 +49,7 @@ typedef struct { HIDDEN XidObject *xid_ensure(PyObject *oxid); HIDDEN XidObject *xid_from_string(PyObject *s); -HIDDEN char *xid_get_tid(XidObject *self); +HIDDEN PyObject *xid_get_tid(XidObject *self); HIDDEN PyObject *xid_recover(PyObject *conn); #endif /* PSYCOPG_XID_H */ diff --git a/psycopg/xid_type.c b/psycopg/xid_type.c index cfe0bc4..fbc4966 100644 --- a/psycopg/xid_type.c +++ b/psycopg/xid_type.c @@ -348,32 +348,26 @@ exit: * see also: the pgjdbc implementation * http://cvs.pgfoundry.org/cgi-bin/cvsweb.cgi/jdbc/pgjdbc/org/postgresql/xa/RecoveredXid.java?rev=1.2 */ -char * +PyObject * xid_get_tid(XidObject *self) { - char *buf = NULL; - Py_ssize_t bufsize = 0; + PyObject *rv = NULL; PyObject *egtrid = NULL; PyObject *ebqual = NULL; PyObject *format = NULL; PyObject *args = NULL; - PyObject *tid = NULL; if (Py_None == self->format_id) { /* Unparsed xid: return the gtrid. */ - bufsize = 1 + PyString_Size(self->gtrid); - if (!(buf = (char *)PyMem_Malloc(bufsize))) { - PyErr_NoMemory(); - goto exit; - } - strncpy(buf, PyString_AsString(self->gtrid), bufsize); + Py_INCREF(self->gtrid); + rv = self->gtrid; } else { /* XA xid: mash together the components. */ if (!(egtrid = _xid_encode64(self->gtrid))) { goto exit; } if (!(ebqual = _xid_encode64(self->bqual))) { goto exit; } - /* tid = "%d_%s_%s" % (format_id, egtrid, ebqual) */ + /* rv = "%d_%s_%s" % (format_id, egtrid, ebqual) */ if (!(format = PyString_FromString("%d_%s_%s"))) { goto exit; } if (!(args = PyTuple_New(3))) { goto exit; } @@ -382,14 +376,7 @@ xid_get_tid(XidObject *self) PyTuple_SET_ITEM(args, 1, egtrid); egtrid = NULL; PyTuple_SET_ITEM(args, 2, ebqual); ebqual = NULL; - if (!(tid = PyString_Format(format, args))) { goto exit; } - - bufsize = 1 + PyString_Size(tid); - if (!(buf = (char *)PyMem_Malloc(bufsize))) { - PyErr_NoMemory(); - goto exit; - } - strncpy(buf, PyString_AsString(tid), bufsize); + if (!(rv = PyString_Format(format, args))) { goto exit; } } exit: @@ -397,9 +384,8 @@ exit: Py_XDECREF(format); Py_XDECREF(egtrid); Py_XDECREF(ebqual); - Py_XDECREF(tid); - return buf; + return rv; } |
