summaryrefslogtreecommitdiff
path: root/psycopg/microprotocols.c
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2010-11-09 02:38:59 +0000
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2010-11-09 02:41:43 +0000
commit2dc28ee7d87b50a80f4adac019ee0765c68a350e (patch)
treea1a08ffc59d939738d0f7ecf23bc7f6eb42e72a4 /psycopg/microprotocols.c
parent753b580d727da0e3ec01c1c319c4dec9f474891e (diff)
downloadpsycopg2-2dc28ee7d87b50a80f4adac019ee0765c68a350e.tar.gz
Less lookups and more efficient calls in microprotocols_adapt().
Also more explicit handling of the exception indicator.
Diffstat (limited to 'psycopg/microprotocols.c')
-rw-r--r--psycopg/microprotocols.c38
1 files changed, 29 insertions, 9 deletions
diff --git a/psycopg/microprotocols.c b/psycopg/microprotocols.c
index 42435b7..a4f3517 100644
--- a/psycopg/microprotocols.c
+++ b/psycopg/microprotocols.c
@@ -129,7 +129,7 @@ _get_superclass_adapter(PyObject *obj, PyObject *proto)
PyObject *
microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt)
{
- PyObject *adapter, *adapted, *key;
+ PyObject *adapter, *adapted, *key, *meth;
char buffer[256];
/* we don't check for exact type conformance as specified in PEP 246
@@ -159,21 +159,41 @@ microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt)
}
/* try to have the protocol adapt this object*/
- if (PyObject_HasAttrString(proto, "__adapt__")) {
- adapted = PyObject_CallMethod(proto, "__adapt__", "O", obj);
+ if ((meth = PyObject_GetAttrString(proto, "__adapt__"))) {
+ adapted = PyObject_CallFunctionObjArgs(meth, obj, NULL);
+ Py_DECREF(meth);
if (adapted && adapted != Py_None) return adapted;
Py_XDECREF(adapted);
- if (PyErr_Occurred() && !PyErr_ExceptionMatches(PyExc_TypeError))
- return NULL;
+ if (PyErr_Occurred()) {
+ if (PyErr_ExceptionMatches(PyExc_TypeError)) {
+ PyErr_Clear();
+ } else {
+ return NULL;
+ }
+ }
+ }
+ else {
+ /* proto.__adapt__ not found. */
+ PyErr_Clear();
}
/* and finally try to have the object adapt itself */
- if (PyObject_HasAttrString(obj, "__conform__")) {
- adapted = PyObject_CallMethod(obj, "__conform__","O", proto);
+ if ((meth = PyObject_GetAttrString(obj, "__conform__"))) {
+ adapted = PyObject_CallFunctionObjArgs(meth, proto, NULL);
+ Py_DECREF(meth);
if (adapted && adapted != Py_None) return adapted;
Py_XDECREF(adapted);
- if (PyErr_Occurred() && !PyErr_ExceptionMatches(PyExc_TypeError))
- return NULL;
+ if (PyErr_Occurred()) {
+ if (PyErr_ExceptionMatches(PyExc_TypeError)) {
+ PyErr_Clear();
+ } else {
+ return NULL;
+ }
+ }
+ }
+ else {
+ /* obj.__conform__ not found. */
+ PyErr_Clear();
}
/* else set the right exception and return NULL */