Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
gh-133390: Support SQL keyword completion for sqlite3 CLI#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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
1b96be35e50871c1941cb47daca5c54c2f68fff491a766805da55014ca587e0311b4f370f46e99d03730bfcff38805d997276b4a709eeac8fc57d71c508069231b9e7121b06990a86cf5170733b40982a226ea9f4eebbd93f9b2c10410fa2bd0b9ce35a17e73dd16b3f3ea951a493ad334cfc78477b48b68bb4f34c3b1224f1221e38651318d4f659ccd98a5d681425ffd0f026188a6d370dd8b16b1674ea108ba13b527efafd1bb140818cfd6c89e588fb6a5623f1688c8d59b3a2b88File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -4,7 +4,6 @@ | ||
| import textwrap | ||
| import unittest | ||
| from sqlite3.__main__ import main as cli | ||
| from test.support.import_helper import import_module | ||
| from test.support.os_helper import TESTFN, unlink | ||
| @@ -209,76 +208,93 @@ def test_color(self): | ||
| @requires_subprocess() | ||
picnixz marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| @force_not_colorized_test_class | ||
| classCompletion(unittest.TestCase): | ||
| PS1 = "sqlite> " | ||
Member 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 ? Contributor 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. Can you explain your remark. Do you want changes? | ||
| @classmethod | ||
| def setUpClass(cls): | ||
| _sqlite3 = import_module("_sqlite3") | ||
| if not hasattr(_sqlite3, "SQLITE_KEYWORDS"): | ||
| raise unittest.SkipTest("unable to determine SQLite keywords") | ||
| readline = import_module("readline") | ||
| if readline.backend == "editline": | ||
| raise unittest.SkipTest("libedit readline is not supported") | ||
picnixz marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| def write_input(self,input_, env=None): | ||
| script = textwrap.dedent(""" | ||
| import readline | ||
| from sqlite3.__main__ import main | ||
| readline.parse_and_bind("set colored-completion-prefix off") | ||
| main() | ||
| """) | ||
| return run_pty(script,input_, env) | ||
| deftest_complete_sql_keywords(self): | ||
| # List candidates starting with 'S', there should be multiple matches. | ||
| input_ = b"S\t\tEL\t 1;\n.quit\n" | ||
| output = self.write_input(input_) | ||
| 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 = self.write_input(input_) | ||
| self.assertIn(b"SELECT", output) | ||
| self.assertIn(b"(1,)", output) | ||
| @unittest.skipIf(sys.platform.startswith("freebsd"), | ||
| "Two actual tabs are inserted when there are no matching" | ||
| " completions in the pseudo-terminal opened by run_pty()" | ||
| " on FreeBSD") | ||
| def test_complete_no_match(self): | ||
| input_ = b"xyzzy\t\t\b\b\b\b\b\b\b.quit\n" | ||
| # Set NO_COLOR to disable coloring for self.PS1. | ||
| output = self.write_input(input_, env={"NO_COLOR": "1"}) | ||
| lines = output.decode().splitlines() | ||
| indices = ( | ||
| i for i, line in enumerate(lines, 1) | ||
| if line.startswith(f"{self.PS1}xyzzy") | ||
| ) | ||
| line_num = next(indices, -1) | ||
| self.assertNotEqual(line_num, -1) | ||
| # Completions occupy lines, assert no extra lines when there is nothing | ||
| # to complete. | ||
| self.assertEqual(line_num, len(lines)) | ||
| def test_complete_no_input(self): | ||
| from _sqlite3 import SQLITE_KEYWORDS | ||
| script = textwrap.dedent(""" | ||
| import readline | ||
| from sqlite3.__main__ import main | ||
| # Configure readline to ...: | ||
| # - hide control sequences surrounding each candidate | ||
| # - hide "Display all xxx possibilities? (y or n)" | ||
| # - hide "--More--" | ||
| # - show candidates one per line | ||
| readline.parse_and_bind("set colored-completion-prefix off") | ||
| readline.parse_and_bind("set colored-stats off") | ||
| readline.parse_and_bind("set completion-query-items 0") | ||
| readline.parse_and_bind("set page-completions off") | ||
| readline.parse_and_bind("set completion-display-width 0") | ||
| main() | ||
| """) | ||
| input_ = b"\t\t.quit\n" | ||
| output = run_pty(script, input_, env={"NO_COLOR": "1"}) | ||
| lines = output.decode().splitlines() | ||
| indices = [ | ||
| i for i, line in enumerate(lines) | ||
| if line.startswith(self.PS1) | ||
| ] | ||
| self.assertEqual(len(indices), 2) | ||
| start, end = indices | ||
| candidates =[l.strip() for l in lines[start+1:end]] | ||
| self.assertEqual(candidates, sorted(SQLITE_KEYWORDS)) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -405,14 +405,14 @@ pysqlite_error_name(int rc) | ||
| return NULL; | ||
| } | ||
| static int | ||
| add_keyword_tuple(PyObject *module) | ||
| { | ||
| #if SQLITE_VERSION_NUMBER >= 3024000 | ||
| int count = sqlite3_keyword_count(); | ||
erlend-aasland marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| PyObject *keywords = PyTuple_New(count); | ||
| if (keywords == NULL) { | ||
| return -1; | ||
| } | ||
| for (int i = 0; i < count; i++) { | ||
| const char *keyword; | ||
| @@ -427,17 +427,17 @@ add_keyword_tuple(PyObject *module) | ||
| if (!kwd) { | ||
| goto error; | ||
| } | ||
| PyTuple_SET_ITEM(keywords, i, kwd); | ||
| } | ||
| return PyModule_Add(module, "SQLITE_KEYWORDS", keywords); | ||
| error: | ||
| Py_DECREF(keywords); | ||
| return -1; | ||
| #else | ||
| return 0; | ||
| #endif | ||
| } | ||
| static int | ||
| add_integer_constants(PyObject *module) { | ||
| @@ -737,11 +737,9 @@ module_exec(PyObject *module) | ||
| goto error; | ||
| } | ||
| if (add_keyword_tuple(module) < 0) { | ||
| goto error; | ||
| } | ||
| if (PyModule_AddStringConstant(module, "sqlite_version", sqlite3_libversion())) { | ||
| goto error; | ||