[New-bugs-announce] [issue16379] SQLite error code not exposed to python
Torsten Landschoff
report at bugs.python.org
Thu Nov 1 11:14:38 CET 2012
New submission from Torsten Landschoff:
The sqlite3 module does not expose the sqlite3 error codes to python. This makes it impossible to detect specific error conditions directly.
Case in point: If a user selects some random file as the database in our application, we can not detect that it is not a valid database file:
$ /opt/python3/bin/python3
Python 3.4.0a0 (default:2d6eec5d01f7, Nov 1 2012, 10:47:27)
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> conn = sqlite3.connect("/etc/passwd")
>>> from pprint import pprint
>>> try:
... conn.execute("select * from random_table")
... except Exception as e:
... pprint({name: getattr(e, name) for name in dir(e)})
... raise
...
{'__cause__': None,
'__class__': <class 'sqlite3.DatabaseError'>,
'__context__': None,
'__delattr__': <method-wrapper '__delattr__' of DatabaseError object at 0x7ffc9a13b050>,
'__dict__': {},
'__dir__': <built-in method __dir__ of DatabaseError object at 0x7ffc9a13b050>,
'__doc__': None,
'__eq__': <method-wrapper '__eq__' of DatabaseError object at 0x7ffc9a13b050>,
'__format__': <built-in method __format__ of DatabaseError object at 0x7ffc9a13b050>,
'__ge__': <method-wrapper '__ge__' of DatabaseError object at 0x7ffc9a13b050>,
'__getattribute__': <method-wrapper '__getattribute__' of DatabaseError object at 0x7ffc9a13b050>,
'__gt__': <method-wrapper '__gt__' of DatabaseError object at 0x7ffc9a13b050>,
'__hash__': <method-wrapper '__hash__' of DatabaseError object at 0x7ffc9a13b050>,
'__init__': <method-wrapper '__init__' of DatabaseError object at 0x7ffc9a13b050>,
'__le__': <method-wrapper '__le__' of DatabaseError object at 0x7ffc9a13b050>,
'__lt__': <method-wrapper '__lt__' of DatabaseError object at 0x7ffc9a13b050>,
'__module__': 'sqlite3',
'__ne__': <method-wrapper '__ne__' of DatabaseError object at 0x7ffc9a13b050>,
'__new__': <built-in method __new__ of type object at 0x8267e0>,
'__reduce__': <built-in method __reduce__ of DatabaseError object at 0x7ffc9a13b050>,
'__reduce_ex__': <built-in method __reduce_ex__ of DatabaseError object at 0x7ffc9a13b050>,
'__repr__': <method-wrapper '__repr__' of DatabaseError object at 0x7ffc9a13b050>,
'__setattr__': <method-wrapper '__setattr__' of DatabaseError object at 0x7ffc9a13b050>,
'__setstate__': <built-in method __setstate__ of DatabaseError object at 0x7ffc9a13b050>,
'__sizeof__': <built-in method __sizeof__ of DatabaseError object at 0x7ffc9a13b050>,
'__str__': <method-wrapper '__str__' of DatabaseError object at 0x7ffc9a13b050>,
'__subclasshook__': <built-in method __subclasshook__ of type object at 0x1238770>,
'__suppress_context__': False,
'__traceback__': <traceback object at 0x7ffc9a138cf8>,
'__weakref__': None,
'args': ('file is encrypted or is not a database',),
'with_traceback': <built-in method with_traceback of DatabaseError object at 0x7ffc9a13b050>}
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
sqlite3.DatabaseError: file is encrypted or is not a database
>>>
Currently, one can only match the error message, with is bad programming style.
The error code for this error is SQLITE_NOTADB, as found in the function sqlite3ErrStr when searching for the message in SQLite's main.c at http://www.sqlite.org/src/artifact/02255cf1da50956c5427c469abddb15bccc4ba09
Unfortunately, the sqlite3 module does not expose the error code itself (neither the actual error code nor the defined error codes) in any way. Errors are handled in Modules/_sqlite/util.c:
http://hg.python.org/cpython/file/2d6eec5d01f7/Modules/_sqlite/util.c#l99
I would like to have the defined error codes available in some mapping inside the sqlite3 module as well as the actual error code inside every sqlite exception e as e.sqlite_errcode
----------
components: Library (Lib)
messages: 174395
nosy: torsten
priority: normal
severity: normal
status: open
title: SQLite error code not exposed to python
type: enhancement
versions: Python 3.4
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16379>
_______________________________________
More information about the New-bugs-announce
mailing list