Back to index

python3.2  3.2.2
microprotocols.c
Go to the documentation of this file.
00001 /* microprotocols.c - minimalist and non-validating protocols implementation
00002  *
00003  * Copyright (C) 2003-2004 Federico Di Gregorio <fog@debian.org>
00004  *
00005  * This file is part of psycopg and was adapted for pysqlite. Federico Di
00006  * Gregorio gave the permission to use it within pysqlite under the following
00007  * license:
00008  *
00009  * This software is provided 'as-is', without any express or implied
00010  * warranty.  In no event will the authors be held liable for any damages
00011  * arising from the use of this software.
00012  *
00013  * Permission is granted to anyone to use this software for any purpose,
00014  * including commercial applications, and to alter it and redistribute it
00015  * freely, subject to the following restrictions:
00016  *
00017  * 1. The origin of this software must not be misrepresented; you must not
00018  *    claim that you wrote the original software. If you use this software
00019  *    in a product, an acknowledgment in the product documentation would be
00020  *    appreciated but is not required.
00021  * 2. Altered source versions must be plainly marked as such, and must not be
00022  *    misrepresented as being the original software.
00023  * 3. This notice may not be removed or altered from any source distribution.
00024  */
00025 
00026 #include <Python.h>
00027 #include <structmember.h>
00028 
00029 #include "cursor.h"
00030 #include "microprotocols.h"
00031 #include "prepare_protocol.h"
00032 
00033 
00036 PyObject *psyco_adapters;
00037 
00038 /* pysqlite_microprotocols_init - initialize the adapters dictionary */
00039 
00040 int
00041 pysqlite_microprotocols_init(PyObject *dict)
00042 {
00043     /* create adapters dictionary and put it in module namespace */
00044     if ((psyco_adapters = PyDict_New()) == NULL) {
00045         return -1;
00046     }
00047 
00048     return PyDict_SetItemString(dict, "adapters", psyco_adapters);
00049 }
00050 
00051 
00052 /* pysqlite_microprotocols_add - add a reverse type-caster to the dictionary */
00053 
00054 int
00055 pysqlite_microprotocols_add(PyTypeObject *type, PyObject *proto, PyObject *cast)
00056 {
00057     PyObject* key;
00058     int rc;
00059 
00060     if (proto == NULL) proto = (PyObject*)&pysqlite_PrepareProtocolType;
00061 
00062     key = Py_BuildValue("(OO)", (PyObject*)type, proto);
00063     if (!key) {
00064         return -1;
00065     }
00066 
00067     rc = PyDict_SetItem(psyco_adapters, key, cast);
00068     Py_DECREF(key);
00069 
00070     return rc;
00071 }
00072 
00073 /* pysqlite_microprotocols_adapt - adapt an object to the built-in protocol */
00074 
00075 PyObject *
00076 pysqlite_microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt)
00077 {
00078     PyObject *adapter, *key;
00079 
00080     /* we don't check for exact type conformance as specified in PEP 246
00081        because the pysqlite_PrepareProtocolType type is abstract and there is no
00082        way to get a quotable object to be its instance */
00083 
00084     /* look for an adapter in the registry */
00085     key = Py_BuildValue("(OO)", (PyObject*)obj->ob_type, proto);
00086     if (!key) {
00087         return NULL;
00088     }
00089     adapter = PyDict_GetItem(psyco_adapters, key);
00090     Py_DECREF(key);
00091     if (adapter) {
00092         PyObject *adapted = PyObject_CallFunctionObjArgs(adapter, obj, NULL);
00093         return adapted;
00094     }
00095 
00096     /* try to have the protocol adapt this object*/
00097     if (PyObject_HasAttrString(proto, "__adapt__")) {
00098         PyObject *adapted = PyObject_CallMethod(proto, "__adapt__", "O", obj);
00099         if (adapted) {
00100             if (adapted != Py_None) {
00101                 return adapted;
00102             } else {
00103                 Py_DECREF(adapted);
00104             }
00105         }
00106 
00107         if (PyErr_Occurred() && !PyErr_ExceptionMatches(PyExc_TypeError))
00108             return NULL;
00109     }
00110 
00111     /* and finally try to have the object adapt itself */
00112     if (PyObject_HasAttrString(obj, "__conform__")) {
00113         PyObject *adapted = PyObject_CallMethod(obj, "__conform__","O", proto);
00114         if (adapted) {
00115             if (adapted != Py_None) {
00116                 return adapted;
00117             } else {
00118                 Py_DECREF(adapted);
00119             }
00120         }
00121 
00122         if (PyErr_Occurred() && !PyErr_ExceptionMatches(PyExc_TypeError)) {
00123             return NULL;
00124         }
00125     }
00126 
00127     /* else set the right exception and return NULL */
00128     PyErr_SetString(pysqlite_ProgrammingError, "can't adapt");
00129     return NULL;
00130 }
00131 
00134 PyObject *
00135 pysqlite_adapt(pysqlite_Cursor *self, PyObject *args)
00136 {
00137     PyObject *obj, *alt = NULL;
00138     PyObject *proto = (PyObject*)&pysqlite_PrepareProtocolType;
00139 
00140     if (!PyArg_ParseTuple(args, "O|OO", &obj, &proto, &alt)) return NULL;
00141     return pysqlite_microprotocols_adapt(obj, proto, alt);
00142 }