File: exceptions.rst.txt

package info (click to toggle)
python-apsw 3.46.0.1-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,684 kB
  • sloc: python: 13,125; ansic: 12,334; javascript: 911; makefile: 10; sh: 7
file content (447 lines) | stat: -rw-r--r-- 15,633 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
.. currentmodule:: apsw

.. _exceptions:

Exceptions and Errors
*********************

Python uses :class:`exceptions <Exception>` to indicate an error has
happened.  The SQLite library uses `integer error codes
<https://www.sqlite.org/rescode.html>`__.  APSW maps between the two
systems as needed.  Exceptions raised in Python code called by SQLite
will have that exception present when control returns to Python, and
SQLite will understand that an error occurred.

Chaining
--------

When an error is reported to SQLite, it may take further actions.  For
example errors in :doc:`VFS <vfs>` can result in error recovery
attempts, while an error in a window function step method will result
in the final method being called to do clean up.  Your code
implementing those could also have additional exceptions.

When multiple exceptions occur in the same SQLite control flow then
they will be :pep:`chained <3134>`.  Python's traceback printing code
will show `all the exceptions
<https://docs.python.org/3/library/exceptions.html#exception-context>`__.


.. _unraisable:

Unraisable
----------

There are a few places where it is not possible for a Python exception
to be reported to SQLite as an error, and Python C code does not allow
destructors to report exceptions.  These exceptions are reported via
`sys.unraisablehook
<https://docs.python.org/3/library/sys.html#sys.unraisablehook>`__,
and if that is not present then `sys.excepthook
<https://docs.python.org/3/library/sys.html#sys.excepthook>`__.

`sqlite3_log <https://www.sqlite.org/c3ref/log.html>`__ is also called
so that you will have the context of when the exception happened
relative to the errors SQLite is logging.

Exception Classes
-----------------


.. exception:: Error

  This is the base for APSW exceptions.

.. attribute:: Error.result

         For exceptions corresponding to `SQLite error codes
         <https://sqlite.org/c3ref/c_abort.html>`_ codes this attribute
         is the numeric error code.

.. attribute:: Error.extendedresult

         APSW runs with `extended result codes
         <https://sqlite.org/rescode.html>`_ turned on.
         This attribute includes the detailed code.

         As an example, if SQLite issued a read request and the system
         returned less data than expected then :attr:`~Error.result`
         would have the value *SQLITE_IOERR* while
         :attr:`~Error.extendedresult` would have the value
         *SQLITE_IOERR_SHORT_READ*.

.. attribute:: Error.error_offset

        The location of the error in the SQL when encoded in UTF-8.
        The value is from `sqlite3_error_offset
        <https://www.sqlite.org/c3ref/errcode.html>`__, and will be
        `-1` when a specific token in the input is not the cause.



APSW specific exceptions
========================

The following exceptions happen when APSW detects various problems.

.. exception:: ThreadingViolationError

  You have used an object concurrently in two threads. For example you
  may try to use the same cursor in two different threads at the same
  time, or tried to close the same connection in two threads at the
  same time.

  You can also get this exception by using a cursor as an argument to
  itself (eg as the input data for :meth:`Cursor.executemany`).
  Cursors can only be used for one thing at a time.

.. exception:: ForkingViolationError

  See :meth:`apsw.fork_checker`.

.. exception:: IncompleteExecutionError

  You have tried to start a new SQL execute call before executing all
  the previous ones. See the :ref:`execution model <executionmodel>`
  for more details.

.. exception:: ConnectionNotClosedError

  This exception is no longer generated.  It was required in earlier
  releases due to constraints in threading usage with SQLite.

.. exception:: ConnectionClosedError

  You have called :meth:`Connection.close` and then continued to use
  the :class:`Connection` or associated :class:`cursors <Cursor>`.

.. exception:: CursorClosedError

  You have called :meth:`Cursor.close` and then tried to use the cursor.

.. exception:: BindingsError

  There are several causes for this exception.  When using tuples, an incorrect number of bindings where supplied::

     cursor.execute("select ?,?,?", (1,2))     # too few bindings
     cursor.execute("select ?,?,?", (1,2,3,4)) # too many bindings

  You are using named bindings, but not all bindings are named.  You should either use entirely the
  named style or entirely numeric (unnamed) style::

     cursor.execute("select * from foo where x=:name and y=?")

.. exception:: ExecutionCompleteError

  Execution of the statements is complete and cannot be run further.


.. exception:: ExecTraceAbort

  The :ref:`execution tracer <executiontracer>` returned False so
  execution was aborted.


.. exception:: ExtensionLoadingError

  An error happened loading an `extension
  <https://sqlite.org/loadext.html>`_.

.. exception:: VFSNotImplementedError

  A call cannot be made to an inherited :ref:`VFS` method as the VFS
  does not implement the method.

.. exception:: VFSFileClosedError

  The VFS file is closed so the operation cannot be performed.

SQLite Exceptions
=================

The following lists which Exception classes correspond to which `SQLite
error codes <https://sqlite.org/c3ref/c_abort.html>`_.


General Errors
^^^^^^^^^^^^^^

.. exception:: SQLError

  `SQLITE_ERROR <https://sqlite.org/rescode.html#error>`__.  The
  standard error code, unless a more specific one is  applicable.

.. exception:: MismatchError

  `SQLITE_MISMATCH <https://sqlite.org/rescode.html#mismatch>`__. Data
  type mismatch.  For example a rowid or integer primary key must be
  an integer.

.. exception:: NotFoundError

  `SQLITE_NOTFOUND <https://sqlite.org/rescode.html#notfound>`__.
  Returned when various internal items were not found such as requests
  for non-existent system calls or file controls.

Internal Errors
^^^^^^^^^^^^^^^

.. exception:: InternalError

  `SQLITE_INTERNAL <https://sqlite.org/rescode.html#internal>`__. (No
  longer used) Internal logic error in SQLite.

.. exception:: ProtocolError

  `SQLITE_PROTOCOL <https://sqlite.org/rescode.html#protocol>`__. (No
  longer used) Database lock protocol error.

.. exception:: MisuseError

  `SQLITE_MISUSE <https://sqlite.org/rescode.html#misuse>`__.  SQLite
  library used incorrectly - typically similar to *ValueError* in
  Python.  Examples include not having enough flags when opening a
  connection (eg not including a READ or WRITE flag), or out of spec
  such as registering a function with more than 127 parameters.

.. exception:: RangeError

  `SQLITE_RANGE <https://sqlite.org/rescode.html#range>`__.  (Cannot
  be generated using APSW).  2nd parameter to `sqlite3_bind
  <https://sqlite.org/c3ref/bind_blob.html>`_ out of range

Permissions Etc
^^^^^^^^^^^^^^^

.. exception:: PermissionsError

  `SQLITE_PERM <https://sqlite.org/rescode.html#perm>`__. Access
  permission denied by the operating system.

.. exception:: ReadOnlyError

  `SQLITE_READONLY <https://sqlite.org/rescode.html#readonly>`__.
  Attempt to write to a readonly database.

.. exception:: CantOpenError

  `SQLITE_CANTOPEN <https://sqlite.org/rescode.html#cantopen>`__.
  Unable to open the database file.

.. exception:: AuthError

  `SQLITE_AUTH <https://sqlite.org/rescode.html#auth>`__.
  :attr:`Authorization <Connection.authorizer>` denied.

Abort/Busy Etc
^^^^^^^^^^^^^^

.. exception:: AbortError

  `SQLITE_ABORT <https://sqlite.org/rescode.html#abort>`__. Callback
  routine requested an abort.

.. exception:: BusyError

  `SQLITE_BUSY <https://sqlite.org/rescode.html#busy>`__.  The
  database file is locked.  Use  :meth:`Connection.set_busy_timeout`
  to change how long SQLite waits for the database to be unlocked or
  :meth:`Connection.set_busy_handler` to use your own handler.

.. exception:: LockedError

  `SQLITE_LOCKED <https://sqlite.org/rescode.html#locked>`__.  Shared
  cache lock.

.. exception:: InterruptError

  SQLITE_INTERRUPT <https://sqlite.org/rescode.html#interrupt>`__.
  Operation terminated by `sqlite3_interrupt
  <https://sqlite.org/c3ref/interrupt.html>`_ - use
  :meth:`Connection.interrupt`.

.. exception:: SchemaChangeError

  `SQLITE_SCHEMA <https://sqlite.org/rescode.html#schema>`__.  The
  database schema changed.  A  :meth:`prepared statement
  <Cursor.execute>` becomes invalid if the database schema was
  changed.  Behind the scenes SQLite reprepares the statement.
  Another or the same :class:`Connection` may change the schema again
  before the statement runs.  SQLite will retry before giving up and
  returning this error.

.. exception:: ConstraintError

  `SQLITE_CONSTRAINT <https://sqlite.org/rescode.html#constraint>`__.
  Abort due to `constraint
  <https://sqlite.org/lang_createtable.html>`_ violation.

Memory/Disk
^^^^^^^^^^^

.. exception:: NoMemError

  `SQLITE_NOMEM <https://sqlite.org/rescode.html#nomem>`__.  A memory
   allocation failed.

.. exception:: IOError

  `SQLITE_IOERR <https://sqlite.org/rescode.html#ioerr>`__.  A disk
  I/O error occurred.  The :ref:`extended error code <exceptions>`
  will give more detail.

.. exception:: CorruptError

  `SQLITE_CORRUPT <https://sqlite.org/rescode.html#corrupt>`__.  The
  database disk image appears to be a SQLite database but the values
  inside are inconsistent.

.. exception:: FullError

  `SQLITE_FULL <https://sqlite.org/rescode.html#full>`__.  The disk
  appears to be full.

.. exception:: TooBigError

  `SQLITE_TOOBIG <https://sqlite.org/rescode.html#toobig>`__.  String
  or BLOB exceeds size limit.  You can  change the limits using
  :meth:`Connection.limit`.

.. exception:: NoLFSError

  `SQLITE_NOLFS <https://sqlite.org/rescode.html#nolfs>`__.  SQLite
  has attempted to use a feature not supported by the operating system
  such as `large file support
  <https://en.wikipedia.org/wiki/Large_file_support>`_.

.. exception:: EmptyError

  `SQLITE_EMPTY <https://sqlite.org/rescode.html#empty>`__. Not
  currently used.

.. exception:: FormatError

  `SQLITE_FORMAT <https://sqlite.org/rescode.html#format>`__. (No
  longer used) `Auxiliary database
  <https://sqlite.org/lang_attach.html>`_ format error.

.. exception:: NotADBError

  `SQLITE_NOTADB <https://sqlite.org/rescode.html#notadb>`__.  File
  opened that is not a database file.  SQLite has a header on database
  files to verify they are indeed SQLite databases.


.. _augmentedstacktraces:

Augmented stack traces
======================

When an exception occurs, Python does not include frames from
non-Python code (ie the C code called from Python).  This can make it
more difficult to work out what was going on when an exception
occurred for example when there are callbacks to collations, functions
or virtual tables, triggers firing etc.

This is an example showing the difference between the tracebacks you
would have got with earlier versions of apsw and the augmented
traceback::

  import apsw

  def myfunc(x):
    1/0

  con=apsw.Connection(":memory:")
  con.create_scalar_function("foo", myfunc)
  con.create_scalar_function("fam", myfunc)
  cursor=con.cursor()
  cursor.execute("create table bar(x,y,z);insert into bar values(1,2,3)")
  cursor.execute("select foo(1) from bar")


+-----------------------------------------------------------+
| Original Traceback                                        |
+===========================================================+
| ::                                                        |
|                                                           |
|   Traceback (most recent call last):                      |
|     File "t.py", line 11, in <module>                     |
|       cursor.execute("select foo(1) from bar")            |
|     File "t.py", line 4, in myfunc                        |
|       1/0                                                 |
|   ZeroDivisionError: integer division or modulo by zero   |
|                                                           |
|                                                           |
+-----------------------------------------------------------+

+----------------------------------------------------------+
|      Augmented Traceback                                 |
+==========================================================+
| ::                                                       |
|                                                          |
|   Traceback (most recent call last):                     |
|     File "t.py", line 11, in <module>                    |
|       cursor.execute("select foo(1) from bar")           |
|     File "apsw.c", line 3412, in resetcursor             |
|     File "apsw.c", line 1597, in user-defined-scalar-foo |
|     File "t.py", line 4, in myfunc                       |
|       1/0                                                |
|   ZeroDivisionError: integer division or modulo by zero  |
+----------------------------------------------------------+

In the original traceback you can't even see that code in apsw was
involved. The augmented traceback shows that there were indeed two
function calls within apsw and gives you line numbers should you need
to examine the code. Also note how you are told that the call was in
`user-defined-scalar-foo` (ie you can tell which function was called.)

*But wait, there is more!!!* In order to further aid troubleshooting,
the augmented stack traces make additional information available. Each
frame in the traceback has local variables defined with more
information. You can use :meth:`apsw.ext.print_augmented_traceback` to
print an exception with the local variables.

Here is a far more complex example from some :ref:`virtual tables
<Virtualtables>` code I was writing. The BestIndex method in my code
had returned an incorrect value. The augmented traceback includes
local variables. I can see what was passed in to my method, what I
returned and which item was erroneous. The original traceback is
almost completely useless!

Original traceback::

  Traceback (most recent call last):
    File "tests.py", line 1387, in testVtables
      cursor.execute(allconstraints)
  TypeError: Bad constraint (#2) - it should be one of None, an integer or a tuple of an integer and a boolean

Augmented traceback with local variables::

  Traceback (most recent call last):
    File "tests.py", line 1387, in testVtables
      cursor.execute(allconstraints)
                  VTable =  __main__.VTable
                     cur =  <apsw.Cursor object at 0x988f30>
                       i =  10
                    self =  testVtables (__main__.APSW)
          allconstraints =  select rowid,* from foo where rowid>-1000 ....

    File "apsw.c", line 4050, in Cursor_execute.sqlite3_prepare
              Connection =  <apsw.Connection object at 0x978800>
               statement =  select rowid,* from foo where rowid>-1000 ....

    File "apsw.c", line 2681, in VirtualTable.xBestIndex
                    self =  <__main__.VTable instance at 0x98d8c0>
                    args =  (((-1, 4), (0, 32), (1, 8), (2, 4), (3, 64)), ((2, False),))
                  result =  ([4, (3,), [2, False], [1], [0]], 997, u'\xea', False)

    File "apsw.c", line 2559, in VirtualTable.xBestIndex.result_constraint
                 indices =  [4, (3,), [2, False], [1], [0]]
                    self =  <__main__.VTable instance at 0x98d8c0>
                  result =  ([4, (3,), [2, False], [1], [0]], 997, u'\xea', False)
              constraint =  (3,)

  TypeError: Bad constraint (#2) - it should be one of None, an integer or a tuple of an integer and a boolean