Skip to content

Commit 479157f

Browse files
committed
try to fix sqlite3 convert warning
See also #14139
1 parent 4cb9024 commit 479157f

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

‎IPython/core/history.py‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66

77
import atexit
8-
import datetime
98
import re
109
import sqlite3
1110
import threading
1211
from pathlib import Path
12+
from datetime import datetime
1313

1414
from decorator import decorator
1515
from traitlets import (
@@ -30,6 +30,22 @@
3030
from IPython.paths import locate_profile
3131
from IPython.utils.decorators import undoc
3232

33+
34+
# get rid of :
35+
# Python/core/tests/test_history.py::test_timestamp_type
36+
# IPython/core/history.py:335: DeprecationWarning:
37+
# The default timestamp converter is deprecated as of Python 3.12;
38+
# see the sqlite3 documentation for suggested replacement recipes
39+
# return self.db.execute(query, (session,)).fetchone()
40+
#
41+
#
42+
def convert_timestamp(val):
43+
"""Convert Unix epoch timestamp to datetime.datetime object."""
44+
return datetime.fromtimestamp(val)
45+
46+
47+
sqlite3.register_converter("timestamp", convert_timestamp)
48+
3349
#-----------------------------------------------------------------------------
3450
# Classes and functions
3551
#-----------------------------------------------------------------------------
@@ -90,8 +106,8 @@ def catch_corrupt_db(f, self, *a, **kw):
90106
size = self.hist_file.stat().st_size
91107
if size >= _SAVE_DB_SIZE:
92108
# if there's significant content, avoid clobbering
93-
now = datetime.datetime.now().isoformat().replace(':', '.')
94-
newpath = base + '-corrupt-' + now + ext
109+
now = datetime.now().isoformat().replace(":", ".")
110+
newpath = base + "-corrupt-" + now + ext
95111
# don't clobber previous corrupt backups
96112
for i in range(100):
97113
if not Path(newpath).exists():
@@ -583,7 +599,7 @@ def new_session(self, conn=None):
583599
cur = conn.execute(
584600
"""INSERT INTO sessions VALUES (NULL, ?, NULL,
585601
NULL, '') """,
586-
(datetime.datetime.now().isoformat(" "),),
602+
(datetime.now().isoformat(" "),),
587603
)
588604
self.session_number = cur.lastrowid
589605

@@ -595,7 +611,7 @@ def end_session(self):
595611
"""UPDATE sessions SET end=?, num_cmds=? WHERE
596612
session==?""",
597613
(
598-
datetime.datetime.now().isoformat(" "),
614+
datetime.now().isoformat(" "),
599615
len(self.input_hist_parsed) - 1,
600616
self.session_number,
601617
),

0 commit comments

Comments
 (0)