Skip to content

Commit beb8a50

Browse files
[3.14] gh-133439: Fix dot commands with trailing spaces are mistaken for multi-line sqlite statements in the sqlite3 command-line interface (GH-133440) (GH-133738)
(cherry picked from commit ebd4881) Co-authored-by: Tan Long <tanloong@foxmail.com>
1 parent 92d5677 commit beb8a50

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 if 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
@@ -116,6 +116,38 @@ def test_interact_version(self):
116116
self.assertEqual(out.count(self.PS2), 0)
117117
self.assertIn(sqlite3.sqlite_version, out)
118118

119+
def test_interact_empty_source(self):
120+
out, err = self.run_cli(commands=("", " "))
121+
self.assertIn(self.MEMORY_DB_MSG, err)
122+
self.assertEndsWith(out, self.PS1)
123+
self.assertEqual(out.count(self.PS1), 3)
124+
self.assertEqual(out.count(self.PS2), 0)
125+
126+
def test_interact_dot_commands_unknown(self):
127+
out, err = self.run_cli(commands=(".unknown_command", ))
128+
self.assertIn(self.MEMORY_DB_MSG, err)
129+
self.assertEndsWith(out, self.PS1)
130+
self.assertEqual(out.count(self.PS1), 2)
131+
self.assertEqual(out.count(self.PS2), 0)
132+
self.assertIn("Error", err)
133+
# test "unknown_command" is pointed out in the error message
134+
self.assertIn("unknown_command", err)
135+
136+
def test_interact_dot_commands_empty(self):
137+
out, err = self.run_cli(commands=("."))
138+
self.assertIn(self.MEMORY_DB_MSG, err)
139+
self.assertEndsWith(out, self.PS1)
140+
self.assertEqual(out.count(self.PS1), 2)
141+
self.assertEqual(out.count(self.PS2), 0)
142+
143+
def test_interact_dot_commands_with_whitespaces(self):
144+
out, err = self.run_cli(commands=(".version ", ". version"))
145+
self.assertIn(self.MEMORY_DB_MSG, err)
146+
self.assertEqual(out.count(sqlite3.sqlite_version + "\n"), 2)
147+
self.assertEndsWith(out, self.PS1)
148+
self.assertEqual(out.count(self.PS1), 3)
149+
self.assertEqual(out.count(self.PS2), 0)
150+
119151
def test_interact_valid_sql(self):
120152
out, err = self.run_cli(commands=("SELECT 1;",))
121153
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)