py/repl: Skip private variables when printing tab completion options.

Any '_' variables/functions in frozen modules are currently printed, when
they shouldn't be.  That's due to underscore names possibly existing
between the start and end qstrs which are used to print the auto-complete
matches.  The underscore names should be skipped when iterating between the
two boundary qstrs.

The underscore attributes are removed from the extra coverage exp file
because tab completing "import <tab>" no longer lists modules beginning
with an underscore.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
This commit is contained in:
Andrew Leech
2025-03-26 11:29:54 +11:00
committed by Damien George
parent 8eb5636996
commit 09541b7896
5 changed files with 89 additions and 10 deletions

View File

@@ -218,6 +218,10 @@ static void print_completions(const mp_print_t *print,
for (qstr q = q_first; q <= q_last; ++q) { for (qstr q = q_first; q <= q_last; ++q) {
size_t d_len; size_t d_len;
const char *d_str = (const char *)qstr_data(q, &d_len); const char *d_str = (const char *)qstr_data(q, &d_len);
// filter out words that begin with underscore unless there's already a partial match
if (s_len == 0 && d_str[0] == '_') {
continue;
}
if (s_len <= d_len && strncmp(s_start, d_str, s_len) == 0) { if (s_len <= d_len && strncmp(s_start, d_str, s_len) == 0) {
if (test_qstr(obj, q)) { if (test_qstr(obj, q)) {
int gap = (line_len + WORD_SLOT_LEN - 1) / WORD_SLOT_LEN * WORD_SLOT_LEN - line_len; int gap = (line_len + WORD_SLOT_LEN - 1) / WORD_SLOT_LEN * WORD_SLOT_LEN - line_len;

View File

@@ -29,6 +29,7 @@ target-version = "py37"
[tool.ruff.lint] [tool.ruff.lint]
exclude = [ # Ruff finds Python SyntaxError in these files exclude = [ # Ruff finds Python SyntaxError in these files
"tests/cmdline/repl_autocomplete.py", "tests/cmdline/repl_autocomplete.py",
"tests/cmdline/repl_autocomplete_underscore.py",
"tests/cmdline/repl_autoindent.py", "tests/cmdline/repl_autoindent.py",
"tests/cmdline/repl_basic.py", "tests/cmdline/repl_basic.py",
"tests/cmdline/repl_cont.py", "tests/cmdline/repl_cont.py",

View File

@@ -0,0 +1,33 @@
# Test REPL autocompletion filtering of underscore attributes
# Start paste mode
{\x05}
class TestClass:
def __init__(self):
self.public_attr = 1
self._private_attr = 2
self.__very_private = 3
def public_method(self):
pass
def _private_method(self):
pass
@property
def public_property(self):
return 42
@property
def _private_property(self):
return 99
{\x04}
# Paste executed
# Create an instance
obj = TestClass()
# Test tab completion on the instance
# The tab character after `obj.` and 'a' below triggers the completions
obj.{\x09}{\x09}a{\x09}

View File

@@ -0,0 +1,41 @@
MicroPython \.\+ version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> # Test REPL autocompletion filtering of underscore attributes
>>>
>>> # Start paste mode
>>>
paste mode; Ctrl-C to cancel, Ctrl-D to finish
===
=== class TestClass:
=== def __init__(self):
=== self.public_attr = 1
=== self._private_attr = 2
=== self.__very_private = 3
===
=== def public_method(self):
=== pass
===
=== def _private_method(self):
=== pass
===
=== @property
=== def public_property(self):
=== return 42
===
=== @property
=== def _private_property(self):
=== return 99
===
===
>>> # Paste executed
>>>
>>> # Create an instance
>>> obj = TestClass()
>>>
>>> # Test tab completion on the instance
>>> # The tab character after `obj.` and 'a' below triggers the completions
>>> obj.public_
public_attr public_method public_property
>>> obj.public_attr
1
>>>

View File

@@ -51,16 +51,16 @@ RuntimeError:
ame__ ame__
port port
builtins micropython _asyncio _thread builtins micropython array binascii
array binascii btree cexample btree cexample cmath collections
cmath collections cppexample cryptolib cppexample cryptolib deflate errno
deflate errno example_package example_package ffi framebuf
ffi framebuf gc hashlib gc hashlib heapq io
heapq io json machine json machine marshal math
marshal math os platform os platform random re
random re select socket select socket struct sys
struct sys termios time termios time tls uctypes
tls uctypes vfs websocket vfs websocket
me me
micropython machine marshal math micropython machine marshal math