Skip to content

add supervisor.get_setting(); change os.getenv() behavior#10819

Open
dhalbert wants to merge 1 commit intoadafruit:mainfrom
dhalbert:improved-settings-toml-fetching
Open

add supervisor.get_setting(); change os.getenv() behavior#10819
dhalbert wants to merge 1 commit intoadafruit:mainfrom
dhalbert:improved-settings-toml-fetching

Conversation

@dhalbert
Copy link
Collaborator

See #9113 for the motivation for this PR.

  • os.getenv(key, default=None) now always returns a string. It never raises an error. It will return whatever is on the right hand side of key = <value> in settings.toml after removing leading and trailing whitepsace. The value is returnedeven if that value is not valid TOML, such as an unquoted string. If the key cannot be found or there is a pathological problem (bad Unicode escape, etc.), it will return the default value.
  • A new function supervisor.get_setting(key, default=None) is added. It parses key = <value>. The value must be one these kinds of valid TOML values:
    • a double-quoted string, and is returned as a str.
    • an integer, signed or unsigned. The value is returned as an int.
    • true or false, returned as the Python boolean True or False.
    • Any other kind of value raises ValueError. If key is not present, then the value of default is returned.

Internally, these changes were made:

  • The code in shared-module/os/getenv.c was moved to supervisor/shared/settings.c, and was slightly simplified. The routines were renamed from common_hal_os_getenv_* to settings_*. The error reporting is simpler and doesn't bother to report the particular offending bad character. Errors that occur when trying to read, say, CIRCUITPY_WIFI_SSID are still printed in the REPL stream for the user's benefit.
  • CIRCUITPY_OS_GETENV is now CIRCUITPY_SETTINGS_TOML.
  • The tricky code that tests os.getenv() in the unix micropython variant=COVERAGE build is now simpler and smaller. The test was updated to handle the new behavior of os.getenv().

The original code is now slightly smaller, but adding supervisor.get_setting() added about 260 bytes.

The original version of this code was developed using Copilot CLI using the GPT-5.3-Codex model and some fairly specific prompts about what I wanted. The code worked, but I then had further ideas about how to structure the code and do some cleanup, and did most of that by hand. I also reworked the documentation Codex wrote. Some refactoring and renaming was left to Copilot. I reviewed all the new diffs and new code.

Tested on Metro M4 and Metro ESP32-S3. Automatic WiFi access point connection worked as expected with settings.toml values as before. supervisor.get_setting() was tested manually.

Aside: I'd like to have written automated tests for supervisor.get_setting(), but that's not possible with the current UNIX micropython executable. The POSIX port (#9581) would be a move in that direction, as would, say an ARM executable on qemue or something like rp2040js/wokwi.

@dhalbert dhalbert force-pushed the improved-settings-toml-fetching branch from 987d9d5 to 4f9929b Compare February 15, 2026 03:06
@dhalbert dhalbert force-pushed the improved-settings-toml-fetching branch from 4f9929b to 4b2111e Compare February 15, 2026 03:48
@dhalbert dhalbert requested a review from tannewt February 15, 2026 12:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

os.getenv("A_NUMBER") can return an int

1 participant