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: 10 additions & 1 deletion Lib/sqlite3/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,16 @@ def runsource(self, source, filename="<input>", symbol="single"):
self.write(f'{t.type}Error{t.reset}: {t.message}unknown '
f'command: "{unknown}"{t.reset}\n')
else:
if not sqlite3.complete_statement(source):
try:
complete = sqlite3.complete_statement(source)
# NUL -> ValueError, lone surrogate -> UnicodeEncodeError; keep
# narrow so real bugs still surface.
except (ValueError, UnicodeEncodeError) as e:
t = theme.traceback
self.write(f"{t.type}{type(e).__name__}{t.reset}: "
f"{t.message}{e}{t.reset}\n")
return False
if not complete:
return True
execute(self._cur, source, theme=theme)
return False
Expand Down
22 changes: 22 additions & 0 deletions Lib/test/test_sqlite3/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,28 @@ def test_interact_invalid_sql(self):
self.assertEqual(out.count(self.PS1), 2)
self.assertEqual(out.count(self.PS2), 0)

def test_interact_null_byte(self):
# NUL byte -> ValueError from complete_statement().
out, err = self.run_cli(commands=("SELECT '\0';", "SELECT 1;"))
self.assertIn(self.MEMORY_DB_MSG, err)
self.assertNotIn("Traceback (most recent call last)", err)
self.assertIn("ValueError: ", err)
self.assertIn("(1,)\n", out)
self.assertEndsWith(out, self.PS1)
self.assertEqual(out.count(self.PS1), 3)
self.assertEqual(out.count(self.PS2), 0)

def test_interact_lone_surrogate(self):
# Lone surrogate -> UnicodeEncodeError from complete_statement().
out, err = self.run_cli(commands=("SELECT '\udc80';", "SELECT 1;"))
self.assertIn(self.MEMORY_DB_MSG, err)
self.assertNotIn("Traceback (most recent call last)", err)
self.assertIn("UnicodeEncodeError: ", err)
self.assertIn("(1,)\n", out)
self.assertEndsWith(out, self.PS1)
self.assertEqual(out.count(self.PS1), 3)
self.assertEqual(out.count(self.PS2), 0)

def test_interact_on_disk_file(self):
self.addCleanup(unlink, TESTFN)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix the :mod:`sqlite3` command-line interface so that an input line
containing an embedded null character or a lone surrogate no longer crashes
the interactive shell; an error is now printed and the shell continues.
Patch by tonghuaroot.
Loading