From 4b04bdf44d981d39bac307dd4436ff88a71d800c Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Thu, 27 Mar 2025 20:10:41 +0800 Subject: [PATCH 1/5] ok --- Lib/sqlite3/dbapi2.py | 5 ++++- .../Library/2025-01-21-14-51-44.gh-issue-118761.Z0F-d5.rst | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2025-01-21-14-51-44.gh-issue-118761.Z0F-d5.rst diff --git a/Lib/sqlite3/dbapi2.py b/Lib/sqlite3/dbapi2.py index 0315760516edf8..26eaf153bad7d2 100644 --- a/Lib/sqlite3/dbapi2.py +++ b/Lib/sqlite3/dbapi2.py @@ -20,9 +20,9 @@ # misrepresented as being the original software. # 3. This notice may not be removed or altered from any source distribution. -import datetime import time import collections.abc +import datetime from _sqlite3 import * @@ -37,12 +37,15 @@ Timestamp = datetime.datetime def DateFromTicks(ticks): + import time # Lazy, to improve import time return Date(*time.localtime(ticks)[:3]) def TimeFromTicks(ticks): + import time # Lazy, to improve import time return Time(*time.localtime(ticks)[3:6]) def TimestampFromTicks(ticks): + import time # Lazy, to improve import time return Timestamp(*time.localtime(ticks)[:6]) diff --git a/Misc/NEWS.d/next/Library/2025-01-21-14-51-44.gh-issue-118761.Z0F-d5.rst b/Misc/NEWS.d/next/Library/2025-01-21-14-51-44.gh-issue-118761.Z0F-d5.rst new file mode 100644 index 00000000000000..5d9157e9085a17 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-01-21-14-51-44.gh-issue-118761.Z0F-d5.rst @@ -0,0 +1 @@ +Halve the import time of :mod:`sqlite3`. From d3cf658dba37aafbeb5c1a7545186eb886a3abd8 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Fri, 6 Jun 2025 10:29:25 +0800 Subject: [PATCH 2/5] Improve the import time of `sqlite3` by 1.5x faster. --- Lib/sqlite3/__main__.py | 16 ++++++++++------ Lib/sqlite3/dbapi2.py | 6 ++++-- ...025-01-21-14-51-44.gh-issue-118761.Z0F-d5.rst | 2 +- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/Lib/sqlite3/__main__.py b/Lib/sqlite3/__main__.py index 79a6209468dae4..ae33a88ba6964b 100644 --- a/Lib/sqlite3/__main__.py +++ b/Lib/sqlite3/__main__.py @@ -7,10 +7,6 @@ import sqlite3 import sys -from argparse import ArgumentParser -from code import InteractiveConsole -from textwrap import dedent - def execute(c, sql, suppress_errors=True): """Helper that wraps execution of SQL code. @@ -34,13 +30,15 @@ def execute(c, sql, suppress_errors=True): sys.exit(1) -class SqliteInteractiveConsole(InteractiveConsole): +class SqliteInteractiveConsole: """A simple SQLite REPL.""" def __init__(self, connection): - super().__init__() + from code import InteractiveConsole self._con = connection self._cur = connection.cursor() + self._console = InteractiveConsole() + self._console.runsource = self.runsource def runsource(self, source, filename="", symbol="single"): """Override runsource, the core of the InteractiveConsole REPL. @@ -61,8 +59,14 @@ def runsource(self, source, filename="", symbol="single"): execute(self._cur, source) return False + def interact(self, banner, exitmsg=""): + self._console.interact(banner, exitmsg=exitmsg) + def main(*args): + from argparse import ArgumentParser + from textwrap import dedent + parser = ArgumentParser( description="Python sqlite3 CLI", ) diff --git a/Lib/sqlite3/dbapi2.py b/Lib/sqlite3/dbapi2.py index 26eaf153bad7d2..b651aea5c85904 100644 --- a/Lib/sqlite3/dbapi2.py +++ b/Lib/sqlite3/dbapi2.py @@ -20,9 +20,8 @@ # misrepresented as being the original software. # 3. This notice may not be removed or altered from any source distribution. -import time -import collections.abc import datetime +import collections.abc from _sqlite3 import * @@ -54,7 +53,10 @@ def TimestampFromTicks(ticks): Binary = memoryview collections.abc.Sequence.register(Row) +# Lazy registe def register_adapters_and_converters(): + global _lazy_register + _lazy_register = lambda: None # Prevent multiple registration # noqa: E731 from warnings import warn msg = ("The default {what} is deprecated as of Python 3.12; " diff --git a/Misc/NEWS.d/next/Library/2025-01-21-14-51-44.gh-issue-118761.Z0F-d5.rst b/Misc/NEWS.d/next/Library/2025-01-21-14-51-44.gh-issue-118761.Z0F-d5.rst index 5d9157e9085a17..eab19174b21825 100644 --- a/Misc/NEWS.d/next/Library/2025-01-21-14-51-44.gh-issue-118761.Z0F-d5.rst +++ b/Misc/NEWS.d/next/Library/2025-01-21-14-51-44.gh-issue-118761.Z0F-d5.rst @@ -1 +1 @@ -Halve the import time of :mod:`sqlite3`. +Improve the import time of :mod:`sqlite3` by 1.5x faster. From def197450d5718aef3f8e8acc0d91ff221b47962 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Fri, 6 Jun 2025 10:47:41 +0800 Subject: [PATCH 3/5] fix & finish --- Lib/sqlite3/__main__.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Lib/sqlite3/__main__.py b/Lib/sqlite3/__main__.py index 21f46549fc159a..ab9834fe12cfb6 100644 --- a/Lib/sqlite3/__main__.py +++ b/Lib/sqlite3/__main__.py @@ -7,6 +7,7 @@ import sqlite3 import sys +from code import InteractiveConsole from _colorize import get_theme, theme_no_color @@ -36,15 +37,13 @@ def execute(c, sql, suppress_errors=True, theme=theme_no_color): sys.exit(1) -class SqliteInteractiveConsole: +class SqliteInteractiveConsole(InteractiveConsole): """A simple SQLite REPL.""" def __init__(self, connection, use_color=False): from code import InteractiveConsole self._con = connection self._cur = connection.cursor() - self._console = InteractiveConsole() - self._console.runsource = self.runsource self._use_color = use_color def runsource(self, source, filename="", symbol="single"): @@ -77,9 +76,6 @@ def runsource(self, source, filename="", symbol="single"): execute(self._cur, source, theme=theme) return False - def interact(self, banner, exitmsg=""): - self._console.interact(banner, exitmsg=exitmsg) - def main(*args): from argparse import ArgumentParser From 04eedab50ef77749ae361d0aeeb3970f4b3a1a11 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Fri, 6 Jun 2025 10:49:08 +0800 Subject: [PATCH 4/5] Update dbapi2.py --- Lib/sqlite3/dbapi2.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/sqlite3/dbapi2.py b/Lib/sqlite3/dbapi2.py index b651aea5c85904..53cafad41a2fe3 100644 --- a/Lib/sqlite3/dbapi2.py +++ b/Lib/sqlite3/dbapi2.py @@ -53,7 +53,6 @@ def TimestampFromTicks(ticks): Binary = memoryview collections.abc.Sequence.register(Row) -# Lazy registe def register_adapters_and_converters(): global _lazy_register _lazy_register = lambda: None # Prevent multiple registration # noqa: E731 From ccf3575d521358ed9d41c26474560a862a3d9188 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Fri, 6 Jun 2025 10:56:08 +0800 Subject: [PATCH 5/5] fix --- Lib/sqlite3/__main__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/sqlite3/__main__.py b/Lib/sqlite3/__main__.py index ab9834fe12cfb6..471273b32131f8 100644 --- a/Lib/sqlite3/__main__.py +++ b/Lib/sqlite3/__main__.py @@ -41,7 +41,7 @@ class SqliteInteractiveConsole(InteractiveConsole): """A simple SQLite REPL.""" def __init__(self, connection, use_color=False): - from code import InteractiveConsole + super().__init__() self._con = connection self._cur = connection.cursor() self._use_color = use_color