blob: bb0d19f653b31244d04013ee3e1f2e4dc01b4f99 [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001/* microprotocols.c - minimalist and non-validating protocols implementation
2 *
3 * Copyright (C) 2003-2004 Federico Di Gregorio <fog@debian.org>
4 *
5 * This file is part of psycopg and was adapted for pysqlite. Federico Di
6 * Gregorio gave the permission to use it within pysqlite under the following
7 * license:
8 *
9 * This software is provided 'as-is', without any express or implied
10 * warranty. In no event will the authors be held liable for any damages
11 * arising from the use of this software.
12 *
13 * Permission is granted to anyone to use this software for any purpose,
14 * including commercial applications, and to alter it and redistribute it
15 * freely, subject to the following restrictions:
16 *
17 * 1. The origin of this software must not be misrepresented; you must not
18 * claim that you wrote the original software. If you use this software
19 * in a product, an acknowledgment in the product documentation would be
20 * appreciated but is not required.
21 * 2. Altered source versions must be plainly marked as such, and must not be
22 * misrepresented as being the original software.
23 * 3. This notice may not be removed or altered from any source distribution.
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/** the adapters registry **/
35
Benjamin Peterson0a37a302017-12-31 10:04:13 -080036static PyObject *psyco_adapters = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000037
Benjamin Petersond7b03282008-09-13 15:58:53 +000038/* pysqlite_microprotocols_init - initialize the adapters dictionary */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000039
40int
Benjamin Petersond7b03282008-09-13 15:58:53 +000041pysqlite_microprotocols_init(PyObject *dict)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000042{
43 /* create adapters dictionary and put it in module namespace */
44 if ((psyco_adapters = PyDict_New()) == NULL) {
45 return -1;
46 }
47
48 return PyDict_SetItemString(dict, "adapters", psyco_adapters);
49}
50
51
Benjamin Petersond7b03282008-09-13 15:58:53 +000052/* pysqlite_microprotocols_add - add a reverse type-caster to the dictionary */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000053
54int
Benjamin Petersond7b03282008-09-13 15:58:53 +000055pysqlite_microprotocols_add(PyTypeObject *type, PyObject *proto, PyObject *cast)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000056{
57 PyObject* key;
58 int rc;
59
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000060 if (proto == NULL) proto = (PyObject*)&pysqlite_PrepareProtocolType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000061
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
Benjamin Petersond7b03282008-09-13 15:58:53 +000073/* pysqlite_microprotocols_adapt - adapt an object to the built-in protocol */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000074
75PyObject *
Benjamin Petersond7b03282008-09-13 15:58:53 +000076pysqlite_microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000077{
Serhiy Storchakafc662ac2018-12-10 16:06:08 +020078 _Py_IDENTIFIER(__adapt__);
79 _Py_IDENTIFIER(__conform__);
80 PyObject *adapter, *key, *adapted;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000081
82 /* we don't check for exact type conformance as specified in PEP 246
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000083 because the pysqlite_PrepareProtocolType type is abstract and there is no
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000084 way to get a quotable object to be its instance */
85
86 /* look for an adapter in the registry */
Victor Stinnerdaa97562020-02-07 03:37:06 +010087 key = Py_BuildValue("(OO)", (PyObject*)Py_TYPE(obj), proto);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000088 if (!key) {
89 return NULL;
90 }
Serhiy Storchakafc662ac2018-12-10 16:06:08 +020091 adapter = PyDict_GetItemWithError(psyco_adapters, key);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000092 Py_DECREF(key);
93 if (adapter) {
Serhiy Storchakafc662ac2018-12-10 16:06:08 +020094 Py_INCREF(adapter);
Petr Viktorinffd97532020-02-11 17:46:57 +010095 adapted = PyObject_CallOneArg(adapter, obj);
Serhiy Storchakafc662ac2018-12-10 16:06:08 +020096 Py_DECREF(adapter);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000097 return adapted;
98 }
Serhiy Storchakafc662ac2018-12-10 16:06:08 +020099 if (PyErr_Occurred()) {
100 return NULL;
101 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000102
Serhiy Storchakafc662ac2018-12-10 16:06:08 +0200103 /* try to have the protocol adapt this object */
104 if (_PyObject_LookupAttrId(proto, &PyId___adapt__, &adapter) < 0) {
105 return NULL;
106 }
107 if (adapter) {
Petr Viktorinffd97532020-02-11 17:46:57 +0100108 adapted = PyObject_CallOneArg(adapter, obj);
Serhiy Storchakafc662ac2018-12-10 16:06:08 +0200109 Py_DECREF(adapter);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200110
Serhiy Storchakafc662ac2018-12-10 16:06:08 +0200111 if (adapted == Py_None) {
112 Py_DECREF(adapted);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000113 }
Serhiy Storchakafc662ac2018-12-10 16:06:08 +0200114 else if (adapted || !PyErr_ExceptionMatches(PyExc_TypeError)) {
115 return adapted;
116 }
117 else {
118 PyErr_Clear();
119 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000120 }
121
122 /* and finally try to have the object adapt itself */
Serhiy Storchakafc662ac2018-12-10 16:06:08 +0200123 if (_PyObject_LookupAttrId(obj, &PyId___conform__, &adapter) < 0) {
124 return NULL;
125 }
126 if (adapter) {
Petr Viktorinffd97532020-02-11 17:46:57 +0100127 adapted = PyObject_CallOneArg(adapter, proto);
Serhiy Storchakafc662ac2018-12-10 16:06:08 +0200128 Py_DECREF(adapter);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200129
Serhiy Storchakafc662ac2018-12-10 16:06:08 +0200130 if (adapted == Py_None) {
131 Py_DECREF(adapted);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000132 }
Serhiy Storchakafc662ac2018-12-10 16:06:08 +0200133 else if (adapted || !PyErr_ExceptionMatches(PyExc_TypeError)) {
134 return adapted;
135 }
136 else {
137 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000138 }
139 }
140
Serhiy Storchakafc662ac2018-12-10 16:06:08 +0200141 if (alt) {
142 Py_INCREF(alt);
143 return alt;
144 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000145 /* else set the right exception and return NULL */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000146 PyErr_SetString(pysqlite_ProgrammingError, "can't adapt");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000147 return NULL;
148}
149
150/** module-level functions **/
151
152PyObject *
Benjamin Petersond7b03282008-09-13 15:58:53 +0000153pysqlite_adapt(pysqlite_Cursor *self, PyObject *args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000154{
155 PyObject *obj, *alt = NULL;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000156 PyObject *proto = (PyObject*)&pysqlite_PrepareProtocolType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000157
158 if (!PyArg_ParseTuple(args, "O|OO", &obj, &proto, &alt)) return NULL;
Benjamin Petersond7b03282008-09-13 15:58:53 +0000159 return pysqlite_microprotocols_adapt(obj, proto, alt);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000160}