blob: 3b2d7f42b87353d1f541db84b27e9219aaa3506c [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>
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000027
28#include "cursor.h"
29#include "microprotocols.h"
30#include "prepare_protocol.h"
31
32
33/** the adapters registry **/
34
Benjamin Peterson0a37a302017-12-31 10:04:13 -080035static PyObject *psyco_adapters = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000036
Benjamin Petersond7b03282008-09-13 15:58:53 +000037/* pysqlite_microprotocols_init - initialize the adapters dictionary */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000038
39int
Benjamin Petersond7b03282008-09-13 15:58:53 +000040pysqlite_microprotocols_init(PyObject *dict)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000041{
42 /* create adapters dictionary and put it in module namespace */
43 if ((psyco_adapters = PyDict_New()) == NULL) {
44 return -1;
45 }
46
47 return PyDict_SetItemString(dict, "adapters", psyco_adapters);
48}
49
50
Benjamin Petersond7b03282008-09-13 15:58:53 +000051/* pysqlite_microprotocols_add - add a reverse type-caster to the dictionary */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000052
53int
Benjamin Petersond7b03282008-09-13 15:58:53 +000054pysqlite_microprotocols_add(PyTypeObject *type, PyObject *proto, PyObject *cast)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000055{
56 PyObject* key;
57 int rc;
58
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000059 if (proto == NULL) proto = (PyObject*)&pysqlite_PrepareProtocolType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000060
61 key = Py_BuildValue("(OO)", (PyObject*)type, proto);
62 if (!key) {
63 return -1;
64 }
65
66 rc = PyDict_SetItem(psyco_adapters, key, cast);
67 Py_DECREF(key);
68
69 return rc;
70}
71
Benjamin Petersond7b03282008-09-13 15:58:53 +000072/* pysqlite_microprotocols_adapt - adapt an object to the built-in protocol */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000073
74PyObject *
Benjamin Petersond7b03282008-09-13 15:58:53 +000075pysqlite_microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000076{
Serhiy Storchakafc662ac2018-12-10 16:06:08 +020077 _Py_IDENTIFIER(__adapt__);
78 _Py_IDENTIFIER(__conform__);
79 PyObject *adapter, *key, *adapted;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000080
81 /* we don't check for exact type conformance as specified in PEP 246
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000082 because the pysqlite_PrepareProtocolType type is abstract and there is no
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000083 way to get a quotable object to be its instance */
84
85 /* look for an adapter in the registry */
Victor Stinnerdaa97562020-02-07 03:37:06 +010086 key = Py_BuildValue("(OO)", (PyObject*)Py_TYPE(obj), proto);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000087 if (!key) {
88 return NULL;
89 }
Serhiy Storchakafc662ac2018-12-10 16:06:08 +020090 adapter = PyDict_GetItemWithError(psyco_adapters, key);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000091 Py_DECREF(key);
92 if (adapter) {
Serhiy Storchakafc662ac2018-12-10 16:06:08 +020093 Py_INCREF(adapter);
Petr Viktorinffd97532020-02-11 17:46:57 +010094 adapted = PyObject_CallOneArg(adapter, obj);
Serhiy Storchakafc662ac2018-12-10 16:06:08 +020095 Py_DECREF(adapter);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000096 return adapted;
97 }
Serhiy Storchakafc662ac2018-12-10 16:06:08 +020098 if (PyErr_Occurred()) {
99 return NULL;
100 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000101
Serhiy Storchakafc662ac2018-12-10 16:06:08 +0200102 /* try to have the protocol adapt this object */
103 if (_PyObject_LookupAttrId(proto, &PyId___adapt__, &adapter) < 0) {
104 return NULL;
105 }
106 if (adapter) {
Petr Viktorinffd97532020-02-11 17:46:57 +0100107 adapted = PyObject_CallOneArg(adapter, obj);
Serhiy Storchakafc662ac2018-12-10 16:06:08 +0200108 Py_DECREF(adapter);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200109
Serhiy Storchakafc662ac2018-12-10 16:06:08 +0200110 if (adapted == Py_None) {
111 Py_DECREF(adapted);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000112 }
Serhiy Storchakafc662ac2018-12-10 16:06:08 +0200113 else if (adapted || !PyErr_ExceptionMatches(PyExc_TypeError)) {
114 return adapted;
115 }
116 else {
117 PyErr_Clear();
118 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000119 }
120
121 /* and finally try to have the object adapt itself */
Serhiy Storchakafc662ac2018-12-10 16:06:08 +0200122 if (_PyObject_LookupAttrId(obj, &PyId___conform__, &adapter) < 0) {
123 return NULL;
124 }
125 if (adapter) {
Petr Viktorinffd97532020-02-11 17:46:57 +0100126 adapted = PyObject_CallOneArg(adapter, proto);
Serhiy Storchakafc662ac2018-12-10 16:06:08 +0200127 Py_DECREF(adapter);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200128
Serhiy Storchakafc662ac2018-12-10 16:06:08 +0200129 if (adapted == Py_None) {
130 Py_DECREF(adapted);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000131 }
Serhiy Storchakafc662ac2018-12-10 16:06:08 +0200132 else if (adapted || !PyErr_ExceptionMatches(PyExc_TypeError)) {
133 return adapted;
134 }
135 else {
136 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000137 }
138 }
139
Serhiy Storchakafc662ac2018-12-10 16:06:08 +0200140 if (alt) {
141 Py_INCREF(alt);
142 return alt;
143 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000144 /* else set the right exception and return NULL */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000145 PyErr_SetString(pysqlite_ProgrammingError, "can't adapt");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000146 return NULL;
147}
148
149/** module-level functions **/
150
151PyObject *
Benjamin Petersond7b03282008-09-13 15:58:53 +0000152pysqlite_adapt(pysqlite_Cursor *self, PyObject *args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000153{
154 PyObject *obj, *alt = NULL;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000155 PyObject *proto = (PyObject*)&pysqlite_PrepareProtocolType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000156
157 if (!PyArg_ParseTuple(args, "O|OO", &obj, &proto, &alt)) return NULL;
Benjamin Petersond7b03282008-09-13 15:58:53 +0000158 return pysqlite_microprotocols_adapt(obj, proto, alt);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000159}