Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.

Commit b92cbe6

Browse files
committed
py: Move definition of mp_sys_exit to core.
sys.exit always raises SystemExit so doesn't need a special implementation for each port. If C exit() is really needed, use the standard os._exit function. Also initialise mp_sys_path and mp_sys_argv in teensy port.
1 parent 8369559 commit b92cbe6

File tree

5 files changed

+26
-42
lines changed

5 files changed

+26
-42
lines changed

py/modsys.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525
*/
2626

2727
#include <stdint.h>
28-
#include <limits.h>
28+
2929
#include "mpconfig.h"
30+
#include "nlr.h"
3031
#include "misc.h"
3132
#include "qstr.h"
3233
#include "obj.h"
@@ -42,13 +43,7 @@
4243

4344
/// \module sys - system specific functions
4445

45-
// These should be implemented by ports, specific types don't matter,
46-
// only addresses.
47-
struct _dummy_t;
48-
extern struct _dummy_t mp_sys_exit_obj;
49-
50-
extern mp_obj_int_t mp_maxsize_obj;
51-
46+
// These two lists must be initialised per port (after the call to mp_init).
5247
// TODO document these properly, they aren't constants or functions...
5348
/// \constant path - a mutable list of directories to search for imported modules
5449
mp_obj_list_t mp_sys_path_obj;
@@ -69,6 +64,20 @@ STATIC const mp_obj_tuple_t mp_sys_version_info_obj = {{&mp_type_tuple}, 3, {I(3
6964
STATIC const MP_DEFINE_STR_OBJ(platform_obj, MICROPY_PY_SYS_PLATFORM);
7065
#endif
7166

67+
/// \function exit([retval])
68+
/// Raise a `SystemExit` exception. If an argument is given, it is the
69+
/// value given to `SystemExit`.
70+
STATIC mp_obj_t mp_sys_exit(mp_uint_t n_args, const mp_obj_t *args) {
71+
mp_obj_t exc;
72+
if (n_args == 0) {
73+
exc = mp_obj_new_exception(&mp_type_SystemExit);
74+
} else {
75+
exc = mp_obj_new_exception_arg1(&mp_type_SystemExit, args[0]);
76+
}
77+
nlr_raise(exc);
78+
}
79+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit);
80+
7281
STATIC const mp_map_elem_t mp_module_sys_globals_table[] = {
7382
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_sys) },
7483

py/objint.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ typedef struct _mp_obj_int_t {
3333
#endif
3434
} mp_obj_int_t;
3535

36+
extern const mp_obj_int_t mp_maxsize_obj;
37+
3638
void mp_obj_int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind);
3739
char *mp_obj_int_formatted(char **buf, int *buf_size, int *fmt_size, mp_const_obj_t self_in,
3840
int base, const char *prefix, char base_char, char comma);

stmhal/main.c

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -550,16 +550,3 @@ int main(void) {
550550
first_soft_reset = false;
551551
goto soft_reset;
552552
}
553-
554-
/// \moduleref sys
555-
/// \function exit([retval])
556-
/// Raise a `SystemExit` exception. If an argument is given, it is the
557-
/// value given to `SystemExit`.
558-
STATIC NORETURN mp_obj_t mp_sys_exit(uint n_args, const mp_obj_t *args) {
559-
int rc = 0;
560-
if (n_args > 0) {
561-
rc = mp_obj_get_int(args[0]);
562-
}
563-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_SystemExit, mp_obj_new_int(rc)));
564-
}
565-
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit);

teensy/main.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ int main(void) {
269269

270270
// Micro Python init
271271
mp_init();
272+
mp_obj_list_init(mp_sys_path, 0);
273+
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
274+
mp_obj_list_init(mp_sys_argv, 0);
272275

273276
readline_init0();
274277

@@ -369,12 +372,3 @@ char * ultoa(unsigned long val, char *buf, int radix)
369372
}
370373
return buf;
371374
}
372-
373-
STATIC NORETURN mp_obj_t mp_sys_exit(uint n_args, const mp_obj_t *args) {
374-
int rc = 0;
375-
if (n_args > 0) {
376-
rc = mp_obj_get_int(args[0]);
377-
}
378-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_SystemExit, mp_obj_new_int(rc)));
379-
}
380-
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit);

unix/main.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,11 @@ STATIC int execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind,
130130
// check for SystemExit
131131
mp_obj_t exc = (mp_obj_t)nlr.ret_val;
132132
if (mp_obj_is_subclass_fast(mp_obj_get_type(exc), &mp_type_SystemExit)) {
133+
// None is an exit value of 0; an int is its value; anything else is 1
133134
mp_obj_t exit_val = mp_obj_exception_get_value(exc);
134-
mp_int_t val;
135-
if (!mp_obj_get_int_maybe(exit_val, &val)) {
136-
val = 0;
135+
mp_int_t val = 0;
136+
if (exit_val != mp_const_none && !mp_obj_get_int_maybe(exit_val, &val)) {
137+
val = 1;
137138
}
138139
exit(val);
139140
}
@@ -406,15 +407,6 @@ int main(int argc, char **argv) {
406407
return ret;
407408
}
408409

409-
STATIC mp_obj_t mp_sys_exit(uint n_args, const mp_obj_t *args) {
410-
int rc = 0;
411-
if (n_args > 0) {
412-
rc = mp_obj_get_int(args[0]);
413-
}
414-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_SystemExit, mp_obj_new_int(rc)));
415-
}
416-
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit);
417-
418410
uint mp_import_stat(const char *path) {
419411
struct stat st;
420412
if (stat(path, &st) == 0) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy