Skip to content

Commit 95723d0

Browse files
committed
gh-139105: Fix the EOF key shows on Windows for .help command
Signed-off-by: Keming <[email protected]>
1 parent 6ea4258 commit 95723d0

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

Lib/sqlite3/__main__.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ def execute(c, sql, suppress_errors=True, theme=theme_no_color):
4141
sys.exit(1)
4242

4343

44+
def _eof_key():
45+
if sys.platform == "win32" and "idlelib.run" not in sys.modules:
46+
return "CTRL-Z"
47+
else:
48+
return "CTRL-D"
49+
50+
4451
class SqliteInteractiveConsole(InteractiveConsole):
4552
"""A simple SQLite REPL."""
4653

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

119126
# Prepare REPL banner and prompts.
120-
if sys.platform == "win32" and "idlelib.run" not in sys.modules:
121-
eofkey = "CTRL-Z"
122-
else:
123-
eofkey = "CTRL-D"
124127
banner = dedent(f"""
125128
sqlite3 shell, running on SQLite version {sqlite3.sqlite_version}
126129
Connected to {db_name}
127130
128131
Each command will be run using execute() on the cursor.
129-
Type ".help" for more information; type ".quit" or {eofkey} to quit.
132+
Type ".help" for more information; type ".quit" or {_eof_key()} to quit.
130133
""").strip()
131134

132135
theme = get_theme()

Lib/test/test_sqlite3/test_cli.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,34 @@ def test_interact(self):
109109
self.assertEqual(out.count(self.PS1), 1)
110110
self.assertEqual(out.count(self.PS2), 0)
111111

112+
@unittest.skipUnless(sys.platform == "win32", "Windows EOF is CTRL-Z")
113+
def test_interact_banner_win(self):
114+
_, err = self.run_cli()
115+
self.assertIn('type ".quit" or CTRL-Z to quit', err)
116+
117+
@unittest.skipUnless(sys.platform != "win32", "Non-Windows EOF is CTRL-D")
118+
def test_interact_banner_non_win(self):
119+
_, err = self.run_cli()
120+
self.assertIn('type ".quit" or CTRL-D to quit', err)
121+
122+
@unittest.skipUnless(sys.platform == "win32", "Windows EOF is CTRL-Z")
123+
def test_interact_help_eof_win(self):
124+
out, err = self.run_cli(commands=(".help",))
125+
self.assertIn(self.MEMORY_DB_MSG, err)
126+
self.assertIn("Exit the CLI, equivalent to CTRL-Z", out)
127+
self.assertEndsWith(out, self.PS1)
128+
self.assertEqual(out.count(self.PS1), 2)
129+
self.assertEqual(out.count(self.PS2), 0)
130+
131+
@unittest.skipUnless(sys.platform != "win32", "Non-Windows EOF is CTRL-D")
132+
def test_interact_help_eof_non_win(self):
133+
out, err = self.run_cli(commands=(".help",))
134+
self.assertIn(self.MEMORY_DB_MSG, err)
135+
self.assertIn("Exit the CLI, equivalent to CTRL-D", out)
136+
self.assertEndsWith(out, self.PS1)
137+
self.assertEqual(out.count(self.PS1), 2)
138+
self.assertEqual(out.count(self.PS2), 0)
139+
112140
def test_interact_quit(self):
113141
out, err = self.run_cli(commands=(".quit",))
114142
self.assertIn(self.MEMORY_DB_MSG, err)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix the EOF key shows in sqlite3 `.help` command on Windows.

0 commit comments

Comments
 (0)