Skip to content

Commit 3459fe3

Browse files
committed
Withdraw the _environ module
This existed solely for testing, so expose it a different way during the unix coverage build Also turn off os.getenv support on samd21.
1 parent 243ecc2 commit 3459fe3

File tree

22 files changed

+93
-226
lines changed

22 files changed

+93
-226
lines changed

devices/ble_hci/common-hal/_bleio/Adapter.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
#include "shared-bindings/_bleio/ScanEntry.h"
5050
#include "shared-bindings/time/__init__.h"
5151

52-
#if CIRCUITPY_ENVIRON
53-
#include "shared-bindings/_environ/__init__.h"
52+
#if CIRCUITPY_OS_GETENV
53+
#include "shared-bindings/os/__init__.h"
5454
#endif
5555

5656
#define MSEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000) / (RESOLUTION))
@@ -284,8 +284,8 @@ char default_ble_name[] = { 'C', 'I', 'R', 'C', 'U', 'I', 'T', 'P', 'Y', 0, 0, 0
284284
STATIC void bleio_adapter_hci_init(bleio_adapter_obj_t *self) {
285285
mp_int_t name_len = 0;
286286

287-
#if CIRCUITPY_ENVIRON
288-
mp_obj_t name = common_hal__environ_get_key("CIRCUITPY_BLE_NAME");
287+
#if CIRCUITPY_OS_GETENV
288+
mp_obj_t name = common_hal_os_getenv("CIRCUITPY_BLE_NAME", mp_const_none);
289289
if (name != mp_const_none) {
290290
mp_arg_validate_type_string(name, MP_QSTR_CIRCUITPY_BLE_NAME);
291291
self->name = name;

docs/environment.rst

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,33 @@ variables are commonly used to store "secrets" such as Wi-Fi passwords and API
66
keys. This method *does not* make them secure. It only separates them from the
77
code.
88

9-
CircuitPython supports these by parsing a subset of the `toml <https://toml.io/>`_ file format internally.
9+
CircuitPython uses a file called ``settings.toml`` at the drive root (no
10+
folder) as the environment. User code can access the values from the file
11+
using `os.getenv()`. It is recommended to save any values used repeatedly in a
12+
variable because `os.getenv()` will parse the ``settings.toml`` file contents
13+
on every access.
1014

11-
Here is a simple example:
15+
CircuitPython only supports a subset of the full toml specification, see below
16+
for more details. The subset is very "Python-like", which is a key reason we
17+
selected the format.
1218

13-
.. code-block:: bash
19+
Due to technical limitations it probably also accepts some files that are
20+
not valid TOML files; bugs of this nature are subject to change (i.e., be
21+
fixed) without the usual deprecation period for incompatible changes.
1422

15-
KEY1="value1"
16-
# Comment
17-
KEY2="value2\ncontains a newline"
23+
File format example:
1824

19-
[SECTION] # Only values in the "root table" are parsed
20-
SECTION_VALUE = ... # so this value cannot be seen by getenv
25+
.. code-block::
26+
27+
str_key="Hello world" # with trailing comment
28+
int_key = 7
29+
unicode_key="👨"
30+
unicode_key2="\\U0001f468" # same as above
31+
escape_codes="supported, including \\r\\n\\"\\\\"
32+
# comment
33+
[subtable]
34+
subvalue="cannot retrieve this using _environ or getenv"
2135
22-
CircuitPython uses the ``settings.toml`` at the drive root (no folder) as the environment.
23-
User code can access the values from the file using `os.getenv()`. It is
24-
recommended to save any values used repeatedly in a variable because `os.getenv()`
25-
will parse the ``settings.toml`` file contents on every access.
2636
2737
Details of the toml language subset
2838
-----------------------------------
@@ -36,6 +46,8 @@ Details of the toml language subset
3646
* Duplicate keys are not diagnosed.
3747
* Comments are supported
3848
* Only values from the "root table" can be retrieved
49+
* due to technical limitations, the content of multi-line
50+
strings can erroneously be parsed as a value.
3951

4052
CircuitPython behavior
4153
----------------------

ports/atmel-samd/mpconfigport.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ CIRCUITPY_BLEIO_HCI = 0
3333
CIRCUITPY_BUILTINS_POW3 ?= 0
3434
CIRCUITPY_COMPUTED_GOTO_SAVE_SPACE ?= 1
3535
CIRCUITPY_COUNTIO ?= 0
36+
CIRCUITPY_OS_GETENV ?= 0
3637
# Not enough RAM for framebuffers
3738
CIRCUITPY_FRAMEBUFFERIO ?= 0
3839
CIRCUITPY_FREQUENCYIO ?= 0

ports/espressif/common-hal/_bleio/Adapter.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@
5959
#include "esp_bt.h"
6060
#include "esp_nimble_hci.h"
6161

62-
#if CIRCUITPY_ENVIRON
63-
#include "shared-module/_environ/__init__.h"
62+
#if CIRCUITPY_OS_GETENV
63+
#include "shared-module/os/__init__.h"
6464
#endif
6565

6666
bleio_connection_internal_t bleio_connections[BLEIO_TOTAL_CONNECTION_COUNT];
@@ -101,9 +101,9 @@ void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enable
101101
ble_hs_cfg.sync_cb = _on_sync;
102102
// ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
103103

104-
#if CIRCUITPY_ENVIRON
104+
#if CIRCUITPY_OS_GETENV
105105
char ble_name[1 + MYNEWT_VAL_BLE_SVC_GAP_DEVICE_NAME_MAX_LENGTH];
106-
_environ_err_t result = _environ_get_key_str("CIRCUITPY_BLE_NAME", ble_name, sizeof(ble_name));
106+
os_environ_err_t result = common_hal_os_environ_get_key_str("CIRCUITPY_BLE_NAME", ble_name, sizeof(ble_name));
107107
if (result == ENVIRON_OK) {
108108
ble_svc_gap_device_name_set(ble_name);
109109
} else

ports/espressif/supervisor/port.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
#include "shared-bindings/microcontroller/RunMode.h"
5656
#include "shared-bindings/rtc/__init__.h"
5757
#include "shared-bindings/socketpool/__init__.h"
58-
#include "shared-module/_environ/__init__.h"
58+
#include "shared-module/os/__init__.h"
5959

6060
#include "peripherals/rmt.h"
6161
#include "peripherals/timer.h"
@@ -519,7 +519,7 @@ void port_idle_until_interrupt(void) {
519519
void port_post_boot_py(bool heap_valid) {
520520
if (!heap_valid && filesystem_present()) {
521521
mp_int_t reserved;
522-
if (_environ_get_key_int("CIRCUITPY_RESERVED_PSRAM", &reserved) == ENVIRON_OK) {
522+
if (common_hal_os_environ_get_key_int("CIRCUITPY_RESERVED_PSRAM", &reserved) == ENVIRON_OK) {
523523
common_hal_espidf_set_reserved_psram(reserved);
524524
}
525525
common_hal_espidf_reserve_psram();

ports/nrf/common-hal/_bleio/Adapter.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@
5252
#include "shared-bindings/_bleio/ScanEntry.h"
5353
#include "shared-bindings/time/__init__.h"
5454

55-
#if CIRCUITPY_ENVIRON
56-
#include "shared-bindings/_environ/__init__.h"
55+
#if CIRCUITPY_OS_GETENV
56+
#include "shared-bindings/os/getenv.h"
5757
#endif
5858

5959
#define BLE_MIN_CONN_INTERVAL MSEC_TO_UNITS(15, UNIT_0_625_MS)
@@ -345,8 +345,8 @@ STATIC void bleio_adapter_reset_name(bleio_adapter_obj_t *self) {
345345

346346
mp_int_t name_len = 0;
347347

348-
#if CIRCUITPY_ENVIRON
349-
mp_obj_t ble_name = common_hal__environ_get_key("CIRCUITPY_BLE_NAME");
348+
#if CIRCUITPY_OS_GETENV
349+
mp_obj_t ble_name = common_hal_os_environ_get_key("CIRCUITPY_BLE_NAME");
350350
if (ble_name != mp_const_none) {
351351
common_hal_bleio_adapter_set_name(self, mp_obj_str_get_str(ble_name));
352352
return;

ports/unix/coverage.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ STATIC const mp_rom_map_elem_t rawfile_locals_dict_table[] = {
9595
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
9696
{ MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
9797
{ MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&mp_stream_ioctl_obj) },
98+
{ MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&mp_stream_ioctl_obj) },
9899
};
99100

100101
STATIC MP_DEFINE_CONST_DICT(rawfile_locals_dict, rawfile_locals_dict_table);

ports/unix/main.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,13 @@ STATIC void set_sys_argv(char *argv[], int argc, int start_arg) {
420420
#define PATHLIST_SEP_CHAR ':'
421421
#endif
422422

423+
mp_obj_t common_hal_os_getenv_path(const char *path, const char *key, mp_obj_t default_);
424+
STATIC mp_obj_t get_key(mp_obj_t path_in, mp_obj_t key_to_get_in) {
425+
return common_hal_os_getenv_path(mp_obj_str_get_str(path_in),
426+
mp_obj_str_get_str(key_to_get_in), mp_const_none);
427+
}
428+
MP_DEFINE_CONST_FUN_OBJ_2(get_key_obj, get_key);
429+
423430
MP_NOINLINE int main_(int argc, char **argv);
424431

425432
int main(int argc, char **argv) {
@@ -540,6 +547,7 @@ MP_NOINLINE int main_(int argc, char **argv) {
540547
MP_DECLARE_CONST_FUN_OBJ_0(extra_cpp_coverage_obj);
541548
mp_store_global(MP_QSTR_extra_coverage, MP_OBJ_FROM_PTR(&extra_coverage_obj));
542549
mp_store_global(MP_QSTR_extra_cpp_coverage, MP_OBJ_FROM_PTR(&extra_cpp_coverage_obj));
550+
mp_store_global(MP_QSTR_get_key, MP_OBJ_FROM_PTR(&get_key_obj));
543551
}
544552
#endif
545553

ports/unix/variants/coverage/mpconfigvariant.mk

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ SRC_BITMAP := \
3333
shared-bindings/aesio/__init__.c \
3434
shared-bindings/bitmaptools/__init__.c \
3535
shared-bindings/displayio/Bitmap.c \
36-
shared-bindings/_environ/__init__.c \
3736
shared-bindings/rainbowio/__init__.c \
3837
shared-bindings/traceback/__init__.c \
3938
shared-bindings/util.c \
@@ -45,7 +44,7 @@ SRC_BITMAP := \
4544
shared-module/displayio/Bitmap.c \
4645
shared-module/displayio/ColorConverter.c \
4746
shared-module/displayio/ColorConverter.c \
48-
shared-module/_environ/__init__.c \
47+
shared-module/os/getenv.c \
4948
shared-module/rainbowio/__init__.c \
5049
shared-module/traceback/__init__.c \
5150
shared-module/zlib/__init__.c \
@@ -56,7 +55,7 @@ CFLAGS += \
5655
-DCIRCUITPY_AESIO=1 \
5756
-DCIRCUITPY_BITMAPTOOLS=1 \
5857
-DCIRCUITPY_DISPLAYIO_UNIX=1 \
59-
-DCIRCUITPY_ENVIRON=1 \
58+
-DCIRCUITPY_OS_GETENV=1 \
6059
-DCIRCUITPY_GIFIO=1 \
6160
-DCIRCUITPY_RAINBOWIO=1 \
6261
-DCIRCUITPY_TRACEBACK=1 \

py/circuitpy_defns.mk

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,6 @@ endif
182182
ifeq ($(CIRCUITPY_DISPLAYIO),1)
183183
SRC_PATTERNS += displayio/%
184184
endif
185-
ifeq ($(CIRCUITPY_ENVIRON),1)
186-
SRC_PATTERNS += _environ/%
187-
endif
188185
ifeq ($(CIRCUITPY__EVE),1)
189186
SRC_PATTERNS += _eve/%
190187
endif
@@ -618,6 +615,7 @@ SRC_SHARED_MODULE_ALL = \
618615
onewireio/__init__.c \
619616
onewireio/OneWire.c \
620617
os/__init__.c \
618+
os/getenv.c \
621619
paralleldisplay/ParallelBus.c \
622620
qrio/__init__.c \
623621
qrio/QRDecoder.c \

0 commit comments

Comments
 (0)