2006-04-21 07:40:58 -03:00
|
|
|
/* microprotocols.c - minimalist and non-validating protocols implementation
|
|
|
|
*
|
|
|
|
* Copyright (C) 2003-2004 Federico Di Gregorio <fog@debian.org>
|
|
|
|
*
|
|
|
|
* This file is part of psycopg and was adapted for pysqlite. Federico Di
|
|
|
|
* Gregorio gave the permission to use it within pysqlite under the following
|
|
|
|
* license:
|
|
|
|
*
|
|
|
|
* This software is provided 'as-is', without any express or implied
|
|
|
|
* warranty. In no event will the authors be held liable for any damages
|
|
|
|
* arising from the use of this software.
|
|
|
|
*
|
|
|
|
* Permission is granted to anyone to use this software for any purpose,
|
|
|
|
* including commercial applications, and to alter it and redistribute it
|
|
|
|
* freely, subject to the following restrictions:
|
|
|
|
*
|
|
|
|
* 1. The origin of this software must not be misrepresented; you must not
|
|
|
|
* claim that you wrote the original software. If you use this software
|
|
|
|
* in a product, an acknowledgment in the product documentation would be
|
|
|
|
* appreciated but is not required.
|
|
|
|
* 2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
* misrepresented as being the original software.
|
|
|
|
* 3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Python.h>
|
|
|
|
|
|
|
|
#include "cursor.h"
|
|
|
|
#include "microprotocols.h"
|
|
|
|
#include "prepare_protocol.h"
|
|
|
|
|
|
|
|
|
Merged revisions 66394,66404,66412,66414,66424-66436 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r66394 | benjamin.peterson | 2008-09-11 17:04:02 -0500 (Thu, 11 Sep 2008) | 1 line
fix typo
........
r66404 | gerhard.haering | 2008-09-12 08:54:06 -0500 (Fri, 12 Sep 2008) | 2 lines
sqlite3 module: Mark iterdump() method as "Non-standard" like all the other methods not found in DB-API.
........
r66412 | gerhard.haering | 2008-09-12 13:58:57 -0500 (Fri, 12 Sep 2008) | 2 lines
Fixes issue #3103. In the sqlite3 module, made one more function static. All renaming public symbos now have the pysqlite prefix to avoid name clashes. This at least once created problems where the same symbol name appeared somewhere in Apache and the sqlite3 module was used from mod_python.
........
r66414 | gerhard.haering | 2008-09-12 17:33:22 -0500 (Fri, 12 Sep 2008) | 2 lines
Issue #3846: Release GIL during calls to sqlite3_prepare. This improves concurrent access to the same database file from multiple threads/processes.
........
r66424 | andrew.kuchling | 2008-09-12 20:22:08 -0500 (Fri, 12 Sep 2008) | 1 line
#687648 from Robert Schuppenies: use classic division. (RM Barry gave permission to update the demos.)
........
r66425 | andrew.kuchling | 2008-09-12 20:27:33 -0500 (Fri, 12 Sep 2008) | 1 line
#687648 from Robert Schuppenies: use classic division. From me: don't use string exception; flush stdout after printing
........
r66426 | andrew.kuchling | 2008-09-12 20:34:41 -0500 (Fri, 12 Sep 2008) | 1 line
#687648 from Robert Schuppenies: use classic division. From me: don't use string exception; add __main__ section
........
r66427 | andrew.kuchling | 2008-09-12 20:42:55 -0500 (Fri, 12 Sep 2008) | 1 line
#687648 from Robert Schuppenies: use classic division. From me: remove two stray semicolons
........
r66428 | andrew.kuchling | 2008-09-12 20:43:28 -0500 (Fri, 12 Sep 2008) | 1 line
#687648 from Robert Schuppenies: use classic division.
........
r66429 | andrew.kuchling | 2008-09-12 20:47:02 -0500 (Fri, 12 Sep 2008) | 1 line
Remove semicolon
........
r66430 | andrew.kuchling | 2008-09-12 20:48:36 -0500 (Fri, 12 Sep 2008) | 1 line
Subclass exception
........
r66431 | andrew.kuchling | 2008-09-12 20:56:56 -0500 (Fri, 12 Sep 2008) | 1 line
Fix SyntaxError
........
r66432 | andrew.kuchling | 2008-09-12 20:57:25 -0500 (Fri, 12 Sep 2008) | 1 line
Update uses of string exceptions
........
r66433 | andrew.kuchling | 2008-09-12 21:08:30 -0500 (Fri, 12 Sep 2008) | 1 line
Use title case
........
r66434 | andrew.kuchling | 2008-09-12 21:09:15 -0500 (Fri, 12 Sep 2008) | 1 line
Remove extra 'the'; the following title includes it
........
r66435 | andrew.kuchling | 2008-09-12 21:11:51 -0500 (Fri, 12 Sep 2008) | 1 line
#3288: Document as_integer_ratio
........
r66436 | andrew.kuchling | 2008-09-12 21:14:15 -0500 (Fri, 12 Sep 2008) | 1 line
Use title case
........
2008-09-13 12:58:53 -03:00
|
|
|
/* pysqlite_microprotocols_init - initialize the adapters dictionary */
|
2006-04-21 07:40:58 -03:00
|
|
|
|
|
|
|
int
|
2020-10-15 09:20:15 -03:00
|
|
|
pysqlite_microprotocols_init(PyObject *module)
|
2006-04-21 07:40:58 -03:00
|
|
|
{
|
|
|
|
/* create adapters dictionary and put it in module namespace */
|
2021-07-20 07:59:18 -03:00
|
|
|
pysqlite_state *state = pysqlite_get_state(module);
|
|
|
|
state->psyco_adapters = PyDict_New();
|
|
|
|
if (state->psyco_adapters == NULL) {
|
2006-04-21 07:40:58 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-07-20 07:59:18 -03:00
|
|
|
return PyModule_AddObjectRef(module, "adapters", state->psyco_adapters);
|
2006-04-21 07:40:58 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
Merged revisions 66394,66404,66412,66414,66424-66436 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r66394 | benjamin.peterson | 2008-09-11 17:04:02 -0500 (Thu, 11 Sep 2008) | 1 line
fix typo
........
r66404 | gerhard.haering | 2008-09-12 08:54:06 -0500 (Fri, 12 Sep 2008) | 2 lines
sqlite3 module: Mark iterdump() method as "Non-standard" like all the other methods not found in DB-API.
........
r66412 | gerhard.haering | 2008-09-12 13:58:57 -0500 (Fri, 12 Sep 2008) | 2 lines
Fixes issue #3103. In the sqlite3 module, made one more function static. All renaming public symbos now have the pysqlite prefix to avoid name clashes. This at least once created problems where the same symbol name appeared somewhere in Apache and the sqlite3 module was used from mod_python.
........
r66414 | gerhard.haering | 2008-09-12 17:33:22 -0500 (Fri, 12 Sep 2008) | 2 lines
Issue #3846: Release GIL during calls to sqlite3_prepare. This improves concurrent access to the same database file from multiple threads/processes.
........
r66424 | andrew.kuchling | 2008-09-12 20:22:08 -0500 (Fri, 12 Sep 2008) | 1 line
#687648 from Robert Schuppenies: use classic division. (RM Barry gave permission to update the demos.)
........
r66425 | andrew.kuchling | 2008-09-12 20:27:33 -0500 (Fri, 12 Sep 2008) | 1 line
#687648 from Robert Schuppenies: use classic division. From me: don't use string exception; flush stdout after printing
........
r66426 | andrew.kuchling | 2008-09-12 20:34:41 -0500 (Fri, 12 Sep 2008) | 1 line
#687648 from Robert Schuppenies: use classic division. From me: don't use string exception; add __main__ section
........
r66427 | andrew.kuchling | 2008-09-12 20:42:55 -0500 (Fri, 12 Sep 2008) | 1 line
#687648 from Robert Schuppenies: use classic division. From me: remove two stray semicolons
........
r66428 | andrew.kuchling | 2008-09-12 20:43:28 -0500 (Fri, 12 Sep 2008) | 1 line
#687648 from Robert Schuppenies: use classic division.
........
r66429 | andrew.kuchling | 2008-09-12 20:47:02 -0500 (Fri, 12 Sep 2008) | 1 line
Remove semicolon
........
r66430 | andrew.kuchling | 2008-09-12 20:48:36 -0500 (Fri, 12 Sep 2008) | 1 line
Subclass exception
........
r66431 | andrew.kuchling | 2008-09-12 20:56:56 -0500 (Fri, 12 Sep 2008) | 1 line
Fix SyntaxError
........
r66432 | andrew.kuchling | 2008-09-12 20:57:25 -0500 (Fri, 12 Sep 2008) | 1 line
Update uses of string exceptions
........
r66433 | andrew.kuchling | 2008-09-12 21:08:30 -0500 (Fri, 12 Sep 2008) | 1 line
Use title case
........
r66434 | andrew.kuchling | 2008-09-12 21:09:15 -0500 (Fri, 12 Sep 2008) | 1 line
Remove extra 'the'; the following title includes it
........
r66435 | andrew.kuchling | 2008-09-12 21:11:51 -0500 (Fri, 12 Sep 2008) | 1 line
#3288: Document as_integer_ratio
........
r66436 | andrew.kuchling | 2008-09-12 21:14:15 -0500 (Fri, 12 Sep 2008) | 1 line
Use title case
........
2008-09-13 12:58:53 -03:00
|
|
|
/* pysqlite_microprotocols_add - add a reverse type-caster to the dictionary */
|
2006-04-21 07:40:58 -03:00
|
|
|
|
|
|
|
int
|
2021-10-27 08:12:21 -03:00
|
|
|
pysqlite_microprotocols_add(pysqlite_state *state, PyTypeObject *type,
|
|
|
|
PyObject *proto, PyObject *cast)
|
2006-04-21 07:40:58 -03:00
|
|
|
{
|
|
|
|
PyObject* key;
|
|
|
|
int rc;
|
|
|
|
|
2021-06-15 09:47:34 -03:00
|
|
|
assert(type != NULL);
|
|
|
|
assert(proto != NULL);
|
2022-06-15 05:42:49 -03:00
|
|
|
key = PyTuple_Pack(2, (PyObject *)type, proto);
|
2006-04-21 07:40:58 -03:00
|
|
|
if (!key) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-07-20 07:59:18 -03:00
|
|
|
rc = PyDict_SetItem(state->psyco_adapters, key, cast);
|
2006-04-21 07:40:58 -03:00
|
|
|
Py_DECREF(key);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
Merged revisions 66394,66404,66412,66414,66424-66436 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r66394 | benjamin.peterson | 2008-09-11 17:04:02 -0500 (Thu, 11 Sep 2008) | 1 line
fix typo
........
r66404 | gerhard.haering | 2008-09-12 08:54:06 -0500 (Fri, 12 Sep 2008) | 2 lines
sqlite3 module: Mark iterdump() method as "Non-standard" like all the other methods not found in DB-API.
........
r66412 | gerhard.haering | 2008-09-12 13:58:57 -0500 (Fri, 12 Sep 2008) | 2 lines
Fixes issue #3103. In the sqlite3 module, made one more function static. All renaming public symbos now have the pysqlite prefix to avoid name clashes. This at least once created problems where the same symbol name appeared somewhere in Apache and the sqlite3 module was used from mod_python.
........
r66414 | gerhard.haering | 2008-09-12 17:33:22 -0500 (Fri, 12 Sep 2008) | 2 lines
Issue #3846: Release GIL during calls to sqlite3_prepare. This improves concurrent access to the same database file from multiple threads/processes.
........
r66424 | andrew.kuchling | 2008-09-12 20:22:08 -0500 (Fri, 12 Sep 2008) | 1 line
#687648 from Robert Schuppenies: use classic division. (RM Barry gave permission to update the demos.)
........
r66425 | andrew.kuchling | 2008-09-12 20:27:33 -0500 (Fri, 12 Sep 2008) | 1 line
#687648 from Robert Schuppenies: use classic division. From me: don't use string exception; flush stdout after printing
........
r66426 | andrew.kuchling | 2008-09-12 20:34:41 -0500 (Fri, 12 Sep 2008) | 1 line
#687648 from Robert Schuppenies: use classic division. From me: don't use string exception; add __main__ section
........
r66427 | andrew.kuchling | 2008-09-12 20:42:55 -0500 (Fri, 12 Sep 2008) | 1 line
#687648 from Robert Schuppenies: use classic division. From me: remove two stray semicolons
........
r66428 | andrew.kuchling | 2008-09-12 20:43:28 -0500 (Fri, 12 Sep 2008) | 1 line
#687648 from Robert Schuppenies: use classic division.
........
r66429 | andrew.kuchling | 2008-09-12 20:47:02 -0500 (Fri, 12 Sep 2008) | 1 line
Remove semicolon
........
r66430 | andrew.kuchling | 2008-09-12 20:48:36 -0500 (Fri, 12 Sep 2008) | 1 line
Subclass exception
........
r66431 | andrew.kuchling | 2008-09-12 20:56:56 -0500 (Fri, 12 Sep 2008) | 1 line
Fix SyntaxError
........
r66432 | andrew.kuchling | 2008-09-12 20:57:25 -0500 (Fri, 12 Sep 2008) | 1 line
Update uses of string exceptions
........
r66433 | andrew.kuchling | 2008-09-12 21:08:30 -0500 (Fri, 12 Sep 2008) | 1 line
Use title case
........
r66434 | andrew.kuchling | 2008-09-12 21:09:15 -0500 (Fri, 12 Sep 2008) | 1 line
Remove extra 'the'; the following title includes it
........
r66435 | andrew.kuchling | 2008-09-12 21:11:51 -0500 (Fri, 12 Sep 2008) | 1 line
#3288: Document as_integer_ratio
........
r66436 | andrew.kuchling | 2008-09-12 21:14:15 -0500 (Fri, 12 Sep 2008) | 1 line
Use title case
........
2008-09-13 12:58:53 -03:00
|
|
|
/* pysqlite_microprotocols_adapt - adapt an object to the built-in protocol */
|
2006-04-21 07:40:58 -03:00
|
|
|
|
|
|
|
PyObject *
|
2021-07-29 06:21:45 -03:00
|
|
|
pysqlite_microprotocols_adapt(pysqlite_state *state, PyObject *obj,
|
|
|
|
PyObject *proto, PyObject *alt)
|
2006-04-21 07:40:58 -03:00
|
|
|
{
|
2018-12-10 10:06:08 -04:00
|
|
|
PyObject *adapter, *key, *adapted;
|
2006-04-21 07:40:58 -03:00
|
|
|
|
|
|
|
/* we don't check for exact type conformance as specified in PEP 246
|
2021-06-15 09:47:34 -03:00
|
|
|
because the PrepareProtocolType type is abstract and there is no
|
2006-04-21 07:40:58 -03:00
|
|
|
way to get a quotable object to be its instance */
|
|
|
|
|
|
|
|
/* look for an adapter in the registry */
|
2022-06-15 05:42:49 -03:00
|
|
|
key = PyTuple_Pack(2, (PyObject *)Py_TYPE(obj), proto);
|
2006-04-21 07:40:58 -03:00
|
|
|
if (!key) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2023-11-10 16:49:24 -04:00
|
|
|
if (PyDict_GetItemRef(state->psyco_adapters, key, &adapter) < 0) {
|
|
|
|
Py_DECREF(key);
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-04-21 07:40:58 -03:00
|
|
|
Py_DECREF(key);
|
|
|
|
if (adapter) {
|
2020-02-11 12:46:57 -04:00
|
|
|
adapted = PyObject_CallOneArg(adapter, obj);
|
2018-12-10 10:06:08 -04:00
|
|
|
Py_DECREF(adapter);
|
2006-04-21 07:40:58 -03:00
|
|
|
return adapted;
|
|
|
|
}
|
|
|
|
|
2018-12-10 10:06:08 -04:00
|
|
|
/* try to have the protocol adapt this object */
|
2023-07-12 02:57:10 -03:00
|
|
|
if (PyObject_GetOptionalAttr(proto, state->str___adapt__, &adapter) < 0) {
|
2018-12-10 10:06:08 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (adapter) {
|
2020-02-11 12:46:57 -04:00
|
|
|
adapted = PyObject_CallOneArg(adapter, obj);
|
2018-12-10 10:06:08 -04:00
|
|
|
Py_DECREF(adapter);
|
2006-04-21 07:40:58 -03:00
|
|
|
|
2018-12-10 10:06:08 -04:00
|
|
|
if (adapted == Py_None) {
|
|
|
|
Py_DECREF(adapted);
|
|
|
|
}
|
|
|
|
else if (adapted || !PyErr_ExceptionMatches(PyExc_TypeError)) {
|
|
|
|
return adapted;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
2006-04-21 07:40:58 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* and finally try to have the object adapt itself */
|
2023-07-12 02:57:10 -03:00
|
|
|
if (PyObject_GetOptionalAttr(obj, state->str___conform__, &adapter) < 0) {
|
2018-12-10 10:06:08 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (adapter) {
|
2020-02-11 12:46:57 -04:00
|
|
|
adapted = PyObject_CallOneArg(adapter, proto);
|
2018-12-10 10:06:08 -04:00
|
|
|
Py_DECREF(adapter);
|
2006-04-21 07:40:58 -03:00
|
|
|
|
2018-12-10 10:06:08 -04:00
|
|
|
if (adapted == Py_None) {
|
|
|
|
Py_DECREF(adapted);
|
|
|
|
}
|
|
|
|
else if (adapted || !PyErr_ExceptionMatches(PyExc_TypeError)) {
|
|
|
|
return adapted;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_Clear();
|
2006-04-21 07:40:58 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-10 10:06:08 -04:00
|
|
|
if (alt) {
|
2020-12-27 07:05:33 -04:00
|
|
|
return Py_NewRef(alt);
|
2018-12-10 10:06:08 -04:00
|
|
|
}
|
2006-04-21 07:40:58 -03:00
|
|
|
/* else set the right exception and return NULL */
|
2021-07-14 08:26:44 -03:00
|
|
|
PyErr_SetString(state->ProgrammingError, "can't adapt");
|
2006-04-21 07:40:58 -03:00
|
|
|
return NULL;
|
|
|
|
}
|