+++ /dev/null
-/* microprotocols.c - minimalist and non-validating protocols implementation\r
- *\r
- * Copyright (C) 2003-2004 Federico Di Gregorio <fog@debian.org>\r
- *\r
- * This file is part of psycopg and was adapted for pysqlite. Federico Di\r
- * Gregorio gave the permission to use it within pysqlite under the following\r
- * license:\r
- *\r
- * This software is provided 'as-is', without any express or implied\r
- * warranty. In no event will the authors be held liable for any damages\r
- * arising from the use of this software.\r
- *\r
- * Permission is granted to anyone to use this software for any purpose,\r
- * including commercial applications, and to alter it and redistribute it\r
- * freely, subject to the following restrictions:\r
- *\r
- * 1. The origin of this software must not be misrepresented; you must not\r
- * claim that you wrote the original software. If you use this software\r
- * in a product, an acknowledgment in the product documentation would be\r
- * appreciated but is not required.\r
- * 2. Altered source versions must be plainly marked as such, and must not be\r
- * misrepresented as being the original software.\r
- * 3. This notice may not be removed or altered from any source distribution.\r
- */\r
-\r
-#include <Python.h>\r
-#include <structmember.h>\r
-\r
-#include "cursor.h"\r
-#include "microprotocols.h"\r
-#include "prepare_protocol.h"\r
-\r
-\r
-/** the adapters registry **/\r
-\r
-PyObject *psyco_adapters;\r
-\r
-/* pysqlite_microprotocols_init - initialize the adapters dictionary */\r
-\r
-int\r
-pysqlite_microprotocols_init(PyObject *dict)\r
-{\r
- /* create adapters dictionary and put it in module namespace */\r
- if ((psyco_adapters = PyDict_New()) == NULL) {\r
- return -1;\r
- }\r
-\r
- return PyDict_SetItemString(dict, "adapters", psyco_adapters);\r
-}\r
-\r
-\r
-/* pysqlite_microprotocols_add - add a reverse type-caster to the dictionary */\r
-\r
-int\r
-pysqlite_microprotocols_add(PyTypeObject *type, PyObject *proto, PyObject *cast)\r
-{\r
- PyObject* key;\r
- int rc;\r
-\r
- if (proto == NULL) proto = (PyObject*)&pysqlite_PrepareProtocolType;\r
-\r
- key = Py_BuildValue("(OO)", (PyObject*)type, proto);\r
- if (!key) {\r
- return -1;\r
- }\r
-\r
- rc = PyDict_SetItem(psyco_adapters, key, cast);\r
- Py_DECREF(key);\r
-\r
- return rc;\r
-}\r
-\r
-/* pysqlite_microprotocols_adapt - adapt an object to the built-in protocol */\r
-\r
-PyObject *\r
-pysqlite_microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt)\r
-{\r
- PyObject *adapter, *key;\r
-\r
- /* we don't check for exact type conformance as specified in PEP 246\r
- because the pysqlite_PrepareProtocolType type is abstract and there is no\r
- way to get a quotable object to be its instance */\r
-\r
- /* look for an adapter in the registry */\r
- key = Py_BuildValue("(OO)", (PyObject*)obj->ob_type, proto);\r
- if (!key) {\r
- return NULL;\r
- }\r
- adapter = PyDict_GetItem(psyco_adapters, key);\r
- Py_DECREF(key);\r
- if (adapter) {\r
- PyObject *adapted = PyObject_CallFunctionObjArgs(adapter, obj, NULL);\r
- return adapted;\r
- }\r
-\r
- /* try to have the protocol adapt this object*/\r
- if (PyObject_HasAttrString(proto, "__adapt__")) {\r
- PyObject *adapted = PyObject_CallMethod(proto, "__adapt__", "O", obj);\r
- if (adapted) {\r
- if (adapted != Py_None) {\r
- return adapted;\r
- } else {\r
- Py_DECREF(adapted);\r
- }\r
- }\r
-\r
- if (PyErr_Occurred() && !PyErr_ExceptionMatches(PyExc_TypeError))\r
- return NULL;\r
- }\r
-\r
- /* and finally try to have the object adapt itself */\r
- if (PyObject_HasAttrString(obj, "__conform__")) {\r
- PyObject *adapted = PyObject_CallMethod(obj, "__conform__","O", proto);\r
- if (adapted) {\r
- if (adapted != Py_None) {\r
- return adapted;\r
- } else {\r
- Py_DECREF(adapted);\r
- }\r
- }\r
-\r
- if (PyErr_Occurred() && !PyErr_ExceptionMatches(PyExc_TypeError)) {\r
- return NULL;\r
- }\r
- }\r
-\r
- /* else set the right exception and return NULL */\r
- PyErr_SetString(pysqlite_ProgrammingError, "can't adapt");\r
- return NULL;\r
-}\r
-\r
-/** module-level functions **/\r
-\r
-PyObject *\r
-pysqlite_adapt(pysqlite_Cursor *self, PyObject *args)\r
-{\r
- PyObject *obj, *alt = NULL;\r
- PyObject *proto = (PyObject*)&pysqlite_PrepareProtocolType;\r
-\r
- if (!PyArg_ParseTuple(args, "O|OO", &obj, &proto, &alt)) return NULL;\r
- return pysqlite_microprotocols_adapt(obj, proto, alt);\r
-}\r