• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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 *
get_exception_class(pysqlite_state * state,int errorcode)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
raise_exception(PyObject * type,int errcode,const char * errmsg)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 /**
122  * Checks the SQLite error code and sets the appropriate DB-API exception.
123  * Returns the error code (0 means no error occurred).
124  */
125 int
_pysqlite_seterror(pysqlite_state * state,sqlite3 * db)126 _pysqlite_seterror(pysqlite_state *state, sqlite3 *db)
127 {
128     int errorcode = sqlite3_errcode(db);
129     PyObject *exc_class = get_exception_class(state, errorcode);
130     if (exc_class == NULL) {
131         // No new exception need be raised; just pass the error code
132         return errorcode;
133     }
134 
135     /* Create and set the exception. */
136     int extended_errcode = sqlite3_extended_errcode(db);
137     const char *errmsg = sqlite3_errmsg(db);
138     raise_exception(exc_class, extended_errcode, errmsg);
139     return extended_errcode;
140 }
141 
142 #ifdef WORDS_BIGENDIAN
143 # define IS_LITTLE_ENDIAN 0
144 #else
145 # define IS_LITTLE_ENDIAN 1
146 #endif
147 
148 sqlite_int64
_pysqlite_long_as_int64(PyObject * py_val)149 _pysqlite_long_as_int64(PyObject * py_val)
150 {
151     int overflow;
152     long long value = PyLong_AsLongLongAndOverflow(py_val, &overflow);
153     if (value == -1 && PyErr_Occurred())
154         return -1;
155     if (!overflow) {
156 # if SIZEOF_LONG_LONG > 8
157         if (-0x8000000000000000LL <= value && value <= 0x7FFFFFFFFFFFFFFFLL)
158 # endif
159             return value;
160     }
161     else if (sizeof(value) < sizeof(sqlite_int64)) {
162         sqlite_int64 int64val;
163         if (_PyLong_AsByteArray((PyLongObject *)py_val,
164                                 (unsigned char *)&int64val, sizeof(int64val),
165                                 IS_LITTLE_ENDIAN, 1 /* signed */, 0) >= 0) {
166             return int64val;
167         }
168     }
169     PyErr_SetString(PyExc_OverflowError,
170                     "Python int too large to convert to SQLite INTEGER");
171     return -1;
172 }
173