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:
committed by
Damien George
parent
8eb5636996
commit
09541b7896
@@ -218,6 +218,10 @@ static void print_completions(const mp_print_t *print,
|
||||
for (qstr q = q_first; q <= q_last; ++q) {
|
||||
size_t 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 (test_qstr(obj, q)) {
|
||||
int gap = (line_len + WORD_SLOT_LEN - 1) / WORD_SLOT_LEN * WORD_SLOT_LEN - line_len;
|
||||
|
||||
@@ -29,6 +29,7 @@ target-version = "py37"
|
||||
[tool.ruff.lint]
|
||||
exclude = [ # Ruff finds Python SyntaxError in these files
|
||||
"tests/cmdline/repl_autocomplete.py",
|
||||
"tests/cmdline/repl_autocomplete_underscore.py",
|
||||
"tests/cmdline/repl_autoindent.py",
|
||||
"tests/cmdline/repl_basic.py",
|
||||
"tests/cmdline/repl_cont.py",
|
||||
|
||||
33
tests/cmdline/repl_autocomplete_underscore.py
Normal file
33
tests/cmdline/repl_autocomplete_underscore.py
Normal 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}
|
||||
41
tests/cmdline/repl_autocomplete_underscore.py.exp
Normal file
41
tests/cmdline/repl_autocomplete_underscore.py.exp
Normal 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
|
||||
>>>
|
||||
@@ -51,16 +51,16 @@ RuntimeError:
|
||||
ame__
|
||||
port
|
||||
|
||||
builtins micropython _asyncio _thread
|
||||
array binascii btree cexample
|
||||
cmath collections cppexample cryptolib
|
||||
deflate errno example_package
|
||||
ffi framebuf gc hashlib
|
||||
heapq io json machine
|
||||
marshal math os platform
|
||||
random re select socket
|
||||
struct sys termios time
|
||||
tls uctypes vfs websocket
|
||||
builtins micropython array binascii
|
||||
btree cexample cmath collections
|
||||
cppexample cryptolib deflate errno
|
||||
example_package ffi framebuf
|
||||
gc hashlib heapq io
|
||||
json machine marshal math
|
||||
os platform random re
|
||||
select socket struct sys
|
||||
termios time tls uctypes
|
||||
vfs websocket
|
||||
me
|
||||
|
||||
micropython machine marshal math
|
||||
|
||||
Reference in New Issue
Block a user