blob: 5040acdc0decc383e711b5513fa13e31b3dd1c44 [file] [log] [blame]
Anthony Baxterc51ee692006-04-01 00:57:31 +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
36PyObject *psyco_adapters;
37
38/* microprotocols_init - initialize the adapters dictionary */
39
40int
41microprotocols_init(PyObject *dict)
42{
43 /* create adapters dictionary and put it in module namespace */
44 if ((psyco_adapters = PyDict_New()) == NULL) {
45 return -1;
46 }
47
Anthony Baxter72289a62006-04-04 06:29:05 +000048 return PyDict_SetItemString(dict, "adapters", psyco_adapters);
Anthony Baxterc51ee692006-04-01 00:57:31 +000049}
50
51
52/* microprotocols_add - add a reverse type-caster to the dictionary */
53
54int
55microprotocols_add(PyTypeObject *type, PyObject *proto, PyObject *cast)
56{
57 PyObject* key;
58
59 if (proto == NULL) proto = (PyObject*)&SQLitePrepareProtocolType;
60
61 /*
62 Dprintf("microprotocols_add: cast %p for (%s, ?)",
63 cast, type->tp_name);
64 */
65
Anthony Baxter72289a62006-04-04 06:29:05 +000066
Anthony Baxterc51ee692006-04-01 00:57:31 +000067 key = Py_BuildValue("(OO)", (PyObject*)type, proto);
Anthony Baxter72289a62006-04-04 06:29:05 +000068 if (!key) {
69 return -1;
70 }
71
72 if (PyDict_SetItem(psyco_adapters, key, cast) != 0) {
73 Py_DECREF(key);
74 return -1;
75 }
76
Anthony Baxterc51ee692006-04-01 00:57:31 +000077 Py_DECREF(key);
78
79 return 0;
80}
81
82/* microprotocols_adapt - adapt an object to the built-in protocol */
83
84PyObject *
85microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt)
86{
87 PyObject *adapter, *key;
88
89 /* we don't check for exact type conformance as specified in PEP 246
90 because the SQLitePrepareProtocolType type is abstract and there is no
91 way to get a quotable object to be its instance */
92
93 /* look for an adapter in the registry */
94 key = Py_BuildValue("(OO)", (PyObject*)obj->ob_type, proto);
Anthony Baxter72289a62006-04-04 06:29:05 +000095 if (!key) {
96 return NULL;
97 }
Anthony Baxterc51ee692006-04-01 00:57:31 +000098 adapter = PyDict_GetItem(psyco_adapters, key);
99 Py_DECREF(key);
100 if (adapter) {
101 PyObject *adapted = PyObject_CallFunctionObjArgs(adapter, obj, NULL);
102 return adapted;
103 }
104
105 /* try to have the protocol adapt this object*/
106 if (PyObject_HasAttrString(proto, "__adapt__")) {
107 PyObject *adapted = PyObject_CallMethod(proto, "__adapt__", "O", obj);
108 if (adapted) {
109 if (adapted != Py_None) {
110 return adapted;
111 } else {
112 Py_DECREF(adapted);
113 }
114 }
115
116 if (PyErr_Occurred() && !PyErr_ExceptionMatches(PyExc_TypeError))
117 return NULL;
118 }
119
120 /* and finally try to have the object adapt itself */
121 if (PyObject_HasAttrString(obj, "__conform__")) {
122 PyObject *adapted = PyObject_CallMethod(obj, "__conform__","O", proto);
123 if (adapted) {
124 if (adapted != Py_None) {
125 return adapted;
126 } else {
127 Py_DECREF(adapted);
128 }
129 }
130
131 if (PyErr_Occurred() && !PyErr_ExceptionMatches(PyExc_TypeError)) {
132 return NULL;
133 }
134 }
135
136 /* else set the right exception and return NULL */
137 PyErr_SetString(ProgrammingError, "can't adapt");
138 return NULL;
139}
140
141/** module-level functions **/
142
143PyObject *
144psyco_microprotocols_adapt(Cursor *self, PyObject *args)
145{
146 PyObject *obj, *alt = NULL;
147 PyObject *proto = (PyObject*)&SQLitePrepareProtocolType;
148
149 if (!PyArg_ParseTuple(args, "O|OO", &obj, &proto, &alt)) return NULL;
150 return microprotocols_adapt(obj, proto, alt);
151}