[issue29099] sqlite3 timestamp converter ValueError
Bozo Kopic
report at bugs.python.org
Wed Dec 28 18:04:29 EST 2016
New submission from Bozo Kopic:
Current convert_timestamp function raises exception when parsing timestamps that contain timezone information and have microsecond set to zero.
Patch for this behavior is included.
Example script that demonstrates this problem:
import sys
import datetime
import sqlite3
import traceback
import contextlib
def main():
db = sqlite3.connect('test.db', detect_types=sqlite3.PARSE_DECLTYPES)
db.executescript("""
CREATE TABLE IF NOT EXISTS test (
timestamp TIMESTAMP)
""")
t = datetime.datetime.now(tz=datetime.timezone.utc)
t = t.replace(microsecond=0)
db.executemany("INSERT INTO test VALUES (:timestamp)", [{'timestamp': t}])
db.commit()
with contextlib.closing(db.cursor()) as c:
try:
c.execute('SELECT * FROM test')
c.fetchall()
print('original implementation success')
except Exception as e:
print('original implementation failed')
traceback.print_exc()
c.close()
sqlite3.register_converter("timestamp", _sqlite_convert_timestamp)
with contextlib.closing(db.cursor()) as c:
try:
c.execute('SELECT * FROM test')
c.fetchall()
print('patched implementation success')
except Exception as e:
print('patched implementation failed')
traceback.print_exc()
c.close()
def _sqlite_convert_timestamp(val):
datepart, timepart = val.split(b" ")
# this is the patch
timepart = timepart.split(b'+', 1)[0].split(b'-', 1)[0]
year, month, day = map(int, datepart.split(b"-"))
timepart_full = timepart.split(b".")
hours, minutes, seconds = map(int, timepart_full[0].split(b":"))
if len(timepart_full) == 2:
microseconds = int('{:0<6.6}'.format(timepart_full[1].decode()))
else:
microseconds = 0
val = datetime.datetime(year, month, day, hours, minutes, seconds,
microseconds)
return val
if __name__ == '__main__':
sys.exit(main())
----------
components: Library (Lib)
files: sqlite3.patch
keywords: patch
messages: 284205
nosy: bozo.kopic
priority: normal
severity: normal
status: open
title: sqlite3 timestamp converter ValueError
type: crash
versions: Python 3.5, Python 3.6, Python 3.7
Added file: http://bugs.python.org/file46067/sqlite3.patch
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29099>
_______________________________________
More information about the Python-bugs-list
mailing list