Skip to content

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

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

andrewleech
Copy link
Contributor

Summary

The tab completion on repl is intended to not show any attributes starting with a single _ character, however variables/functions in frozen modules are are currently shown.

This PR introduces a change to py/repl to skip private variables when printing tab completion options.

Originally included in #17011

Testing

A unit test has added which covers this, currently based on the asyncio module which is generally frozen in.
However looking again now maybe it would be better to add a suitable function / attribute to ./tests/frozen/frozentest.py and test against that, assuming it should have the same behavior?

Copy link

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:   +56 +0.030% 
   unix x64:   +64 +0.007% standard
      stm32:   +24 +0.006% PYBV10
     mimxrt:   +24 +0.006% TEENSY40
        rp2:   +16 +0.002% RPI_PICO_W
       samd:   +24 +0.009% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:   +24 +0.005% VIRT_RV32

py/repl.c Outdated
@@ -218,7 +218,8 @@ 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);
if (s_len <= d_len && strncmp(s_start, d_str, s_len) == 0) {
// check if item matches prefix and it's not a hidden attribute (starting with single underscore)
if (s_len <= d_len && strncmp(s_start, d_str, s_len) == 0 && (d_str[0] != '_' || (d_len > 1 && d_str[1] == '_'))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be simpler and maybe smaller code to follow the pattern of find_completions() like this:

        if (s_len == 0 && d_str[0] == '_') {
            continue;
        }                                        
        if (s_len <= d_len && strncmp(...) == 0) {

?

@dpgeorge
Copy link
Member

It's actually not easy to make a test for this that is robust because the bug relies on a certain ordering of qstrs in the frozen set of qstrs (it needs qstrs beginning with underscore to appear between the first and last items that match the tab completion).

So maybe it's not worth adding a test for? Does the test you added fail prior to the fix in this PR?

@dpgeorge dpgeorge added py-core Relates to py/ directory in source board-definition New or updated board definition files. Combine with a port- label. and removed board-definition New or updated board definition files. Combine with a port- label. labels Apr 10, 2025
@andrewleech
Copy link
Contributor Author

I'll test without the fix again; asyncio did include an _attr in the tab completion when I tested it manually with standard Unix build beforehand though.

This test also highlighted an issue in the original version of this commit in that it was also hiding __foo__ style attributes too.

@andrewleech andrewleech force-pushed the fix-repl-autocomplete branch from 8c01246 to 9dceed4 Compare April 10, 2025 12:21
Any '_' variables/functions in frozen modules are currently printed.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
@andrewleech andrewleech force-pushed the fix-repl-autocomplete branch from 9dceed4 to ee5694f Compare April 11, 2025 12:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
py-core Relates to py/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants