Skip to content
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
11 changes: 5 additions & 6 deletions Lib/sqlite3/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
from ._completer import completer


EOF_KEY = "CTRL-Z" if sys.platform == "win32" else "CTRL-D"


def execute(c, sql, suppress_errors=True, theme=theme_no_color):
"""Helper that wraps execution of SQL code.

Expand Down Expand Up @@ -69,7 +72,7 @@ def runsource(self, source, filename="<input>", symbol="single"):
print(f"Enter SQL code or one of the below commands, and press enter.\n\n"
f"{t.builtin}.version{t.reset} Print underlying SQLite library version\n"
f"{t.builtin}.help{t.reset} Print this help message\n"
f"{t.builtin}.quit{t.reset} Exit the CLI, equivalent to CTRL-D\n")
f"{t.builtin}.quit{t.reset} Exit the CLI, equivalent to {EOF_KEY}\n")
case "quit":
sys.exit(0)
case "":
Expand Down Expand Up @@ -117,16 +120,12 @@ def main(*args):
db_name = repr(args.filename)

# Prepare REPL banner and prompts.
if sys.platform == "win32" and "idlelib.run" not in sys.modules:
eofkey = "CTRL-Z"
else:
eofkey = "CTRL-D"
banner = dedent(f"""
sqlite3 shell, running on SQLite version {sqlite3.sqlite_version}
Connected to {db_name}

Each command will be run using execute() on the cursor.
Type ".help" for more information; type ".quit" or {eofkey} to quit.
Type ".help" for more information; type ".quit" or {EOF_KEY} to quit.
""").strip()

theme = get_theme()
Expand Down
28 changes: 28 additions & 0 deletions Lib/test/test_sqlite3/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,34 @@ def test_interact(self):
self.assertEqual(out.count(self.PS1), 1)
self.assertEqual(out.count(self.PS2), 0)

@unittest.skipUnless(sys.platform == "win32", "Windows EOF is CTRL-Z")
def test_interact_banner_win(self):
_, err = self.run_cli()
self.assertIn('type ".quit" or CTRL-Z to quit', err)

@unittest.skipUnless(sys.platform != "win32", "Non-Windows EOF is CTRL-D")
def test_interact_banner_non_win(self):
_, err = self.run_cli()
self.assertIn('type ".quit" or CTRL-D to quit', err)

@unittest.skipUnless(sys.platform == "win32", "Windows EOF is CTRL-Z")
def test_interact_help_eof_win(self):
out, err = self.run_cli(commands=(".help",))
self.assertIn(self.MEMORY_DB_MSG, err)
self.assertIn("Exit the CLI, equivalent to CTRL-Z", out)
self.assertEndsWith(out, self.PS1)
self.assertEqual(out.count(self.PS1), 2)
self.assertEqual(out.count(self.PS2), 0)

@unittest.skipUnless(sys.platform != "win32", "Non-Windows EOF is CTRL-D")
def test_interact_help_eof_non_win(self):
out, err = self.run_cli(commands=(".help",))
self.assertIn(self.MEMORY_DB_MSG, err)
self.assertIn("Exit the CLI, equivalent to CTRL-D", out)
self.assertEndsWith(out, self.PS1)
self.assertEqual(out.count(self.PS1), 2)
self.assertEqual(out.count(self.PS2), 0)

def test_interact_quit(self):
out, err = self.run_cli(commands=(".quit",))
self.assertIn(self.MEMORY_DB_MSG, err)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Correct the EOF key shown in the :mod:`sqlite3` CLI ``.help`` text on Windows.
Loading