From 2830c657afbf77118cef0b037da88703b0e93c64 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Tue, 21 Jan 2025 14:59:08 +0800 Subject: [PATCH 1/7] Improve import time of sqlite3 --- Lib/sqlite3/__init__.py | 6 +++--- Lib/sqlite3/__main__.py | 6 +++--- Lib/sqlite3/dbapi2.py | 11 +++++++---- .../2025-01-21-14-51-44.gh-issue-118761.Z0F-d5.rst | 1 + 4 files changed, 14 insertions(+), 10 deletions(-) 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/__init__.py b/Lib/sqlite3/__init__.py index 34a9c047dd607c..05487a96b7648a 100644 --- a/Lib/sqlite3/__init__.py +++ b/Lib/sqlite3/__init__.py @@ -1,8 +1,8 @@ -# pysqlite2/__init__.py: the pysqlite2 package. +# sqlite3/__init__.py: the sqlite3 package. # # Copyright (C) 2005 Gerhard Häring # -# This file is part of pysqlite. +# This file is part of sqlite3. # # This software is provided 'as-is', without any express or implied # warranty. In no event will the authors be held liable for any damages @@ -22,7 +22,7 @@ """ The sqlite3 extension module provides a DB-API 2.0 (PEP 249) compliant -interface to the SQLite library, and requires SQLite 3.7.15 or newer. +interface to the SQLite library, and requires SQLite 3.15.2 or newer. To use the module, start by creating a database Connection object: diff --git a/Lib/sqlite3/__main__.py b/Lib/sqlite3/__main__.py index cfdee61403d1fa..7ad3cbffedceb7 100644 --- a/Lib/sqlite3/__main__.py +++ b/Lib/sqlite3/__main__.py @@ -6,10 +6,7 @@ """ import sqlite3 import sys - -from argparse import ArgumentParser from code import InteractiveConsole -from textwrap import dedent def execute(c, sql, suppress_errors=True): @@ -63,6 +60,9 @@ def runsource(self, source, filename="", symbol="single"): 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 0315760516edf8..5f121d827c28fa 100644 --- a/Lib/sqlite3/dbapi2.py +++ b/Lib/sqlite3/dbapi2.py @@ -1,4 +1,4 @@ -# pysqlite2/dbapi2.py: the DB-API 2.0 interface +# sqlite3/dbapi2.py: the DB-API 2.0 interface # # Copyright (C) 2004-2005 Gerhard Häring # @@ -20,11 +20,10 @@ # 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 * +from _sqlite3 import Row, sqlite_version paramstyle = "qmark" @@ -37,12 +36,15 @@ Timestamp = datetime.datetime def DateFromTicks(ticks): + import time return Date(*time.localtime(ticks)[:3]) def TimeFromTicks(ticks): + import time return Time(*time.localtime(ticks)[3:6]) def TimestampFromTicks(ticks): + import time return Timestamp(*time.localtime(ticks)[:6]) @@ -52,6 +54,7 @@ def TimestampFromTicks(ticks): collections.abc.Sequence.register(Row) def register_adapters_and_converters(): + from _sqlite3 import register_adapter, register_converter 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 new file mode 100644 index 00000000000000..e7a1466fe68f34 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-01-21-14-51-44.gh-issue-118761.Z0F-d5.rst @@ -0,0 +1 @@ +Improve import time of :mod:`sqlite3` by around 2 times. Patch by Jiahao Li. From c6ca6774a38cb07b5f638be539dce386e5bd75ed Mon Sep 17 00:00:00 2001 From: Wulian <1055917385@qq.com> Date: Tue, 21 Jan 2025 16:26:28 +0800 Subject: [PATCH 2/7] fix? --- Lib/sqlite3/dbapi2.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/sqlite3/dbapi2.py b/Lib/sqlite3/dbapi2.py index 5f121d827c28fa..b55a636537f321 100644 --- a/Lib/sqlite3/dbapi2.py +++ b/Lib/sqlite3/dbapi2.py @@ -23,7 +23,7 @@ import collections.abc import datetime -from _sqlite3 import Row, sqlite_version +from _sqlite3 import * paramstyle = "qmark" @@ -54,7 +54,6 @@ def TimestampFromTicks(ticks): collections.abc.Sequence.register(Row) def register_adapters_and_converters(): - from _sqlite3 import register_adapter, register_converter from warnings import warn msg = ("The default {what} is deprecated as of Python 3.12; " From d727e36d843297919ca993db105c717b8ac4c9a3 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Sun, 26 Jan 2025 10:54:50 +0800 Subject: [PATCH 3/7] comment --- Lib/sqlite3/__init__.py | 4 ++-- Lib/sqlite3/dbapi2.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/sqlite3/__init__.py b/Lib/sqlite3/__init__.py index 05487a96b7648a..60fa6228c3b6aa 100644 --- a/Lib/sqlite3/__init__.py +++ b/Lib/sqlite3/__init__.py @@ -1,8 +1,8 @@ -# sqlite3/__init__.py: the sqlite3 package. +# pysqlite3/__init__.py: the pysqlite2 package. # # Copyright (C) 2005 Gerhard Häring # -# This file is part of sqlite3. +# This file is part of pysqlite2. # # This software is provided 'as-is', without any express or implied # warranty. In no event will the authors be held liable for any damages diff --git a/Lib/sqlite3/dbapi2.py b/Lib/sqlite3/dbapi2.py index 5f121d827c28fa..bddc4728d4cf5e 100644 --- a/Lib/sqlite3/dbapi2.py +++ b/Lib/sqlite3/dbapi2.py @@ -1,4 +1,4 @@ -# sqlite3/dbapi2.py: the DB-API 2.0 interface +# pysqlite2/dbapi2.py: the DB-API 2.0 interface # # Copyright (C) 2004-2005 Gerhard Häring # From d456d45d181554254bf1f4ad67d3c2c51b2dc44e Mon Sep 17 00:00:00 2001 From: Wulian <1055917385@qq.com> Date: Sun, 26 Jan 2025 10:56:50 +0800 Subject: [PATCH 4/7] comment --- Lib/sqlite3/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/sqlite3/__init__.py b/Lib/sqlite3/__init__.py index 60fa6228c3b6aa..ed727fae609d1d 100644 --- a/Lib/sqlite3/__init__.py +++ b/Lib/sqlite3/__init__.py @@ -1,8 +1,8 @@ -# pysqlite3/__init__.py: the pysqlite2 package. +# pysqlite2/__init__.py: the pysqlite2 package. # # Copyright (C) 2005 Gerhard Häring # -# This file is part of pysqlite2. +# This file is part of pysqlite. # # This software is provided 'as-is', without any express or implied # warranty. In no event will the authors be held liable for any damages From 14b8c29a2bcc3ce334d79a3ef41389a3872f0258 Mon Sep 17 00:00:00 2001 From: Wulian <1055917385@qq.com> Date: Sun, 26 Jan 2025 11:06:37 +0800 Subject: [PATCH 5/7] Update __main__.py --- Lib/sqlite3/__main__.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Lib/sqlite3/__main__.py b/Lib/sqlite3/__main__.py index 7ad3cbffedceb7..d9b41e839ebd01 100644 --- a/Lib/sqlite3/__main__.py +++ b/Lib/sqlite3/__main__.py @@ -6,7 +6,9 @@ """ import sqlite3 import sys +from argparse import ArgumentParser from code import InteractiveConsole +from textwrap import dedent def execute(c, sql, suppress_errors=True): @@ -60,9 +62,6 @@ def runsource(self, source, filename="", symbol="single"): def main(*args): - from argparse import ArgumentParser - from textwrap import dedent - parser = ArgumentParser( description="Python sqlite3 CLI", ) From 1bdb28f1f4b3e884b0bda5baa057e81a3f40d1c6 Mon Sep 17 00:00:00 2001 From: Wulian <1055917385@qq.com> Date: Sun, 26 Jan 2025 11:07:08 +0800 Subject: [PATCH 6/7] fix --- Lib/sqlite3/__main__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/sqlite3/__main__.py b/Lib/sqlite3/__main__.py index d9b41e839ebd01..cfdee61403d1fa 100644 --- a/Lib/sqlite3/__main__.py +++ b/Lib/sqlite3/__main__.py @@ -6,6 +6,7 @@ """ import sqlite3 import sys + from argparse import ArgumentParser from code import InteractiveConsole from textwrap import dedent From e905f127dcfb482732893fa7ff8106be08824441 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Mon, 3 Feb 2025 09:45:45 +0800 Subject: [PATCH 7/7] Add lazy import comment --- Lib/sqlite3/dbapi2.py | 6 +++--- .../Library/2025-01-21-14-51-44.gh-issue-118761.Z0F-d5.rst | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/sqlite3/dbapi2.py b/Lib/sqlite3/dbapi2.py index 82411a2f5e75e6..c1683b94d2b405 100644 --- a/Lib/sqlite3/dbapi2.py +++ b/Lib/sqlite3/dbapi2.py @@ -36,15 +36,15 @@ Timestamp = datetime.datetime def DateFromTicks(ticks): - import time + import time # Lazy, to improve import time return Date(*time.localtime(ticks)[:3]) def TimeFromTicks(ticks): - import time + import time # Lazy, to improve import time return Time(*time.localtime(ticks)[3:6]) def TimestampFromTicks(ticks): - import time + 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 index e7a1466fe68f34..d156673de5cc79 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 @@ -Improve import time of :mod:`sqlite3` by around 2 times. Patch by Jiahao Li. +Halve the import time of :mod:`sqlite3`. Patch by Jiahao Li.