| 1 | n/a | /* microprotocols.c - minimalist and non-validating protocols implementation |
|---|
| 2 | n/a | * |
|---|
| 3 | n/a | * Copyright (C) 2003-2004 Federico Di Gregorio <fog@debian.org> |
|---|
| 4 | n/a | * |
|---|
| 5 | n/a | * This file is part of psycopg and was adapted for pysqlite. Federico Di |
|---|
| 6 | n/a | * Gregorio gave the permission to use it within pysqlite under the following |
|---|
| 7 | n/a | * license: |
|---|
| 8 | n/a | * |
|---|
| 9 | n/a | * This software is provided 'as-is', without any express or implied |
|---|
| 10 | n/a | * warranty. In no event will the authors be held liable for any damages |
|---|
| 11 | n/a | * arising from the use of this software. |
|---|
| 12 | n/a | * |
|---|
| 13 | n/a | * Permission is granted to anyone to use this software for any purpose, |
|---|
| 14 | n/a | * including commercial applications, and to alter it and redistribute it |
|---|
| 15 | n/a | * freely, subject to the following restrictions: |
|---|
| 16 | n/a | * |
|---|
| 17 | n/a | * 1. The origin of this software must not be misrepresented; you must not |
|---|
| 18 | n/a | * claim that you wrote the original software. If you use this software |
|---|
| 19 | n/a | * in a product, an acknowledgment in the product documentation would be |
|---|
| 20 | n/a | * appreciated but is not required. |
|---|
| 21 | n/a | * 2. Altered source versions must be plainly marked as such, and must not be |
|---|
| 22 | n/a | * misrepresented as being the original software. |
|---|
| 23 | n/a | * 3. This notice may not be removed or altered from any source distribution. |
|---|
| 24 | n/a | */ |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | #include <Python.h> |
|---|
| 27 | n/a | #include <structmember.h> |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | #include "cursor.h" |
|---|
| 30 | n/a | #include "microprotocols.h" |
|---|
| 31 | n/a | #include "prepare_protocol.h" |
|---|
| 32 | n/a | |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | /** the adapters registry **/ |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | PyObject *psyco_adapters; |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | /* pysqlite_microprotocols_init - initialize the adapters dictionary */ |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | int |
|---|
| 41 | n/a | pysqlite_microprotocols_init(PyObject *dict) |
|---|
| 42 | n/a | { |
|---|
| 43 | n/a | /* create adapters dictionary and put it in module namespace */ |
|---|
| 44 | n/a | if ((psyco_adapters = PyDict_New()) == NULL) { |
|---|
| 45 | n/a | return -1; |
|---|
| 46 | n/a | } |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | return PyDict_SetItemString(dict, "adapters", psyco_adapters); |
|---|
| 49 | n/a | } |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | /* pysqlite_microprotocols_add - add a reverse type-caster to the dictionary */ |
|---|
| 53 | n/a | |
|---|
| 54 | n/a | int |
|---|
| 55 | n/a | pysqlite_microprotocols_add(PyTypeObject *type, PyObject *proto, PyObject *cast) |
|---|
| 56 | n/a | { |
|---|
| 57 | n/a | PyObject* key; |
|---|
| 58 | n/a | int rc; |
|---|
| 59 | n/a | |
|---|
| 60 | n/a | if (proto == NULL) proto = (PyObject*)&pysqlite_PrepareProtocolType; |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | key = Py_BuildValue("(OO)", (PyObject*)type, proto); |
|---|
| 63 | n/a | if (!key) { |
|---|
| 64 | n/a | return -1; |
|---|
| 65 | n/a | } |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | rc = PyDict_SetItem(psyco_adapters, key, cast); |
|---|
| 68 | n/a | Py_DECREF(key); |
|---|
| 69 | n/a | |
|---|
| 70 | n/a | return rc; |
|---|
| 71 | n/a | } |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | /* pysqlite_microprotocols_adapt - adapt an object to the built-in protocol */ |
|---|
| 74 | n/a | |
|---|
| 75 | n/a | PyObject * |
|---|
| 76 | n/a | pysqlite_microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt) |
|---|
| 77 | n/a | { |
|---|
| 78 | n/a | PyObject *adapter, *key; |
|---|
| 79 | n/a | |
|---|
| 80 | n/a | /* we don't check for exact type conformance as specified in PEP 246 |
|---|
| 81 | n/a | because the pysqlite_PrepareProtocolType type is abstract and there is no |
|---|
| 82 | n/a | way to get a quotable object to be its instance */ |
|---|
| 83 | n/a | |
|---|
| 84 | n/a | /* look for an adapter in the registry */ |
|---|
| 85 | n/a | key = Py_BuildValue("(OO)", (PyObject*)obj->ob_type, proto); |
|---|
| 86 | n/a | if (!key) { |
|---|
| 87 | n/a | return NULL; |
|---|
| 88 | n/a | } |
|---|
| 89 | n/a | adapter = PyDict_GetItem(psyco_adapters, key); |
|---|
| 90 | n/a | Py_DECREF(key); |
|---|
| 91 | n/a | if (adapter) { |
|---|
| 92 | n/a | PyObject *adapted = PyObject_CallFunctionObjArgs(adapter, obj, NULL); |
|---|
| 93 | n/a | return adapted; |
|---|
| 94 | n/a | } |
|---|
| 95 | n/a | |
|---|
| 96 | n/a | /* try to have the protocol adapt this object*/ |
|---|
| 97 | n/a | if (PyObject_HasAttrString(proto, "__adapt__")) { |
|---|
| 98 | n/a | _Py_IDENTIFIER(__adapt__); |
|---|
| 99 | n/a | PyObject *adapted = _PyObject_CallMethodId(proto, &PyId___adapt__, "O", obj); |
|---|
| 100 | n/a | |
|---|
| 101 | n/a | if (adapted) { |
|---|
| 102 | n/a | if (adapted != Py_None) { |
|---|
| 103 | n/a | return adapted; |
|---|
| 104 | n/a | } else { |
|---|
| 105 | n/a | Py_DECREF(adapted); |
|---|
| 106 | n/a | } |
|---|
| 107 | n/a | } |
|---|
| 108 | n/a | |
|---|
| 109 | n/a | if (PyErr_Occurred() && !PyErr_ExceptionMatches(PyExc_TypeError)) |
|---|
| 110 | n/a | return NULL; |
|---|
| 111 | n/a | } |
|---|
| 112 | n/a | |
|---|
| 113 | n/a | /* and finally try to have the object adapt itself */ |
|---|
| 114 | n/a | if (PyObject_HasAttrString(obj, "__conform__")) { |
|---|
| 115 | n/a | _Py_IDENTIFIER(__conform__); |
|---|
| 116 | n/a | PyObject *adapted = _PyObject_CallMethodId(obj, &PyId___conform__,"O", proto); |
|---|
| 117 | n/a | |
|---|
| 118 | n/a | if (adapted) { |
|---|
| 119 | n/a | if (adapted != Py_None) { |
|---|
| 120 | n/a | return adapted; |
|---|
| 121 | n/a | } else { |
|---|
| 122 | n/a | Py_DECREF(adapted); |
|---|
| 123 | n/a | } |
|---|
| 124 | n/a | } |
|---|
| 125 | n/a | |
|---|
| 126 | n/a | if (PyErr_Occurred() && !PyErr_ExceptionMatches(PyExc_TypeError)) { |
|---|
| 127 | n/a | return NULL; |
|---|
| 128 | n/a | } |
|---|
| 129 | n/a | } |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | /* else set the right exception and return NULL */ |
|---|
| 132 | n/a | PyErr_SetString(pysqlite_ProgrammingError, "can't adapt"); |
|---|
| 133 | n/a | return NULL; |
|---|
| 134 | n/a | } |
|---|
| 135 | n/a | |
|---|
| 136 | n/a | /** module-level functions **/ |
|---|
| 137 | n/a | |
|---|
| 138 | n/a | PyObject * |
|---|
| 139 | n/a | pysqlite_adapt(pysqlite_Cursor *self, PyObject *args) |
|---|
| 140 | n/a | { |
|---|
| 141 | n/a | PyObject *obj, *alt = NULL; |
|---|
| 142 | n/a | PyObject *proto = (PyObject*)&pysqlite_PrepareProtocolType; |
|---|
| 143 | n/a | |
|---|
| 144 | n/a | if (!PyArg_ParseTuple(args, "O|OO", &obj, &proto, &alt)) return NULL; |
|---|
| 145 | n/a | return pysqlite_microprotocols_adapt(obj, proto, alt); |
|---|
| 146 | n/a | } |
|---|