• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* 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 
28 #include "cursor.h"
29 #include "microprotocols.h"
30 #include "prepare_protocol.h"
31 
32 
33 /* pysqlite_microprotocols_init - initialize the adapters dictionary */
34 
35 int
pysqlite_microprotocols_init(PyObject * module)36 pysqlite_microprotocols_init(PyObject *module)
37 {
38     /* create adapters dictionary and put it in module namespace */
39     pysqlite_state *state = pysqlite_get_state(module);
40     state->psyco_adapters = PyDict_New();
41     if (state->psyco_adapters == NULL) {
42         return -1;
43     }
44 
45     return PyModule_AddObjectRef(module, "adapters", state->psyco_adapters);
46 }
47 
48 
49 /* pysqlite_microprotocols_add - add a reverse type-caster to the dictionary */
50 
51 int
pysqlite_microprotocols_add(pysqlite_state * state,PyTypeObject * type,PyObject * proto,PyObject * cast)52 pysqlite_microprotocols_add(pysqlite_state *state, PyTypeObject *type,
53                             PyObject *proto, PyObject *cast)
54 {
55     PyObject* key;
56     int rc;
57 
58     assert(type != NULL);
59     assert(proto != NULL);
60     key = PyTuple_Pack(2, (PyObject *)type, proto);
61     if (!key) {
62         return -1;
63     }
64 
65     rc = PyDict_SetItem(state->psyco_adapters, key, cast);
66     Py_DECREF(key);
67 
68     return rc;
69 }
70 
71 /* pysqlite_microprotocols_adapt - adapt an object to the built-in protocol */
72 
73 PyObject *
pysqlite_microprotocols_adapt(pysqlite_state * state,PyObject * obj,PyObject * proto,PyObject * alt)74 pysqlite_microprotocols_adapt(pysqlite_state *state, PyObject *obj,
75                               PyObject *proto, PyObject *alt)
76 {
77     PyObject *adapter, *key, *adapted;
78 
79     /* we don't check for exact type conformance as specified in PEP 246
80        because the PrepareProtocolType type is abstract and there is no
81        way to get a quotable object to be its instance */
82 
83     /* look for an adapter in the registry */
84     key = PyTuple_Pack(2, (PyObject *)Py_TYPE(obj), proto);
85     if (!key) {
86         return NULL;
87     }
88     if (PyDict_GetItemRef(state->psyco_adapters, key, &adapter) < 0) {
89         Py_DECREF(key);
90         return NULL;
91     }
92     Py_DECREF(key);
93     if (adapter) {
94         adapted = PyObject_CallOneArg(adapter, obj);
95         Py_DECREF(adapter);
96         return adapted;
97     }
98 
99     /* try to have the protocol adapt this object */
100     if (PyObject_GetOptionalAttr(proto, state->str___adapt__, &adapter) < 0) {
101         return NULL;
102     }
103     if (adapter) {
104         adapted = PyObject_CallOneArg(adapter, obj);
105         Py_DECREF(adapter);
106 
107         if (adapted == Py_None) {
108             Py_DECREF(adapted);
109         }
110         else if (adapted || !PyErr_ExceptionMatches(PyExc_TypeError)) {
111             return adapted;
112         }
113         else {
114             PyErr_Clear();
115         }
116     }
117 
118     /* and finally try to have the object adapt itself */
119     if (PyObject_GetOptionalAttr(obj, state->str___conform__, &adapter) < 0) {
120         return NULL;
121     }
122     if (adapter) {
123         adapted = PyObject_CallOneArg(adapter, proto);
124         Py_DECREF(adapter);
125 
126         if (adapted == Py_None) {
127             Py_DECREF(adapted);
128         }
129         else if (adapted || !PyErr_ExceptionMatches(PyExc_TypeError)) {
130             return adapted;
131         }
132         else {
133             PyErr_Clear();
134         }
135     }
136 
137     if (alt) {
138         return Py_NewRef(alt);
139     }
140     /* else set the right exception and return NULL */
141     PyErr_SetString(state->ProgrammingError, "can't adapt");
142     return NULL;
143 }
144