Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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> |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 27 | |
| 28 | #include "cursor.h" |
| 29 | #include "microprotocols.h" |
| 30 | #include "prepare_protocol.h" |
| 31 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 32 | /** the adapters registry **/ |
| 33 | |
Benjamin Peterson | 0a37a30 | 2017-12-31 10:04:13 -0800 | [diff] [blame] | 34 | static PyObject *psyco_adapters = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 35 | |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 36 | /* pysqlite_microprotocols_init - initialize the adapters dictionary */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 37 | |
| 38 | int |
Erlend Egeberg Aasland | 644e942 | 2020-10-15 14:20:15 +0200 | [diff] [blame] | 39 | pysqlite_microprotocols_init(PyObject *module) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 40 | { |
| 41 | /* create adapters dictionary and put it in module namespace */ |
| 42 | if ((psyco_adapters = PyDict_New()) == NULL) { |
| 43 | return -1; |
| 44 | } |
| 45 | |
Erlend Egeberg Aasland | 644e942 | 2020-10-15 14:20:15 +0200 | [diff] [blame] | 46 | if (PyModule_AddObject(module, "adapters", psyco_adapters) < 0) { |
| 47 | Py_DECREF(psyco_adapters); |
| 48 | return -1; |
| 49 | } |
| 50 | |
| 51 | return 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 55 | /* pysqlite_microprotocols_add - add a reverse type-caster to the dictionary */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 56 | |
| 57 | int |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 58 | pysqlite_microprotocols_add(PyTypeObject *type, PyObject *proto, PyObject *cast) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 59 | { |
| 60 | PyObject* key; |
| 61 | int rc; |
| 62 | |
Erlend Egeberg Aasland | cb6db8b | 2020-09-29 00:05:04 +0200 | [diff] [blame] | 63 | if (proto == NULL) proto = (PyObject*)pysqlite_PrepareProtocolType; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 64 | |
| 65 | key = Py_BuildValue("(OO)", (PyObject*)type, proto); |
| 66 | if (!key) { |
| 67 | return -1; |
| 68 | } |
| 69 | |
| 70 | rc = PyDict_SetItem(psyco_adapters, key, cast); |
| 71 | Py_DECREF(key); |
| 72 | |
| 73 | return rc; |
| 74 | } |
| 75 | |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 76 | /* pysqlite_microprotocols_adapt - adapt an object to the built-in protocol */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 77 | |
| 78 | PyObject * |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 79 | pysqlite_microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 80 | { |
Serhiy Storchaka | fc662ac | 2018-12-10 16:06:08 +0200 | [diff] [blame] | 81 | _Py_IDENTIFIER(__adapt__); |
| 82 | _Py_IDENTIFIER(__conform__); |
| 83 | PyObject *adapter, *key, *adapted; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 84 | |
| 85 | /* we don't check for exact type conformance as specified in PEP 246 |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 86 | because the pysqlite_PrepareProtocolType type is abstract and there is no |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 87 | way to get a quotable object to be its instance */ |
| 88 | |
| 89 | /* look for an adapter in the registry */ |
Victor Stinner | daa9756 | 2020-02-07 03:37:06 +0100 | [diff] [blame] | 90 | key = Py_BuildValue("(OO)", (PyObject*)Py_TYPE(obj), proto); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 91 | if (!key) { |
| 92 | return NULL; |
| 93 | } |
Serhiy Storchaka | fc662ac | 2018-12-10 16:06:08 +0200 | [diff] [blame] | 94 | adapter = PyDict_GetItemWithError(psyco_adapters, key); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 95 | Py_DECREF(key); |
| 96 | if (adapter) { |
Serhiy Storchaka | fc662ac | 2018-12-10 16:06:08 +0200 | [diff] [blame] | 97 | Py_INCREF(adapter); |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 98 | adapted = PyObject_CallOneArg(adapter, obj); |
Serhiy Storchaka | fc662ac | 2018-12-10 16:06:08 +0200 | [diff] [blame] | 99 | Py_DECREF(adapter); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 100 | return adapted; |
| 101 | } |
Serhiy Storchaka | fc662ac | 2018-12-10 16:06:08 +0200 | [diff] [blame] | 102 | if (PyErr_Occurred()) { |
| 103 | return NULL; |
| 104 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 105 | |
Serhiy Storchaka | fc662ac | 2018-12-10 16:06:08 +0200 | [diff] [blame] | 106 | /* try to have the protocol adapt this object */ |
| 107 | if (_PyObject_LookupAttrId(proto, &PyId___adapt__, &adapter) < 0) { |
| 108 | return NULL; |
| 109 | } |
| 110 | if (adapter) { |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 111 | adapted = PyObject_CallOneArg(adapter, obj); |
Serhiy Storchaka | fc662ac | 2018-12-10 16:06:08 +0200 | [diff] [blame] | 112 | Py_DECREF(adapter); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 113 | |
Serhiy Storchaka | fc662ac | 2018-12-10 16:06:08 +0200 | [diff] [blame] | 114 | if (adapted == Py_None) { |
| 115 | Py_DECREF(adapted); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 116 | } |
Serhiy Storchaka | fc662ac | 2018-12-10 16:06:08 +0200 | [diff] [blame] | 117 | else if (adapted || !PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 118 | return adapted; |
| 119 | } |
| 120 | else { |
| 121 | PyErr_Clear(); |
| 122 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /* and finally try to have the object adapt itself */ |
Serhiy Storchaka | fc662ac | 2018-12-10 16:06:08 +0200 | [diff] [blame] | 126 | if (_PyObject_LookupAttrId(obj, &PyId___conform__, &adapter) < 0) { |
| 127 | return NULL; |
| 128 | } |
| 129 | if (adapter) { |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 130 | adapted = PyObject_CallOneArg(adapter, proto); |
Serhiy Storchaka | fc662ac | 2018-12-10 16:06:08 +0200 | [diff] [blame] | 131 | Py_DECREF(adapter); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 132 | |
Serhiy Storchaka | fc662ac | 2018-12-10 16:06:08 +0200 | [diff] [blame] | 133 | if (adapted == Py_None) { |
| 134 | Py_DECREF(adapted); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 135 | } |
Serhiy Storchaka | fc662ac | 2018-12-10 16:06:08 +0200 | [diff] [blame] | 136 | else if (adapted || !PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 137 | return adapted; |
| 138 | } |
| 139 | else { |
| 140 | PyErr_Clear(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
Serhiy Storchaka | fc662ac | 2018-12-10 16:06:08 +0200 | [diff] [blame] | 144 | if (alt) { |
| 145 | Py_INCREF(alt); |
| 146 | return alt; |
| 147 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 148 | /* else set the right exception and return NULL */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 149 | PyErr_SetString(pysqlite_ProgrammingError, "can't adapt"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 150 | return NULL; |
| 151 | } |