blob: 5df41a1901cb92e7bfcf831cc6d84977ed2659d1 [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
48 PyDict_SetItemString(dict, "adapters", psyco_adapters);
49
50 return 0;
51}
52
53
54/* microprotocols_add - add a reverse type-caster to the dictionary */
55
56int
57microprotocols_add(PyTypeObject *type, PyObject *proto, PyObject *cast)
58{
59 PyObject* key;
60
61 if (proto == NULL) proto = (PyObject*)&SQLitePrepareProtocolType;
62
63 /*
64 Dprintf("microprotocols_add: cast %p for (%s, ?)",
65 cast, type->tp_name);
66 */
67
68 key = Py_BuildValue("(OO)", (PyObject*)type, proto);
69 PyDict_SetItem(psyco_adapters, key, cast);
70 Py_DECREF(key);
71
72 return 0;
73}
74
75/* microprotocols_adapt - adapt an object to the built-in protocol */
76
77PyObject *
78microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt)
79{
80 PyObject *adapter, *key;
81
82 /* we don't check for exact type conformance as specified in PEP 246
83 because the SQLitePrepareProtocolType type is abstract and there is no
84 way to get a quotable object to be its instance */
85
86 /* look for an adapter in the registry */
87 key = Py_BuildValue("(OO)", (PyObject*)obj->ob_type, proto);
88 adapter = PyDict_GetItem(psyco_adapters, key);
89 Py_DECREF(key);
90 if (adapter) {
91 PyObject *adapted = PyObject_CallFunctionObjArgs(adapter, obj, NULL);
92 return adapted;
93 }
94
95 /* try to have the protocol adapt this object*/
96 if (PyObject_HasAttrString(proto, "__adapt__")) {
97 PyObject *adapted = PyObject_CallMethod(proto, "__adapt__", "O", obj);
98 if (adapted) {
99 if (adapted != Py_None) {
100 return adapted;
101 } else {
102 Py_DECREF(adapted);
103 }
104 }
105
106 if (PyErr_Occurred() && !PyErr_ExceptionMatches(PyExc_TypeError))
107 return NULL;
108 }
109
110 /* and finally try to have the object adapt itself */
111 if (PyObject_HasAttrString(obj, "__conform__")) {
112 PyObject *adapted = PyObject_CallMethod(obj, "__conform__","O", proto);
113 if (adapted) {
114 if (adapted != Py_None) {
115 return adapted;
116 } else {
117 Py_DECREF(adapted);
118 }
119 }
120
121 if (PyErr_Occurred() && !PyErr_ExceptionMatches(PyExc_TypeError)) {
122 return NULL;
123 }
124 }
125
126 /* else set the right exception and return NULL */
127 PyErr_SetString(ProgrammingError, "can't adapt");
128 return NULL;
129}
130
131/** module-level functions **/
132
133PyObject *
134psyco_microprotocols_adapt(Cursor *self, PyObject *args)
135{
136 PyObject *obj, *alt = NULL;
137 PyObject *proto = (PyObject*)&SQLitePrepareProtocolType;
138
139 if (!PyArg_ParseTuple(args, "O|OO", &obj, &proto, &alt)) return NULL;
140 return microprotocols_adapt(obj, proto, alt);
141}