8000 gh-133439: Fix dot commands with trailing spaces are mistaken for mul… · python/cpython@0741f45 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0741f45

Browse files
committed
gh-133439: Fix dot commands with trailing spaces are mistaken for multi-line sqlite statements in the sqlite3 command-line interface (GH-133440)
(cherry picked from commit ebd4881)
1 parent d14a1b7 commit 0741f45

File tree

3 files changed

+53
-11
lines changed

3 files changed

+53
-11
lines changed

Lib/sqlite3/__main__.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,25 @@ def runsource(self, source, filename="<input>", symbol="single"):
4848
Return True if more input is needed; buffering is done automatically.
4949
Return False is input is a complete statement ready for execution.
5050
"""
51-
match source:
52-
case ".version":
53-
print(f"{sqlite3.sqlite_version}")
54-
case ".help":
55-
print("Enter SQL code and press enter.")
56-
case ".quit":
57-
sys.exit(0)
58-
case _:
59-
if not sqlite3.complete_statement(source):
60-
return True
61-
execute(self._cur, source)
51+
if not source or source.isspace():
52+
return False
53+
if source[0] == ".":
54+
match source[1:].strip():
55+
case "version":
56+
print(f"{sqlite3.sqlite_version}")
57+
case "help":
58+
print("Enter SQL code and press enter.")
59+
case "quit":
60+
sys.exit(0)
61+
case "":
62+
pass
63+
case _ as unknown:
64+
self.write("Error: unknown command or invalid arguments:"
65+
f' "{unknown}".\n')
66+
else:
67+
if not sqlite3.complete_statement(source):
68+
return True
69+
execute(self._cur, source)
6270
return False
6371

6472

Lib/test/test_sqlite3/test_cli.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,38 @@ def test_interact_version(self):
108108
self.assertEqual(out.count(self.PS2), 0)
109109
self.assertIn(sqlite3.sqlite_version, out)
110110

111+
def test_interact_empty_source(self):
112+
out, err = self.run_cli(commands=("", " "))
113+
self.assertIn(self.MEMORY_DB_MSG, err)
114+
self.assertEndsWith(out, self.PS1)
115+
self.assertEqual(out.count(self.PS1), 3)
116+
self.assertEqual(out.count(self.PS2), 0)
117+
118+
def test_interact_dot_commands_unknown(self):
119+
out, err = self.run_cli(commands=(".unknown_command", ))
120+
self.assertIn(self.MEMORY_DB_MSG, err)
121+
self.assertEndsWith(out, self.PS1)
122+
self.assertEqual(out.count(self.PS1), 2)
123+
self.assertEqual(out.count(self.PS2), 0)
124+
self.assertIn("Error", err)
125+
# test "unknown_command" is pointed out in the error message
126+
self.assertIn("unknown_command", err)
127+
128+
def test_interact_dot_commands_empty(self):
129+
out, err = self.run_cli(commands=("."))
130+
self.assertIn(self.MEMORY_DB_MSG, err)
131+
self.assertEndsWith(out, self.PS1)
132+
self.assertEqual(out.count(self.PS1), 2)
133+
self.assertEqual(out.count(self.PS2), 0)
134+
135+
def test_interact_dot_commands_with_whitespaces(self):
136+
out, err = self.run_cli(commands=(".version ", ". version"))
137+
self.assertIn(self.MEMORY_DB_MSG, err)
138+
self.assertEqual(out.count(sqlite3.sqlite_version + "\n"), 2)
139+
self.assertEndsWith(out, self.PS1)
140+
self.assertEqual(out.count(self.PS1), 3)
141+
self.assertEqual(out.count(self.PS2), 0)
142+
111143
def test_interact_valid_sql(self):
112144
out, err = self.run_cli(commands=("SELECT 1;",))
113145
self.assertIn(self.MEMORY_DB_MSG, err)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix dot commands with trailing spaces are mistaken for multi-line SQL
2+
statements in the sqlite3 command-line interface.

0 commit comments

Comments
 (0)
0