-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-133390: Support basic completion for sqlite3 command-line interface #133393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 21 commits
1b96be3
5e50871
c1941cb
47daca5
c54c2f6
8fff491
a766805
da55014
ca587e0
311b4f3
70f46e9
9d03730
bfcff38
805d997
276b4a7
09eeac8
fc57d71
c508069
231b9e7
121b069
90a86cf
5170733
b40982a
226ea9f
4eebbd9
3f9b2c1
0410fa2
bd0b9ce
35a17e7
3dd16b3
f3ea951
a493ad3
34cfc78
477b48b
68bb4f3
4c3b122
4f1221e
3865131
8d4f659
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from _sqlite3 import SQLITE_KEYWORDS | ||
from contextlib import contextmanager | ||
|
||
_completion_matches = [] | ||
|
||
|
||
def _complete(text, state): | ||
global _completion_matches | ||
if state == 0: | ||
_completion_matches = [c + " " for c in SQLITE_KEYWORDS if c.startswith(text.upper())] | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try: | ||
return _completion_matches[state] | ||
except IndexError: | ||
return None | ||
|
||
|
||
@contextmanager | ||
def enable_completer(): | ||
try: | ||
import readline | ||
except ImportError: | ||
yield | ||
return | ||
|
||
old_completer = readline.get_completer() | ||
try: | ||
readline.set_completer(_complete) | ||
if readline.backend == "editline": | ||
# libedit uses "^I" instead of "tab" | ||
tanloong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
command_string = "bind ^I rl_complete" | ||
else: | ||
command_string = "tab: complete" | ||
readline.parse_and_bind(command_string) | ||
yield | ||
finally: | ||
readline.set_completer(old_completer) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,19 @@ | ||
"""sqlite3 CLI tests.""" | ||
import sqlite3 | ||
import textwrap | ||
import unittest | ||
|
||
from _sqlite3 import SQLITE_KEYWORDS | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
from sqlite3.__main__ import main as cli | ||
from test.support.import_helper import import_module | ||
from test.support.os_helper import TESTFN, unlink | ||
from test.support.pty_helper import run_pty | ||
from test.support import ( | ||
captured_stdout, | ||
captured_stderr, | ||
captured_stdin, | ||
force_not_colorized_test_class, | ||
requires_subprocess, | ||
) | ||
|
||
|
||
|
@@ -200,5 +205,74 @@ def test_color(self): | |
self.assertIn('\x1b[1;35mOperationalError (SQLITE_ERROR)\x1b[0m: ' | ||
'\x1b[35mnear "sel": syntax error\x1b[0m', err) | ||
|
||
@requires_subprocess() | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class CompletionTest(unittest.TestCase): | ||
tanloong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
PS1 = "sqlite> " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is no longer freely customizable by users via sys.ps1 ? |
||
|
||
@classmethod | ||
def setUpClass(cls): | ||
readline = import_module("readline") | ||
if readline.backend == "editline": | ||
raise unittest.SkipTest("libedit readline is not supported") | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def test_keyword_completion(self): | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
script = textwrap.dedent(""" | ||
import readline | ||
readline.parse_and_bind("set colored-completion-prefix off") | ||
from sqlite3.__main__ import main; main() | ||
""") | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# List candidates starting with 'S', there should be multiple matches. | ||
input = b"S\t\tEL\t 1;\n.quit\n" | ||
output = run_pty(script, input) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pass There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have @force_not_colorized in support? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mmh, something is wrong with the colorization? and I'm not sure There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I notice that test_pdb.py passes CI even without either passing I added |
||
self.assertIn(b"SELECT", output) | ||
self.assertIn(b"SET", output) | ||
self.assertIn(b"SAVEPOINT", output) | ||
self.assertIn(b"(1,)", output) | ||
|
||
# Keywords are completed in upper case for even lower case user input | ||
input = b"sel\t\t 1;\n.quit\n" | ||
output = run_pty(script, input) | ||
self.assertIn(b"SELECT", output) | ||
self.assertIn(b"(1,)", output) | ||
|
||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def test_nothing_to_complete(self): | ||
script = textwrap.dedent(""" | ||
import readline | ||
readline.parse_and_bind("set colored-completion-prefix off") | ||
from sqlite3.__main__ import main; main() | ||
""") | ||
input = b"xyzzy\t\t\b\b\b\b\b.quit\n" | ||
output = run_pty(script, input, env={"NO_COLOR": "1"}) | ||
output_lines = output.decode().splitlines() | ||
line_num = next(i for i, line in enumerate(output_lines, 1) | ||
if line.startswith(f"{self.PS1}xyzzy")) | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# completions occupy lines, assert no extra lines when there is nothing | ||
# to complete | ||
self.assertEqual(line_num, len(output_lines)) | ||
|
||
def test_completion_order(self): | ||
script = textwrap.dedent(""" | ||
import readline | ||
readline.parse_and_bind("set colored-completion-prefix off") | ||
# hide control sequences surrounding each candidate | ||
readline.parse_and_bind("set colored-stats off") | ||
# hide "Display all xxx possibilities? (y or n)" | ||
readline.parse_and_bind("set completion-query-items 0") | ||
# hide "--More--" | ||
readline.parse_and_bind("set page-completions off") | ||
# show candidates one per line | ||
readline.parse_and_bind("set completion-display-width 0") | ||
from sqlite3.__main__ import main; main() | ||
""") | ||
input = b"\t\t.quit\n" | ||
output = run_pty(script, input, env={"NO_COLOR": "1"}) | ||
output_lines = output.decode().splitlines() | ||
candidates = [] | ||
for line in output_lines[-2::-1]: | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if line.startswith(self.PS1): | ||
break | ||
candidates.append(line.strip()) | ||
self.assertEqual(sorted(candidates, reverse=True), candidates) | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if __name__ == "__main__": | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
unittest.main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Support keyword completion in the :mod:`sqlite3` command-line interface. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -404,6 +404,54 @@ pysqlite_error_name(int rc) | |
return NULL; | ||
} | ||
|
||
static int | ||
add_sequence_constants(PyObject *module) | ||
{ | ||
PyObject *kwd; | ||
const char *_keywords[] = { | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ALWAYS", "ANALYZE", | ||
"AND", "AS", "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", | ||
"BETWEEN", "BY", "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", | ||
"COLUMN", "COMMIT", "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", | ||
"CURRENT", "CURRENT_DATE", "CURRENT_TIME", "CURRENT_TIMESTAMP", | ||
"DATABASE", "DEFAULT", "DEFERRABLE", "DEFERRED", "DELETE", "DESC", | ||
"DETACH", "DISTINCT", "DO", "DROP", "EACH", "ELSE", "END", "ESCAPE", | ||
"EXCEPT", "EXCLUDE", "EXCLUSIVE", "EXISTS", "EXPLAIN", "FAIL", | ||
"FILTER", "FIRST", "FOLLOWING", "FOR", "FOREIGN", "FROM", "FULL", | ||
"GENERATED", "GLOB", "GROUP", "GROUPS", "HAVING", "IF", "IGNORE", | ||
"IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER", "INSERT", | ||
"INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY", "LAST", | ||
"LEFT", "LIKE", "LIMIT", "MATCH", "MATERIALIZED", "NATURAL", "NO", | ||
"NOT", "NOTHING", "NOTNULL", "NULL", "NULLS", "OF", "OFFSET", "ON", | ||
"OR", "ORDER", "OTHERS", "OUTER", "OVER", "PARTITION", "PLAN", | ||
"PRAGMA", "PRECEDING", "PRIMARY", "QUERY", "RAISE", "RANGE", | ||
"RECURSIVE", "REFERENCES", "REGEXP", "REINDEX", "RELEASE", "RENAME", | ||
"REPLACE", "RESTRICT", "RETURNING", "RIGHT", "ROLLBACK", "ROW", "ROWS", | ||
"SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP", "TEMPORARY", "THEN", | ||
"TIES", "TO", "TRANSACTION", "TRIGGER", "UNBOUNDED", "UNION", "UNIQUE", | ||
"UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", | ||
"WHERE", "WINDOW", "WITH", "WITHOUT", NULL | ||
}; | ||
PyObject *keywords = PyTuple_New(147); | ||
|
||
if (keywords == NULL) { | ||
return -1; | ||
} | ||
for (int i = 0; _keywords[i] != NULL; i++) { | ||
kwd = PyUnicode_FromString(_keywords[i]); | ||
if (PyTuple_SetItem(keywords, i, kwd) != 0) { | ||
Py_DECREF(kwd); | ||
Py_DECREF(keywords); | ||
return -1; | ||
} | ||
} | ||
if (PyModule_Add(module, "SQLITE_KEYWORDS", keywords) < 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cc @encukou who implemented this I believe. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May I try to update the C code. As this PR is sent from my fork, it would be more convenient that I make the change. I will convert if I mess it up. |
||
Py_DECREF(keywords); | ||
return -1; | ||
} | ||
return 0; | ||
} | ||
|
||
static int | ||
add_integer_constants(PyObject *module) { | ||
#define ADD_INT(ival) \ | ||
|
@@ -702,6 +750,11 @@ module_exec(PyObject *module) | |
goto error; | ||
} | ||
|
||
/* Set sequence constants */ | ||
if (add_sequence_constants(module) < 0) { | ||
goto error; | ||
} | ||
|
||
if (PyModule_AddStringConstant(module, "sqlite_version", sqlite3_libversion())) { | ||
goto error; | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.