Skip to content

Commit c31b1a5

Browse files
committed
Add tests to ensure lazy imports are lazy and fix sqlite3's main
1 parent 5d16d31 commit c31b1a5

6 files changed

Lines changed: 39 additions & 4 deletions

File tree

Lib/sqlite3/__main__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@
1515
from ._completer import completer
1616

1717

18-
def execute(c, sql, suppress_errors=True, theme=theme_no_color):
18+
def execute(c, sql, suppress_errors=True, theme=None):
1919
"""Helper that wraps execution of SQL code.
2020
2121
This is used both by the REPL and by direct execution from the CLI.
2222
2323
'c' may be a cursor or a connection.
2424
'sql' is the SQL string to execute.
2525
"""
26+
if theme is None:
27+
theme = theme_no_color
2628

2729
try:
2830
for row in c.execute(sql):

Lib/test/test_difflib.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import difflib
2+
from test import support
23
from test.support import findfile, force_colorized
4+
from test.support.import_helper import ensure_lazy_imports
35
import unittest
46
import doctest
57
import sys
@@ -644,6 +646,12 @@ def setUpModule():
644646
difflib.HtmlDiff._default_prefix = 0
645647

646648

649+
class LazyImportTest(unittest.TestCase):
650+
@support.cpython_only
651+
def test_lazy_import(self):
652+
ensure_lazy_imports("difflib", {"_colorize"})
653+
654+
647655
def load_tests(loader, tests, pattern):
648656
tests.addTest(doctest.DocTestSuite(difflib))
649657
return tests

Lib/test/test_json/test_tool.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from test import support
1010
from test.support import force_colorized, force_not_colorized, os_helper
11+
from test.support.import_helper import ensure_lazy_imports
1112
from test.support.script_helper import assert_python_ok
1213

1314
from _colorize import get_theme
@@ -334,5 +335,11 @@ class TestTool(TestMain):
334335
module = 'json.tool'
335336

336337

338+
class LazyImportTest(unittest.TestCase):
339+
@support.cpython_only
340+
def test_lazy_import(self):
341+
ensure_lazy_imports("json.tool", {"_colorize"})
342+
343+
337344
if __name__ == "__main__":
338345
unittest.main()

Lib/test/test_pdb.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from io import StringIO
2323
from test import support
2424
from test.support import has_socket_support, os_helper
25-
from test.support.import_helper import import_module
25+
from test.support.import_helper import ensure_lazy_imports, import_module
2626
from test.support.pty_helper import run_pty, FakeInput
2727
from test.support.script_helper import kill_python
2828
from unittest.mock import patch
@@ -5306,5 +5306,11 @@ def tearDown(test):
53065306
return tests
53075307

53085308

5309+
class LazyImportTest(unittest.TestCase):
5310+
@support.cpython_only
5311+
def test_lazy_import(self):
5312+
ensure_lazy_imports("pdb", {"_colorize"})
5313+
5314+
53095315
if __name__ == '__main__':
53105316
unittest.main()

Lib/test/test_sqlite3/test_cli.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
import os
88

99
from sqlite3.__main__ import main as cli
10-
from test.support.import_helper import import_module
10+
from test.support.import_helper import ensure_lazy_imports, import_module
1111
from test.support.os_helper import TESTFN, unlink
1212
from test.support.pty_helper import run_pty
1313
from test.support import (
1414
captured_stdout,
1515
captured_stderr,
1616
captured_stdin,
17+
cpython_only,
1718
force_not_colorized_test_class,
1819
requires_subprocess,
1920
verbose,
@@ -437,6 +438,11 @@ def test_complete_no_input(self):
437438
raise
438439

439440

441+
class LazyImportTest(unittest.TestCase):
442+
@cpython_only
443+
def test_lazy_import(self):
444+
ensure_lazy_imports("sqlite3.__main__", {"_colorize"})
445+
440446

441447
if __name__ == "__main__":
442448
unittest.main()

Lib/test/test_traceback.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
requires_subprocess, os_helper)
2424
from test.support.os_helper import TESTFN, temp_dir, unlink
2525
from test.support.script_helper import assert_python_ok, assert_python_failure, make_script
26-
from test.support.import_helper import forget
26+
from test.support.import_helper import ensure_lazy_imports, forget
2727
from test.support import force_not_colorized, force_not_colorized_test_class
2828

2929
import json
@@ -5543,5 +5543,11 @@ def test_suggestion_still_works_for_non_lazy_attributes(self):
55435543
self.assertNotIn(b"BAR_MODULE_LOADED", stdout)
55445544

55455545

5546+
class LazyImportTest(unittest.TestCase):
5547+
@support.cpython_only
5548+
def test_lazy_import(self):
5549+
ensure_lazy_imports("traceback", {"_colorize"})
5550+
5551+
55465552
if __name__ == "__main__":
55475553
unittest.main()

0 commit comments

Comments
 (0)