microprotocols.c 4.2 KB

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