Skip to content

gh-118761: Improve import time of sqlite3 #131796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Lib/sqlite3/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import sqlite3
import sys

from argparse import ArgumentParser
from code import InteractiveConsole
from textwrap import dedent
from _colorize import get_theme, theme_no_color


Expand Down Expand Up @@ -80,6 +78,9 @@ def runsource(self, source, filename="<input>", symbol="single"):


def main(*args):
from argparse import ArgumentParser
from textwrap import dedent

Comment on lines 80 to +83
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not see a valid reason to do code churn and delay imports for the sake of speeding up import sqlite3.__main__ while assuming that sqlite3.__main__.main() is never called.

parser = ArgumentParser(
description="Python sqlite3 CLI",
color=True,
Expand Down
6 changes: 5 additions & 1 deletion Lib/sqlite3/dbapi2.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
# 3. This notice may not be removed or altered from any source distribution.

import datetime
import time
import collections.abc

from _sqlite3 import *
Expand All @@ -37,12 +36,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])


Expand All @@ -52,6 +54,8 @@ def TimestampFromTicks(ticks):
collections.abc.Sequence.register(Row)

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; "
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve the import time of :mod:`sqlite3` by 1.5x faster.
Loading