| 1 |
/* util.c - various utility functions |
| 2 |
* |
| 3 |
* Copyright (C) 2005-2010 Gerhard Häring <[email protected]> |
| 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 |
|
| 24 |
#ifndef Py_BUILD_CORE_BUILTIN |
| 25 |
# define Py_BUILD_CORE_MODULE 1 |
| 26 |
#endif |
| 27 |
|
| 28 |
#include "module.h" |
| 29 |
#include "pycore_long.h" // _PyLong_AsByteArray() |
| 30 |
#include "connection.h" |
| 31 |
|
| 32 |
// Returns non-NULL if a new exception should be raised |
| 33 |
static PyObject * |
| 34 |
get_exception_class(pysqlite_state *state, int errorcode) |
| 35 |
{ |
| 36 |
switch (errorcode) { |
| 37 |
case SQLITE_OK: |
| 38 |
PyErr_Clear(); |
| 39 |
return NULL; |
| 40 |
case SQLITE_INTERNAL: |
| 41 |
case SQLITE_NOTFOUND: |
| 42 |
return state->InternalError; |
| 43 |
case SQLITE_NOMEM: |
| 44 |
return PyErr_NoMemory(); |
| 45 |
case SQLITE_ERROR: |
| 46 |
case SQLITE_PERM: |
| 47 |
case SQLITE_ABORT: |
| 48 |
case SQLITE_BUSY: |
| 49 |
case SQLITE_LOCKED: |
| 50 |
case SQLITE_READONLY: |
| 51 |
case SQLITE_INTERRUPT: |
| 52 |
case SQLITE_IOERR: |
| 53 |
case SQLITE_FULL: |
| 54 |
case SQLITE_CANTOPEN: |
| 55 |
case SQLITE_PROTOCOL: |
| 56 |
case SQLITE_EMPTY: |
| 57 |
case SQLITE_SCHEMA: |
| 58 |
return state->OperationalError; |
| 59 |
case SQLITE_CORRUPT: |
| 60 |
return state->DatabaseError; |
| 61 |
case SQLITE_TOOBIG: |
| 62 |
return state->DataError; |
| 63 |
case SQLITE_CONSTRAINT: |
| 64 |
case SQLITE_MISMATCH: |
| 65 |
return state->IntegrityError; |
| 66 |
case SQLITE_MISUSE: |
| 67 |
case SQLITE_RANGE: |
| 68 |
return state->InterfaceError; |
| 69 |
default: |
| 70 |
return state->DatabaseError; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
static void |
| 75 |
raise_exception(PyObject *type, int errcode, const char *errmsg) |
| 76 |
{ |
| 77 |
PyObject *exc = NULL; |
| 78 |
PyObject *args[] = { PyUnicode_FromString(errmsg), }; |
| 79 |
if (args[0] == NULL) { |
| 80 |
goto exit; |
| 81 |
} |
| 82 |
exc = PyObject_Vectorcall(type, args, 1, NULL); |
| 83 |
Py_DECREF(args[0]); |
| 84 |
if (exc == NULL) { |
| 85 |
goto exit; |
| 86 |
} |
| 87 |
|
| 88 |
PyObject *code = PyLong_FromLong(errcode); |
| 89 |
if (code == NULL) { |
| 90 |
goto exit; |
| 91 |
} |
| 92 |
int rc = PyObject_SetAttrString(exc, "sqlite_errorcode", code); |
| 93 |
Py_DECREF(code); |
| 94 |
if (rc < 0) { |
| 95 |
goto exit; |
| 96 |
} |
| 97 |
|
| 98 |
const char *error_name = pysqlite_error_name(errcode); |
| 99 |
PyObject *name; |
| 100 |
if (error_name) { |
| 101 |
name = PyUnicode_FromString(error_name); |
| 102 |
} |
| 103 |
else { |
| 104 |
name = PyUnicode_InternFromString("unknown"); |
| 105 |
} |
| 106 |
if (name == NULL) { |
| 107 |
goto exit; |
| 108 |
} |
| 109 |
rc = PyObject_SetAttrString(exc, "sqlite_errorname", name); |
| 110 |
Py_DECREF(name); |
| 111 |
if (rc < 0) { |
| 112 |
goto exit; |
| 113 |
} |
| 114 |
|
| 115 |
PyErr_SetObject(type, exc); |
| 116 |
|
| 117 |
exit: |
| 118 |
Py_XDECREF(exc); |
| 119 |
} |
| 120 |
|
| 121 |
void |
| 122 |
set_error_from_code(pysqlite_state *state, int code) |
| 123 |
{ |
| 124 |
PyObject *exc_class = get_exception_class(state, code); |
| 125 |
if (exc_class == NULL) { |
| 126 |
// No new exception need be raised. |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
const char *errmsg = sqlite3_errstr(code); |
| 131 |
assert(errmsg != NULL); |
| 132 |
raise_exception(exc_class, code, errmsg); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Checks the SQLite error code and sets the appropriate DB-API exception. |
| 137 |
*/ |
| 138 |
void |
| 139 |
set_error_from_db(pysqlite_state *state, sqlite3 *db) |
| 140 |
{ |
| 141 |
int errorcode = sqlite3_errcode(db); |
| 142 |
PyObject *exc_class = get_exception_class(state, errorcode); |
| 143 |
if (exc_class == NULL) { |
| 144 |
// No new exception need be raised. |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
/* Create and set the exception. */ |
| 149 |
int extended_errcode = sqlite3_extended_errcode(db); |
| 150 |
// sqlite3_errmsg() always returns an UTF-8 encoded message |
| 151 |
const char *errmsg = sqlite3_errmsg(db); |
| 152 |
raise_exception(exc_class, extended_errcode, errmsg); |
| 153 |
} |
| 154 |
|
| 155 |
#ifdef WORDS_BIGENDIAN |
| 156 |
# define IS_LITTLE_ENDIAN 0 |
| 157 |
#else |
| 158 |
# define IS_LITTLE_ENDIAN 1 |
| 159 |
#endif |
| 160 |
|
| 161 |
sqlite_int64 |
| 162 |
_pysqlite_long_as_int64(PyObject * py_val) |
| 163 |
{ |
| 164 |
int overflow; |
| 165 |
long long value = PyLong_AsLongLongAndOverflow(py_val, &overflow); |
| 166 |
if (value == -1 && PyErr_Occurred()) |
| 167 |
return -1; |
| 168 |
if (!overflow) { |
| 169 |
# if SIZEOF_LONG_LONG > 8 |
| 170 |
if (-0x8000000000000000LL <= value && value <= 0x7FFFFFFFFFFFFFFFLL) |
| 171 |
# endif |
| 172 |
return value; |
| 173 |
} |
| 174 |
else if (sizeof(value) < sizeof(sqlite_int64)) { |
| 175 |
sqlite_int64 int64val; |
| 176 |
if (_PyLong_AsByteArray((PyLongObject *)py_val, |
| 177 |
(unsigned char *)&int64val, sizeof(int64val), |
| 178 |
IS_LITTLE_ENDIAN, 1 /* signed */, 0) >= 0) { |
| 179 |
return int64val; |
| 180 |
} |
| 181 |
} |
| 182 |
PyErr_SetString(PyExc_OverflowError, |
| 183 |
"Python int too large to convert to SQLite INTEGER"); |
| 184 |
return -1; |
| 185 |
} |