28
28
// tested in the unix "coverage" build, without bringing in "our" os module
29
29
30
30
#include <stdlib.h>
31
+ #include <stdarg.h>
31
32
#include <string.h>
32
33
33
34
#include "shared-bindings/os/__init__.h"
36
37
#include "py/gc.h"
37
38
#include "py/misc.h"
38
39
#include "py/mpstate.h"
40
+ #include "py/mpprint.h"
39
41
#include "py/objstr.h"
40
42
#include "py/parsenum.h"
41
43
#include "py/runtime.h"
@@ -199,6 +201,7 @@ STATIC os_getenv_err_t read_string_value(file_arg *active_file, vstr_t *buf) {
199
201
case '#' :
200
202
next_line (active_file );
201
203
MP_FALLTHROUGH ;
204
+ case 0 :
202
205
case '\n' :
203
206
return GETENV_OK ;
204
207
default :
@@ -255,7 +258,6 @@ STATIC os_getenv_err_t read_bare_value(file_arg *active_file, vstr_t *buf, int f
255
258
while (true) {
256
259
switch (character ) {
257
260
case 0 :
258
- return GETENV_ERR_UNEXPECTED | character ;
259
261
case '\n' :
260
262
return GETENV_OK ;
261
263
case '#' :
@@ -311,42 +313,70 @@ STATIC os_getenv_err_t os_getenv_buf_terminated(const char *key, char *value, si
311
313
return result ;
312
314
}
313
315
314
- os_getenv_err_t common_hal_os_getenv_str (const char * key , char * value , size_t value_len ) {
315
- bool quoted ;
316
- os_getenv_err_t result = os_getenv_buf_terminated (key , value , value_len , & quoted );
317
- if (result == GETENV_OK && !quoted ) {
318
- result = GETENV_ERR_UNEXPECTED | value [0 ];
319
- }
320
- return result ;
316
+ STATIC void print_dont_raise (const mp_obj_type_t * exc_type , const compressed_string_t * fmt , ...) {
317
+ va_list argptr ;
318
+ va_start (argptr ,fmt );
319
+ mp_vcprintf (& mp_plat_print , fmt , argptr );
320
+ mp_printf (& mp_plat_print , "\n" );
321
+ va_end (argptr );
321
322
}
322
323
323
- STATIC void throw_getenv_error (os_getenv_err_t error ) {
324
+ STATIC void handle_getenv_error (os_getenv_err_t error , void ( * handle )( const mp_obj_type_t * exc_type , const compressed_string_t * fmt , ...) ) {
324
325
if (error == GETENV_OK ) {
325
326
return ;
326
327
}
327
328
if (error & GETENV_ERR_UNEXPECTED ) {
328
329
byte character = (error & 0xff );
329
- mp_print_t print ;
330
+ char buf [ 8 ] ;
330
331
vstr_t vstr ;
331
- vstr_init_print (& vstr , 8 + 4 + 1 , & print );
332
+ vstr_init_fixed_buf (& vstr , sizeof (buf ), buf );
333
+ mp_print_t print = { .data = & vstr , .print_strn = (mp_print_strn_t )vstr_add_strn };
334
+
332
335
if (character ) {
333
336
mp_str_print_quoted (& print , & character , 1 , true);
334
337
} else {
335
338
mp_str_print_quoted (& print , (byte * )"EOF" , 3 , true);
336
339
}
337
- mp_raise_ValueError_varg (translate ("Invalid byte %.*s" ),
338
- vstr .len , vstr .buf );
340
+ handle (& mp_type_ValueError , translate ("Invalid byte %.*s" ), vstr .len , vstr .buf );
341
+ } else {
342
+ switch (error ) {
343
+ case GETENV_ERR_OPEN :
344
+ handle (& mp_type_ValueError , translate ("%S" ), translate ("File not found" ));
345
+ break ;
346
+ case GETENV_ERR_UNICODE :
347
+ handle (& mp_type_ValueError , translate ("%S" ), translate ("Invalid unicode escape" ));
348
+ break ;
349
+ case GETENV_ERR_NOT_FOUND :
350
+ handle (& mp_type_ValueError , translate ("%S" ), translate ("Key not found" ));
351
+ break ;
352
+ default :
353
+ handle (& mp_type_RuntimeError , translate ("%S" ), translate ("Internal error" ));
354
+ break ;
355
+ }
356
+ }
357
+ }
358
+
359
+ STATIC void common_hal_os_getenv_showerr (const char * key , os_getenv_err_t result ) {
360
+ if (result != GETENV_OK && result != GETENV_ERR_OPEN && result != GETENV_ERR_NOT_FOUND ) {
361
+ mp_cprintf (& mp_plat_print , translate ("An error occurred while retrieving '%s':\n" ), key );
362
+ handle_getenv_error (result , print_dont_raise );
339
363
}
340
- switch (error ) {
341
- case GETENV_ERR_OPEN :
342
- mp_raise_ValueError (translate ("File not found" ));
343
- case GETENV_ERR_UNICODE :
344
- mp_raise_ValueError (translate ("Invalid unicode escape" ));
345
- case GETENV_ERR_NOT_FOUND :
346
- mp_raise_ValueError (translate ("Key not found" ));
347
- default :
348
- mp_raise_RuntimeError (translate ("Internal error" ));
364
+ }
365
+
366
+ STATIC
367
+ os_getenv_err_t common_hal_os_getenv_str_inner (const char * key , char * value , size_t value_len ) {
368
+ bool quoted ;
369
+ os_getenv_err_t result = os_getenv_buf_terminated (key , value , value_len , & quoted );
370
+ if (result == GETENV_OK && !quoted ) {
371
+ result = GETENV_ERR_UNEXPECTED | value [0 ];
349
372
}
373
+ return result ;
374
+ }
375
+
376
+ os_getenv_err_t common_hal_os_getenv_str (const char * key , char * value , size_t value_len ) {
377
+ os_getenv_err_t result = common_hal_os_getenv_str_inner (key , value , value_len );
378
+ common_hal_os_getenv_showerr (key , result );
379
+ return result ;
350
380
}
351
381
352
382
mp_obj_t common_hal_os_getenv_path (const char * path , const char * key , mp_obj_t default_ ) {
@@ -358,7 +388,7 @@ mp_obj_t common_hal_os_getenv_path(const char *path, const char *key, mp_obj_t d
358
388
if (result == GETENV_ERR_NOT_FOUND || result == GETENV_ERR_OPEN ) {
359
389
return default_ ;
360
390
}
361
- throw_getenv_error (result );
391
+ handle_getenv_error (result , mp_raise_msg_varg );
362
392
363
393
if (quoted ) {
364
394
return mp_obj_new_str_from_vstr (& mp_type_str , & buf );
@@ -371,7 +401,7 @@ mp_obj_t common_hal_os_getenv(const char *key, mp_obj_t default_) {
371
401
return common_hal_os_getenv_path (GETENV_PATH , key , default_ );
372
402
}
373
403
374
- os_getenv_err_t common_hal_os_getenv_int (const char * key , mp_int_t * value ) {
404
+ STATIC os_getenv_err_t common_hal_os_getenv_int_inner (const char * key , mp_int_t * value ) {
375
405
char buf [16 ];
376
406
bool quoted ;
377
407
os_getenv_err_t result = os_getenv_buf_terminated (key , buf , sizeof (buf ), & quoted );
@@ -389,3 +419,9 @@ os_getenv_err_t common_hal_os_getenv_int(const char *key, mp_int_t *value) {
389
419
* value = (mp_int_t )num ;
390
420
return GETENV_OK ;
391
421
}
422
+
423
+ os_getenv_err_t common_hal_os_getenv_int (const char * key , mp_int_t * value ) {
424
+ os_getenv_err_t result = common_hal_os_getenv_int_inner (key , value );
425
+ common_hal_os_getenv_showerr (key , result );
426
+ return result ;
427
+ }
0 commit comments