microprotocols.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #include <Python.h>
  26. #include <structmember.h>
  27. #include "cursor.h"
  28. #include "microprotocols.h"
  29. #include "prepare_protocol.h"
  30. /** the adapters registry **/
  31. PyObject *psyco_adapters;
  32. /* pysqlite_microprotocols_init - initialize the adapters dictionary */
  33. int
  34. pysqlite_microprotocols_init(PyObject *dict)
  35. {
  36. /* create adapters dictionary and put it in module namespace */
  37. if ((psyco_adapters = PyDict_New()) == NULL) {
  38. return -1;
  39. }
  40. return PyDict_SetItemString(dict, "adapters", psyco_adapters);
  41. }
  42. /* pysqlite_microprotocols_add - add a reverse type-caster to the dictionary */
  43. int
  44. pysqlite_microprotocols_add(PyTypeObject *type, PyObject *proto, PyObject *cast)
  45. {
  46. PyObject* key;
  47. int rc;
  48. if (proto == NULL) proto = (PyObject*)&pysqlite_PrepareProtocolType;
  49. key = Py_BuildValue("(OO)", (PyObject*)type, proto);
  50. if (!key) {
  51. return -1;
  52. }
  53. rc = PyDict_SetItem(psyco_adapters, key, cast);
  54. Py_DECREF(key);
  55. return rc;
  56. }
  57. /* pysqlite_microprotocols_adapt - adapt an object to the built-in protocol */
  58. PyObject *
  59. pysqlite_microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt)
  60. {
  61. PyObject *adapter, *key;
  62. /* we don't check for exact type conformance as specified in PEP 246
  63. because the pysqlite_PrepareProtocolType type is abstract and there is no
  64. way to get a quotable object to be its instance */
  65. /* look for an adapter in the registry */
  66. key = Py_BuildValue("(OO)", (PyObject*)obj->ob_type, proto);
  67. if (!key) {
  68. return NULL;
  69. }
  70. adapter = PyDict_GetItem(psyco_adapters, key);
  71. Py_DECREF(key);
  72. if (adapter) {
  73. PyObject *adapted = PyObject_CallFunctionObjArgs(adapter, obj, NULL);
  74. return adapted;
  75. }
  76. /* try to have the protocol adapt this object*/
  77. if (PyObject_HasAttrString(proto, "__adapt__")) {
  78. PyObject *adapted = PyObject_CallMethod(proto, "__adapt__", "O", obj);
  79. if (adapted) {
  80. if (adapted != Py_None) {
  81. return adapted;
  82. } else {
  83. Py_DECREF(adapted);
  84. }
  85. }
  86. if (PyErr_Occurred() && !PyErr_ExceptionMatches(PyExc_TypeError))
  87. return NULL;
  88. }
  89. /* and finally try to have the object adapt itself */
  90. if (PyObject_HasAttrString(obj, "__conform__")) {
  91. PyObject *adapted = PyObject_CallMethod(obj, "__conform__","O", proto);
  92. if (adapted) {
  93. if (adapted != Py_None) {
  94. return adapted;
  95. } else {
  96. Py_DECREF(adapted);
  97. }
  98. }
  99. if (PyErr_Occurred() && !PyErr_ExceptionMatches(PyExc_TypeError)) {
  100. return NULL;
  101. }
  102. }
  103. /* else set the right exception and return NULL */
  104. PyErr_SetString(pysqlite_ProgrammingError, "can't adapt");
  105. return NULL;
  106. }
  107. /** module-level functions **/
  108. PyObject *
  109. pysqlite_adapt(pysqlite_Cursor *self, PyObject *args)
  110. {
  111. PyObject *obj, *alt = NULL;
  112. PyObject *proto = (PyObject*)&pysqlite_PrepareProtocolType;
  113. if (!PyArg_ParseTuple(args, "O|OO", &obj, &proto, &alt)) return NULL;
  114. return pysqlite_microprotocols_adapt(obj, proto, alt);
  115. }