| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#include <Python.h> |
| 27 |
#include <structmember.h> |
| 28 |
|
| 29 |
#include "cursor.h" |
| 30 |
#include "microprotocols.h" |
| 31 |
#include "prepare_protocol.h" |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
PyObject *psyco_adapters; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
int |
| 41 |
pysqlite_microprotocols_init(PyObject *dict) |
| 42 |
{ |
| 43 |
|
| 44 |
if ((psyco_adapters = PyDict_New()) == NULL) { |
| 45 |
return -1; |
| 46 |
} |
| 47 |
|
| 48 |
return PyDict_SetItemString(dict, "adapters", psyco_adapters); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
int |
| 55 |
pysqlite_microprotocols_add(PyTypeObject *type, PyObject *proto, PyObject *cast) |
| 56 |
{ |
| 57 |
PyObject* key; |
| 58 |
int rc; |
| 59 |
|
| 60 |
if (proto == NULL) proto = (PyObject*)&pysqlite_PrepareProtocolType; |
| 61 |
|
| 62 |
key = Py_BuildValue("(OO)", (PyObject*)type, proto); |
| 63 |
if (!key) { |
| 64 |
return -1; |
| 65 |
} |
| 66 |
|
| 67 |
rc = PyDict_SetItem(psyco_adapters, key, cast); |
| 68 |
Py_DECREF(key); |
| 69 |
|
| 70 |
return rc; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
PyObject * |
| 76 |
pysqlite_microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt) |
| 77 |
{ |
| 78 |
PyObject *adapter, *key; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
key = Py_BuildValue("(OO)", (PyObject*)obj->ob_type, proto); |
| 86 |
if (!key) { |
| 87 |
return NULL; |
| 88 |
} |
| 89 |
adapter = PyDict_GetItem(psyco_adapters, key); |
| 90 |
Py_DECREF(key); |
| 91 |
if (adapter) { |
| 92 |
PyObject *adapted = PyObject_CallFunctionObjArgs(adapter, obj, NULL); |
| 93 |
return adapted; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
if (PyObject_HasAttrString(proto, "__adapt__")) { |
| 98 |
PyObject *adapted = PyObject_CallMethod(proto, "__adapt__", "O", obj); |
| 99 |
if (adapted) { |
| 100 |
if (adapted != Py_None) { |
| 101 |
return adapted; |
| 102 |
} else { |
| 103 |
Py_DECREF(adapted); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
if (PyErr_Occurred() && !PyErr_ExceptionMatches(PyExc_TypeError)) |
| 108 |
return NULL; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
if (PyObject_HasAttrString(obj, "__conform__")) { |
| 113 |
PyObject *adapted = PyObject_CallMethod(obj, "__conform__","O", proto); |
| 114 |
if (adapted) { |
| 115 |
if (adapted != Py_None) { |
| 116 |
return adapted; |
| 117 |
} else { |
| 118 |
Py_DECREF(adapted); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
if (PyErr_Occurred() && !PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 123 |
return NULL; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
PyErr_SetString(pysqlite_ProgrammingError, "can't adapt"); |
| 129 |
return NULL; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
PyObject * |
| 135 |
pysqlite_adapt(pysqlite_Cursor *self, PyObject *args) |
| 136 |
{ |
| 137 |
PyObject *obj, *alt = NULL; |
| 138 |
PyObject *proto = (PyObject*)&pysqlite_PrepareProtocolType; |
| 139 |
|
| 140 |
if (!PyArg_ParseTuple(args, "O|OO", &obj, &proto, &alt)) return NULL; |
| 141 |
return pysqlite_microprotocols_adapt(obj, proto, alt); |
| 142 |
} |