summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2010-11-09 02:12:37 +0000
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2010-11-09 02:21:21 +0000
commit753b580d727da0e3ec01c1c319c4dec9f474891e (patch)
tree6106af30fe6709e5944df2a0d8ded5772483d50e
parent422fede38eed9667a2200f0aaf4afe9e3ac9d551 (diff)
downloadpsycopg2-753b580d727da0e3ec01c1c319c4dec9f474891e.tar.gz
Less lookups and more efficient calls in microprotocols_getquoted().
-rw-r--r--ChangeLog2
-rw-r--r--psycopg/microprotocols.c51
2 files changed, 33 insertions, 20 deletions
diff --git a/ChangeLog b/ChangeLog
index 898b203..762002b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,8 @@
* Replaced PyObject_CallFunction() with *ObjArgs() where more efficient.
+ * psycopg/microprotocols.c: small optimizations.
+
2010-11-08 Daniele Varrazzo <daniele.varrazzo@gmail.com>
* psycopg/microprotocols.c: use faster function to build tuples.
diff --git a/psycopg/microprotocols.c b/psycopg/microprotocols.c
index c8cd5fe..42435b7 100644
--- a/psycopg/microprotocols.c
+++ b/psycopg/microprotocols.c
@@ -188,31 +188,42 @@ PyObject *
microprotocol_getquoted(PyObject *obj, connectionObject *conn)
{
PyObject *res = NULL;
- PyObject *tmp = microprotocols_adapt(
- obj, (PyObject*)&isqlquoteType, NULL);
-
- if (tmp != NULL) {
- Dprintf("microprotocol_getquoted: adapted to %s",
- tmp->ob_type->tp_name);
-
- /* if requested prepare the object passing it the connection */
- if (PyObject_HasAttrString(tmp, "prepare") && conn) {
- res = PyObject_CallMethod(tmp, "prepare", "O", (PyObject*)conn);
- if (res == NULL) {
- Py_DECREF(tmp);
- return NULL;
- }
- else {
+ PyObject *prepare = NULL;
+ PyObject *adapted;
+
+ if (!(adapted = microprotocols_adapt(obj, (PyObject*)&isqlquoteType, NULL))) {
+ goto exit;
+ }
+
+ Dprintf("microprotocol_getquoted: adapted to %s",
+ adapted->ob_type->tp_name);
+
+ /* if requested prepare the object passing it the connection */
+ if (conn) {
+ if ((prepare = PyObject_GetAttrString(adapted, "prepare"))) {
+ res = PyObject_CallFunctionObjArgs(
+ prepare, (PyObject *)conn, NULL);
+ if (res) {
Py_DECREF(res);
+ res = NULL;
+ } else {
+ goto exit;
}
}
-
- /* call the getquoted method on tmp (that should exist because we
- adapted to the right protocol) */
- res = PyObject_CallMethod(tmp, "getquoted", NULL);
- Py_DECREF(tmp);
+ else {
+ /* adapted.prepare not found */
+ PyErr_Clear();
+ }
}
+ /* call the getquoted method on adapted (that should exist because we
+ adapted to the right protocol) */
+ res = PyObject_CallMethod(adapted, "getquoted", NULL);
+
+exit:
+ Py_XDECREF(adapted);
+ Py_XDECREF(prepare);
+
/* we return res with one extra reference, the caller shall free it */
return res;
}