From 2b64759e2a81f5bcce6b0289cdf0cae3375087ed Mon Sep 17 00:00:00 2001 From: lkk7 Date: Wed, 15 Jul 2026 16:31:44 +0200 Subject: [PATCH 1/3] gh-153768: Fix sqlite3 CLI completion for database names with quotes --- Lib/sqlite3/_completer.py | 14 ++++++++--- Lib/test/test_sqlite3/test_cli.py | 24 +++++++++++++++++++ ...-07-15-16-29-05.gh-issue-153768.Q-M37F.rst | 1 + 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-07-15-16-29-05.gh-issue-153768.Q-M37F.rst diff --git a/Lib/sqlite3/_completer.py b/Lib/sqlite3/_completer.py index ba580f968bf92dc..bbd8f4a3798c416 100644 --- a/Lib/sqlite3/_completer.py +++ b/Lib/sqlite3/_completer.py @@ -11,6 +11,14 @@ _completion_matches = [] +def _quote_identifier(value): + return '"' + value.replace('"', '""') + '"' + + +def _quote_string_literal(value): + return "'" + value.replace("'", "''") + "'" + + def _complete(con, text, state): global _completion_matches @@ -32,7 +40,7 @@ def _complete(con, text, state): # escape '_' which can appear in attached database names select_clauses = ( f"""\ - SELECT name || ' ' FROM \"{schema}\".sqlite_master + SELECT name || ' ' FROM {_quote_identifier(schema)}.sqlite_master WHERE name LIKE REPLACE(:text, '_', '^_') || '%' ESCAPE '^'""" for schema in schemata ) @@ -46,8 +54,8 @@ def _complete(con, text, state): try: select_clauses = ( f"""\ - SELECT pti.name || ' ' FROM "{schema}".sqlite_master AS sm - JOIN pragma_table_xinfo(sm.name,'{schema}') AS pti + SELECT pti.name || ' ' FROM {_quote_identifier(schema)}.sqlite_master AS sm + JOIN pragma_table_xinfo(sm.name,{_quote_string_literal(schema)}) AS pti WHERE sm.type='table' AND pti.name LIKE REPLACE(:text, '_', '^_') || '%' ESCAPE '^'""" for schema in schemata diff --git a/Lib/test/test_sqlite3/test_cli.py b/Lib/test/test_sqlite3/test_cli.py index 1fc0236780fa8ba..d31aaa800d1d3c0 100644 --- a/Lib/test/test_sqlite3/test_cli.py +++ b/Lib/test/test_sqlite3/test_cli.py @@ -209,6 +209,30 @@ def test_color(self): '\x1b[35mnear "sel": syntax error\x1b[0m', err) +class Completer(unittest.TestCase): + + def test_schema_name_with_double_quote(self): + from sqlite3._completer import _complete + + con = sqlite3.connect(":memory:") + self.addCleanup(con.close) + con.execute("ATTACH ? AS ?", (":memory:", 'double"quote')) + con.execute('CREATE TABLE [double"quote].table_match(value)') + self.assertEqual(_complete(con, "table_", 0), "table_match ") + + @unittest.skipIf(sqlite3.sqlite_version_info < (3, 16, 0), + "PRAGMA table-valued function is not available until " + "SQLite 3.16.0") + def test_schema_name_with_single_quote(self): + from sqlite3._completer import _complete + + con = sqlite3.connect(":memory:") + self.addCleanup(con.close) + con.execute("ATTACH ? AS ?", (":memory:", "single'quote")) + con.execute("CREATE TABLE [single'quote].other(column_match)") + self.assertEqual(_complete(con, "column_", 0), "column_match ") + + @requires_subprocess() @force_not_colorized_test_class class Completion(unittest.TestCase): diff --git a/Misc/NEWS.d/next/Library/2026-07-15-16-29-05.gh-issue-153768.Q-M37F.rst b/Misc/NEWS.d/next/Library/2026-07-15-16-29-05.gh-issue-153768.Q-M37F.rst new file mode 100644 index 000000000000000..c4b35ec6d252a72 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-15-16-29-05.gh-issue-153768.Q-M37F.rst @@ -0,0 +1 @@ +Fix sqlite3 CLI completion for attached database names containing quotes. From 1c83fd6b13cdcf5c882332a1c65f99adf38f3e3d Mon Sep 17 00:00:00 2001 From: lkk7 <14022447+lkk7@users.noreply.github.com> Date: Sun, 15 Nov 2026 23:06:12 +0100 Subject: [PATCH 2/3] gh-153768: Move quoted completion tests into an existing class --- Lib/test/test_sqlite3/test_cli.py | 45 ++++++++++++++----------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/Lib/test/test_sqlite3/test_cli.py b/Lib/test/test_sqlite3/test_cli.py index d31aaa800d1d3c0..4c6d6bc5cca3d03 100644 --- a/Lib/test/test_sqlite3/test_cli.py +++ b/Lib/test/test_sqlite3/test_cli.py @@ -209,30 +209,6 @@ def test_color(self): '\x1b[35mnear "sel": syntax error\x1b[0m', err) -class Completer(unittest.TestCase): - - def test_schema_name_with_double_quote(self): - from sqlite3._completer import _complete - - con = sqlite3.connect(":memory:") - self.addCleanup(con.close) - con.execute("ATTACH ? AS ?", (":memory:", 'double"quote')) - con.execute('CREATE TABLE [double"quote].table_match(value)') - self.assertEqual(_complete(con, "table_", 0), "table_match ") - - @unittest.skipIf(sqlite3.sqlite_version_info < (3, 16, 0), - "PRAGMA table-valued function is not available until " - "SQLite 3.16.0") - def test_schema_name_with_single_quote(self): - from sqlite3._completer import _complete - - con = sqlite3.connect(":memory:") - self.addCleanup(con.close) - con.execute("ATTACH ? AS ?", (":memory:", "single'quote")) - con.execute("CREATE TABLE [single'quote].other(column_match)") - self.assertEqual(_complete(con, "column_", 0), "column_match ") - - @requires_subprocess() @force_not_colorized_test_class class Completion(unittest.TestCase): @@ -311,6 +287,11 @@ def test_complete_table_indexes_triggers_views(self): ON _Attached_table BEGIN SELECT 1; END; CREATE VIEW attached._Attached_view AS SELECT 1; + ATTACH ':memory:' AS "double""quote"; + CREATE TABLE "double""quote"._Double_quote (id); + ATTACH ':memory:' AS "single'quote"; + CREATE TABLE "single'quote"._Single_quote (id); + SELECT id FROM _\t\tta\t; .quit\n""").encode() output = self.write_input(input_) @@ -325,7 +306,9 @@ def test_complete_table_indexes_triggers_views(self): "_Attached_table", "_Attached_trigger", "_Attached_view", + "_Double_quote", "_Index", + "_Single_quote", "_Table", "_Temp_index", "_Temp_table", @@ -350,6 +333,11 @@ def test_complete_columns(self): ATTACH ':memory:' AS attached; CREATE TABLE attached._attached_table (_col_attached); + ATTACH ':memory:' AS "double""quote"; + CREATE TABLE "double""quote"._double_quote (_col_double_quote); + ATTACH ':memory:' AS "single'quote"; + CREATE TABLE "single'quote"._single_quote (_col_single_quote); + SELECT _col_\t\tta\tFROM _table; .quit\n""").encode() output = self.write_input(input_) @@ -361,7 +349,14 @@ def test_complete_columns(self): candidates = [l.strip() for l in lines[start+1:end]] self.assertEqual( - candidates, ["_col_attached", "_col_table", "_col_temp"] + candidates, + [ + "_col_attached", + "_col_double_quote", + "_col_single_quote", + "_col_table", + "_col_temp", + ], ) @unittest.skipIf(sqlite3.sqlite_version_info < (3, 30, 0), From 60019e514d562cc01de2325853e10fbce5317fb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz?= <14022447+lkk7@users.noreply.github.com> Date: Mon, 16 Nov 2026 13:28:10 +0100 Subject: [PATCH 3/3] Update Misc/NEWS.d/next/Library/2026-07-15-16-29-05.gh-issue-153768.Q-M37F.rst Co-authored-by: Tan Long --- .../next/Library/2026-07-15-16-29-05.gh-issue-153768.Q-M37F.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2026-07-15-16-29-05.gh-issue-153768.Q-M37F.rst b/Misc/NEWS.d/next/Library/2026-07-15-16-29-05.gh-issue-153768.Q-M37F.rst index c4b35ec6d252a72..6ba7948bb15c9da 100644 --- a/Misc/NEWS.d/next/Library/2026-07-15-16-29-05.gh-issue-153768.Q-M37F.rst +++ b/Misc/NEWS.d/next/Library/2026-07-15-16-29-05.gh-issue-153768.Q-M37F.rst @@ -1 +1 @@ -Fix sqlite3 CLI completion for attached database names containing quotes. +Fix :mod:`sqlite3` CLI completion for attached database names containing quotes.