Skip to content

Commit 8afa0c6

Browse files
committed
gh-158215: Fix sqlite3 completer for schema names with a double quote
1 parent ee1bbf0 commit 8afa0c6

3 files changed

Lines changed: 30 additions & 4 deletions

File tree

‎Lib/sqlite3/_completer.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
_completion_matches = []
1212

1313

14+
def _quote_schema(schema):
15+
# Quote an SQLite identifier, doubling embedded double quotes.
16+
return '"' + schema.replace('"', '""') + '"'
17+
18+
1419
def _complete(con, text, state):
1520
global _completion_matches
1621

@@ -32,7 +37,7 @@ def _complete(con, text, state):
3237
# escape '_' which can appear in attached database names
3338
select_clauses = (
3439
f"""\
35-
SELECT name || ' ' FROM \"{schema}\".sqlite_master
40+
SELECT name || ' ' FROM {_quote_schema(schema)}.sqlite_master
3641
WHERE name LIKE REPLACE(:text, '_', '^_') || '%' ESCAPE '^'"""
3742
for schema in schemata
3843
)
@@ -46,8 +51,8 @@ def _complete(con, text, state):
4651
try:
4752
select_clauses = (
4853
f"""\
49-
SELECT pti.name || ' ' FROM "{schema}".sqlite_master AS sm
50-
JOIN pragma_table_xinfo(sm.name,'{schema}') AS pti
54+
SELECT pti.name || ' ' FROM {_quote_schema(schema)}.sqlite_master AS sm
55+
JOIN pragma_table_xinfo(sm.name,{_quote_schema(schema)}) AS pti
5156
WHERE sm.type='table' AND
5257
pti.name LIKE REPLACE(:text, '_', '^_') || '%' ESCAPE '^'"""
5358
for schema in schemata
@@ -108,4 +113,4 @@ def complete(text, state):
108113
readline.parse_and_bind(command_string)
109114
yield
110115
finally:
111-
readline.set_completer(old_completer)
116+
readline.set_completer(old_completer)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import sqlite3
2+
import unittest
3+
from sqlite3 import _completer
4+
5+
6+
class CompleterTests(unittest.TestCase):
7+
def test_schema_with_double_quote(self):
8+
con = sqlite3.connect(':memory:')
9+
self.addCleanup(con.close)
10+
con.execute('ATTACH DATABASE \':memory:\' AS \'weird"name\'')
11+
matches = []
12+
state = 0
13+
while True:
14+
match = _completer._complete(con, 'a', state)
15+
if match is None:
16+
break
17+
matches.append(match)
18+
state += 1
19+
self.assertTrue(matches)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :mod:`sqlite3` completer to handle attached database names
2+
containing a double quote. Patch by Tony Leung.

0 commit comments

Comments
 (0)