util.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* util.c - various utility functions
  2. *
  3. * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de>
  4. *
  5. * This file is part of pysqlite.
  6. *
  7. * This software is provided 'as-is', without any express or implied
  8. * warranty. In no event will the authors be held liable for any damages
  9. * arising from the use of this software.
  10. *
  11. * Permission is granted to anyone to use this software for any purpose,
  12. * including commercial applications, and to alter it and redistribute it
  13. * freely, subject to the following restrictions:
  14. *
  15. * 1. The origin of this software must not be misrepresented; you must not
  16. * claim that you wrote the original software. If you use this software
  17. * in a product, an acknowledgment in the product documentation would be
  18. * appreciated but is not required.
  19. * 2. Altered source versions must be plainly marked as such, and must not be
  20. * misrepresented as being the original software.
  21. * 3. This notice may not be removed or altered from any source distribution.
  22. */
  23. #include "module.h"
  24. #include "connection.h"
  25. // Returns non-NULL if a new exception should be raised
  26. static PyObject *
  27. get_exception_class(pysqlite_state *state, int errorcode)
  28. {
  29. switch (errorcode) {
  30. case SQLITE_OK:
  31. PyErr_Clear();
  32. return NULL;
  33. case SQLITE_INTERNAL:
  34. case SQLITE_NOTFOUND:
  35. return state->InternalError;
  36. case SQLITE_NOMEM:
  37. return PyErr_NoMemory();
  38. case SQLITE_ERROR:
  39. case SQLITE_PERM:
  40. case SQLITE_ABORT:
  41. case SQLITE_BUSY:
  42. case SQLITE_LOCKED:
  43. case SQLITE_READONLY:
  44. case SQLITE_INTERRUPT:
  45. case SQLITE_IOERR:
  46. case SQLITE_FULL:
  47. case SQLITE_CANTOPEN:
  48. case SQLITE_PROTOCOL:
  49. case SQLITE_EMPTY:
  50. case SQLITE_SCHEMA:
  51. return state->OperationalError;
  52. case SQLITE_CORRUPT:
  53. return state->DatabaseError;
  54. case SQLITE_TOOBIG:
  55. return state->DataError;
  56. case SQLITE_CONSTRAINT:
  57. case SQLITE_MISMATCH:
  58. return state->IntegrityError;
  59. case SQLITE_MISUSE:
  60. case SQLITE_RANGE:
  61. return state->InterfaceError;
  62. default:
  63. return state->DatabaseError;
  64. }
  65. }
  66. static void
  67. raise_exception(PyObject *type, int errcode, const char *errmsg)
  68. {
  69. PyObject *exc = NULL;
  70. PyObject *args[] = { PyUnicode_FromString(errmsg), };
  71. if (args[0] == NULL) {
  72. goto exit;
  73. }
  74. exc = PyObject_Vectorcall(type, args, 1, NULL);
  75. Py_DECREF(args[0]);
  76. if (exc == NULL) {
  77. goto exit;
  78. }
  79. PyObject *code = PyLong_FromLong(errcode);
  80. if (code == NULL) {
  81. goto exit;
  82. }
  83. int rc = PyObject_SetAttrString(exc, "sqlite_errorcode", code);
  84. Py_DECREF(code);
  85. if (rc < 0) {
  86. goto exit;
  87. }
  88. const char *error_name = pysqlite_error_name(errcode);
  89. PyObject *name;
  90. if (error_name) {
  91. name = PyUnicode_FromString(error_name);
  92. }
  93. else {
  94. name = PyUnicode_InternFromString("unknown");
  95. }
  96. if (name == NULL) {
  97. goto exit;
  98. }
  99. rc = PyObject_SetAttrString(exc, "sqlite_errorname", name);
  100. Py_DECREF(name);
  101. if (rc < 0) {
  102. goto exit;
  103. }
  104. PyErr_SetObject(type, exc);
  105. exit:
  106. Py_XDECREF(exc);
  107. }
  108. /**
  109. * Checks the SQLite error code and sets the appropriate DB-API exception.
  110. * Returns the error code (0 means no error occurred).
  111. */
  112. int
  113. _pysqlite_seterror(pysqlite_state *state, sqlite3 *db)
  114. {
  115. int errorcode = sqlite3_errcode(db);
  116. PyObject *exc_class = get_exception_class(state, errorcode);
  117. if (exc_class == NULL) {
  118. // No new exception need be raised; just pass the error code
  119. return errorcode;
  120. }
  121. /* Create and set the exception. */
  122. int extended_errcode = sqlite3_extended_errcode(db);
  123. const char *errmsg = sqlite3_errmsg(db);
  124. raise_exception(exc_class, extended_errcode, errmsg);
  125. return extended_errcode;
  126. }
  127. #ifdef WORDS_BIGENDIAN
  128. # define IS_LITTLE_ENDIAN 0
  129. #else
  130. # define IS_LITTLE_ENDIAN 1
  131. #endif
  132. sqlite_int64
  133. _pysqlite_long_as_int64(PyObject * py_val)
  134. {
  135. int overflow;
  136. long long value = PyLong_AsLongLongAndOverflow(py_val, &overflow);
  137. if (value == -1 && PyErr_Occurred())
  138. return -1;
  139. if (!overflow) {
  140. # if SIZEOF_LONG_LONG > 8
  141. if (-0x8000000000000000LL <= value && value <= 0x7FFFFFFFFFFFFFFFLL)
  142. # endif
  143. return value;
  144. }
  145. else if (sizeof(value) < sizeof(sqlite_int64)) {
  146. sqlite_int64 int64val;
  147. if (_PyLong_AsByteArray((PyLongObject *)py_val,
  148. (unsigned char *)&int64val, sizeof(int64val),
  149. IS_LITTLE_ENDIAN, 1 /* signed */) >= 0) {
  150. return int64val;
  151. }
  152. }
  153. PyErr_SetString(PyExc_OverflowError,
  154. "Python int too large to convert to SQLite INTEGER");
  155. return -1;
  156. }