static void on_handle_close(uv_handle_t *handle) { PyGILState_STATE gstate = PyGILState_Ensure(); Handle *self; PyObject *result; ASSERT(handle); self = (Handle *)handle->data; ASSERT(self); if (self->on_close_cb != Py_None) { result = PyObject_CallFunctionObjArgs(self->on_close_cb, self, NULL); if (result == NULL) { handle_uncaught_exception(self->loop); } Py_XDECREF(result); } Py_DECREF(self->on_close_cb); self->on_close_cb = NULL; Py_DECREF(self->loop); self->loop = (Loop *)Py_None; Py_INCREF(Py_None); /* Refcount was increased in the caller function */ Py_DECREF(self); PyGILState_Release(gstate); }
static void on_tcp_client_connection(uv_connect_t *req, int status) { PyGILState_STATE gstate = PyGILState_Ensure(); TCP *self; PyObject *callback, *result, *py_errorno; ASSERT(req); self = PYUV_CONTAINER_OF(req->handle, TCP, tcp_h); callback = (PyObject *)req->data; if (status != 0) { uv_err_t err = uv_last_error(UV_HANDLE_LOOP(self)); py_errorno = PyInt_FromLong(err.code); } else { py_errorno = Py_None; Py_INCREF(Py_None); } result = PyObject_CallFunctionObjArgs(callback, self, py_errorno, NULL); if (result == NULL) { handle_uncaught_exception(HANDLE(self)->loop); } Py_XDECREF(result); Py_DECREF(py_errorno); Py_DECREF(callback); PyMem_Free(req); /* Refcount was increased in the caller function */ Py_DECREF(self); PyGILState_Release(gstate); }
static void pyuv__check_signals(uv_poll_t *handle, int status, int events) { PyGILState_STATE gstate = PyGILState_Ensure(); SignalChecker *self; ASSERT(handle); self = PYUV_CONTAINER_OF(handle, SignalChecker, poll_h); if (status == 0) { ASSERT(events == UV_READABLE); } /* Drain the fd */ if (pyuv__drain_poll_fd(self->fd) != 0) { uv_poll_stop(handle); } /* Check for signals */ PyErr_CheckSignals(); if (PyErr_Occurred()) { handle_uncaught_exception(HANDLE(self)->loop); } Py_DECREF(self); PyGILState_Release(gstate); }
static void pyuv__stream_shutdown_cb(uv_shutdown_t* req, int status) { PyGILState_STATE gstate = PyGILState_Ensure(); stream_shutdown_ctx *ctx; Stream *self; PyObject *callback, *result, *py_errorno; ctx = PYUV_CONTAINER_OF(req, stream_shutdown_ctx, req); self = ctx->obj; callback = ctx->callback; if (callback != Py_None) { if (status < 0) { py_errorno = PyInt_FromLong((long)status); } else { py_errorno = Py_None; Py_INCREF(Py_None); } result = PyObject_CallFunctionObjArgs(callback, self, py_errorno, NULL); if (result == NULL) { handle_uncaught_exception(HANDLE(self)->loop); } Py_XDECREF(result); Py_DECREF(py_errorno); } Py_DECREF(callback); PyMem_Free(req); /* Refcount was increased in the caller function */ Py_DECREF(self); PyGILState_Release(gstate); }
static void on_udp_read(uv_udp_t* handle, int nread, uv_buf_t buf, struct sockaddr* addr, unsigned flags) { PyGILState_STATE gstate = PyGILState_Ensure(); char ip[INET6_ADDRSTRLEN]; struct sockaddr_in addr4; struct sockaddr_in6 addr6; uv_err_t err; UDP *self; PyObject *result, *address_tuple, *data, *py_errorno; ASSERT(handle); ASSERT(flags == 0); self = (UDP *)handle->data; ASSERT(self); /* Object could go out of scope in the callback, increase refcount to avoid it */ Py_INCREF(self); if (nread == 0) { goto done; } if (nread > 0) { ASSERT(addr); if (addr->sa_family == AF_INET) { addr4 = *(struct sockaddr_in*)addr; uv_ip4_name(&addr4, ip, INET_ADDRSTRLEN); address_tuple = Py_BuildValue("(si)", ip, ntohs(addr4.sin_port)); } else { addr6 = *(struct sockaddr_in6*)addr; uv_ip6_name(&addr6, ip, INET6_ADDRSTRLEN); address_tuple = Py_BuildValue("(si)", ip, ntohs(addr6.sin6_port)); } data = PyBytes_FromStringAndSize(buf.base, nread); py_errorno = Py_None; Py_INCREF(Py_None); } else { address_tuple = Py_None; Py_INCREF(Py_None); data = Py_None; Py_INCREF(Py_None); err = uv_last_error(UV_HANDLE_LOOP(self)); py_errorno = PyInt_FromLong((long)err.code); } result = PyObject_CallFunctionObjArgs(self->on_read_cb, self, address_tuple, PyInt_FromLong((long)flags), data, py_errorno, NULL); if (result == NULL) { handle_uncaught_exception(HANDLE(self)->loop); } Py_XDECREF(result); Py_DECREF(address_tuple); Py_DECREF(data); Py_DECREF(py_errorno); done: Py_DECREF(self); PyGILState_Release(gstate); }
// Returns standard error codes: 0 for success, 1 for all other errors, // except if FORCED_EXIT bit is set then script raised SystemExit and the // value of the exit is in the lower 8 bits of the return value STATIC int execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) { if (lex == NULL) { printf("MemoryError: lexer could not allocate memory\n"); return 1; } nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { qstr source_name = lex->source_name; #if MICROPY_PY___FILE__ if (input_kind == MP_PARSE_FILE_INPUT) { mp_store_global(MP_QSTR___file__, MP_OBJ_NEW_QSTR(source_name)); } #endif mp_parse_tree_t parse_tree = mp_parse(lex, input_kind); mp_obj_t module_fun = mp_compile(&parse_tree, source_name, emit_opt, is_repl); if (!compile_only) { // execute it mp_call_function_0(module_fun); } nlr_pop(); return 0; } else { // uncaught exception return handle_uncaught_exception((mp_obj_t)nlr.ret_val); } }
static void on_tcp_connection(uv_stream_t *handle, int status) { PyGILState_STATE gstate = PyGILState_Ensure(); TCP *self; PyObject *result, *py_errorno; ASSERT(handle); self = PYUV_CONTAINER_OF(handle, TCP, tcp_h); /* Object could go out of scope in the callback, increase refcount to avoid it */ Py_INCREF(self); if (status != 0) { uv_err_t err = uv_last_error(UV_HANDLE_LOOP(self)); py_errorno = PyInt_FromLong((long)err.code); } else { py_errorno = Py_None; Py_INCREF(Py_None); } result = PyObject_CallFunctionObjArgs(self->on_new_connection_cb, self, py_errorno, NULL); if (result == NULL) { handle_uncaught_exception(HANDLE(self)->loop); } Py_XDECREF(result); Py_DECREF(py_errorno); Py_DECREF(self); PyGILState_Release(gstate); }
static void on_process_exit(uv_process_t *handle, int64_t exit_status, int term_signal) { PyGILState_STATE gstate = PyGILState_Ensure(); Process *self; PyObject *result, *py_exit_status, *py_term_signal; ASSERT(handle); self = PYUV_CONTAINER_OF(handle, Process, process_h); py_exit_status = PyInt_FromLong(exit_status); py_term_signal = PyInt_FromLong(term_signal); if (self->on_exit_cb != Py_None) { result = PyObject_CallFunctionObjArgs(self->on_exit_cb, self, py_exit_status, py_term_signal, NULL); if (result == NULL) { handle_uncaught_exception(HANDLE(self)->loop); } Py_XDECREF(result); Py_DECREF(py_exit_status); Py_DECREF(py_term_signal); } /* Refcount was increased in the spawn function */ Py_DECREF(self); PyGILState_Release(gstate); }
// Returns standard error codes: 0 for success, 1 for all other errors, // except if FORCED_EXIT bit is set then script raised SystemExit and the // value of the exit is in the lower 8 bits of the return value STATIC int execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) { if (lex == NULL) { printf("MemoryError: lexer could not allocate memory\n"); return 1; } #ifndef _WIN32 // enable signal handler struct sigaction sa; sa.sa_handler = sighandler; sigemptyset(&sa.sa_mask); sigaction(SIGINT, &sa, NULL); sa.sa_handler = SIG_DFL; #endif nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { qstr source_name = lex->source_name; #if MICROPY_PY___FILE__ if (input_kind == MP_PARSE_FILE_INPUT) { mp_store_global(MP_QSTR___file__, MP_OBJ_NEW_QSTR(source_name)); } #endif mp_parse_node_t pn = mp_parse(lex, input_kind); /* printf("----------------\n"); mp_parse_node_print(pn, 0); printf("----------------\n"); */ mp_obj_t module_fun = mp_compile(pn, source_name, emit_opt, is_repl); if (!compile_only) { // execute it mp_call_function_0(module_fun); } #ifndef _WIN32 // disable signal handler sigaction(SIGINT, &sa, NULL); #endif nlr_pop(); return 0; } else { // uncaught exception #ifndef _WIN32 // disable signal handler sigaction(SIGINT, &sa, NULL); #endif return handle_uncaught_exception((mp_obj_t)nlr.ret_val); } }
static void on_udp_read(uv_udp_t* handle, int nread, const uv_buf_t* buf, struct sockaddr* addr, unsigned flags) { PyGILState_STATE gstate = PyGILState_Ensure(); Loop *loop; UDP *self; PyObject *result, *address_tuple, *data, *py_errorno; ASSERT(handle); ASSERT(flags == 0); self = PYUV_CONTAINER_OF(handle, UDP, udp_h); /* Object could go out of scope in the callback, increase refcount to avoid it */ Py_INCREF(self); if (nread == 0) { goto done; } if (nread > 0) { ASSERT(addr); address_tuple = makesockaddr(addr, sizeof(*addr)); data = PyBytes_FromStringAndSize(buf->base, nread); py_errorno = Py_None; Py_INCREF(Py_None); } else { address_tuple = Py_None; Py_INCREF(Py_None); data = Py_None; Py_INCREF(Py_None); py_errorno = PyInt_FromLong((long)nread); } result = PyObject_CallFunctionObjArgs(self->on_read_cb, self, address_tuple, PyInt_FromLong((long)flags), data, py_errorno, NULL); if (result == NULL) { handle_uncaught_exception(HANDLE(self)->loop); } Py_XDECREF(result); Py_DECREF(address_tuple); Py_DECREF(data); Py_DECREF(py_errorno); done: /* data has been read, unlock the buffer */ loop = handle->loop->data; ASSERT(loop); loop->buffer.in_use = False; Py_DECREF(self); PyGILState_Release(gstate); }
static void on_udp_send(uv_udp_send_t* req, int status) { PyGILState_STATE gstate = PyGILState_Ensure(); int i; udp_send_data_t* req_data; UDP *self; PyObject *callback, *result, *py_errorno; ASSERT(req); req_data = (udp_send_data_t *)req->data; self = (UDP *)req->handle->data; callback = req_data->callback; ASSERT(self); /* Object could go out of scope in the callback, increase refcount to avoid it */ Py_INCREF(self); if (callback != Py_None) { if (status < 0) { uv_err_t err = uv_last_error(UV_HANDLE_LOOP(self)); py_errorno = PyInt_FromLong((long)err.code); } else { py_errorno = Py_None; Py_INCREF(Py_None); } result = PyObject_CallFunctionObjArgs(callback, self, py_errorno, NULL); if (result == NULL) { handle_uncaught_exception(((Handle *)self)->loop); } Py_XDECREF(result); Py_DECREF(py_errorno); } if (req_data->buf_count == 1) { PyBuffer_Release(&req_data->data.view); } else { for (i = 0; i < req_data->buf_count; i++) { PyMem_Free(req_data->data.bufs[i].base); } PyMem_Free(req_data->data.bufs); } Py_DECREF(callback); PyMem_Free(req_data); PyMem_Free(req); Py_DECREF(self); PyGILState_Release(gstate); }
// Returns standard error codes: 0 for success, 1 for all other errors, // except if FORCED_EXIT bit is set then script raised SystemExit and the // value of the exit is in the lower 8 bits of the return value STATIC int execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) { if (lex == NULL) { printf("MemoryError: lexer could not allocate memory\n"); return 1; } mp_hal_set_interrupt_char(CHAR_CTRL_C); nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { qstr source_name = lex->source_name; #if MICROPY_PY___FILE__ if (input_kind == MP_PARSE_FILE_INPUT) { mp_store_global(MP_QSTR___file__, MP_OBJ_NEW_QSTR(source_name)); } #endif mp_parse_tree_t parse_tree = mp_parse(lex, input_kind); /* printf("----------------\n"); mp_parse_node_print(parse_tree.root, 0); printf("----------------\n"); */ mp_obj_t module_fun = mp_compile(&parse_tree, source_name, emit_opt, is_repl); if (!compile_only) { // execute it mp_call_function_0(module_fun); // check for pending exception if (MP_STATE_VM(mp_pending_exception) != MP_OBJ_NULL) { mp_obj_t obj = MP_STATE_VM(mp_pending_exception); MP_STATE_VM(mp_pending_exception) = MP_OBJ_NULL; nlr_raise(obj); } } mp_hal_set_interrupt_char(-1); nlr_pop(); return 0; } else { // uncaught exception mp_hal_set_interrupt_char(-1); return handle_uncaught_exception(nlr.ret_val); } }
static void on_stream_write(uv_write_t* req, int status) { PyGILState_STATE gstate = PyGILState_Ensure(); int i; stream_write_ctx *ctx; Stream *self; PyObject *callback, *send_handle, *result, *py_errorno; ASSERT(req); ctx = PYUV_CONTAINER_OF(req, stream_write_ctx, req); self = ctx->obj; callback = ctx->callback; send_handle = ctx->send_handle; if (callback != Py_None) { if (status < 0) { py_errorno = PyInt_FromLong((long)status); } else { py_errorno = Py_None; Py_INCREF(Py_None); } result = PyObject_CallFunctionObjArgs(callback, self, py_errorno, NULL); if (result == NULL) { handle_uncaught_exception(HANDLE(self)->loop); } Py_XDECREF(result); Py_DECREF(py_errorno); } Py_DECREF(callback); Py_XDECREF(send_handle); for (i = 0; i < ctx->view_count; i++) { PyBuffer_Release(&ctx->views[i]); } if (ctx->views != ctx->view) { PyMem_Free(ctx->views); } PyMem_Free(ctx); /* Refcount was increased in the caller function */ Py_DECREF(self); PyGILState_Release(gstate); }
static void pyuv__stream_read_cb(uv_stream_t* handle, int nread, const uv_buf_t* buf) { PyGILState_STATE gstate = PyGILState_Ensure(); Loop *loop; Stream *self; PyObject *result, *data, *py_errorno; ASSERT(handle); /* Can't use container_of here */ self = (Stream *)handle->data; /* Object could go out of scope in the callback, increase refcount to avoid it */ Py_INCREF(self); if (nread >= 0) { data = PyBytes_FromStringAndSize(buf->base, nread); py_errorno = Py_None; Py_INCREF(Py_None); } else { data = Py_None; Py_INCREF(Py_None); py_errorno = PyInt_FromLong((long)nread); /* Stop reading, otherwise an assert blows up on unix */ uv_read_stop(handle); } result = PyObject_CallFunctionObjArgs(self->on_read_cb, self, data, py_errorno, NULL); if (result == NULL) { handle_uncaught_exception(HANDLE(self)->loop); } Py_XDECREF(result); Py_DECREF(data); Py_DECREF(py_errorno); /* data has been read, unlock the buffer */ loop = handle->loop->data; ASSERT(loop); loop->buffer.in_use = False; Py_DECREF(self); PyGILState_Release(gstate); }
static void on_timer_callback(uv_timer_t *handle) { PyGILState_STATE gstate = PyGILState_Ensure(); Timer *self; PyObject *result; ASSERT(handle); self = PYUV_CONTAINER_OF(handle, Timer, timer_h); /* Object could go out of scope in the callback, increase refcount to avoid it */ Py_INCREF(self); result = PyObject_CallFunctionObjArgs(self->callback, self, NULL); if (result == NULL) { handle_uncaught_exception(HANDLE(self)->loop); } Py_XDECREF(result); Py_DECREF(self); PyGILState_Release(gstate); }
static void on_pipe_read2(uv_pipe_t* handle, int nread, uv_buf_t buf, uv_handle_type pending) { PyGILState_STATE gstate = PyGILState_Ensure(); uv_err_t err; Stream *self; PyObject *result, *data, *py_errorno, *py_pending; ASSERT(handle); self = (Stream *)handle->data; ASSERT(self); /* Object could go out of scope in the callback, increase refcount to avoid it */ Py_INCREF(self); py_pending = PyInt_FromLong((long)pending); if (nread >= 0) { data = PyBytes_FromStringAndSize(buf.base, nread); py_errorno = Py_None; Py_INCREF(Py_None); } else { data = Py_None; Py_INCREF(Py_None); err = uv_last_error(UV_HANDLE_LOOP(self)); py_errorno = PyInt_FromLong((long)err.code); } result = PyObject_CallFunctionObjArgs(self->on_read_cb, self, data, py_pending, py_errorno, NULL); if (result == NULL) { handle_uncaught_exception(HANDLE(self)->loop); } Py_XDECREF(result); Py_DECREF(data); Py_DECREF(py_pending); Py_DECREF(py_errorno); Py_DECREF(self); PyGILState_Release(gstate); }
static void on_signal_callback(uv_signal_t *handle, int signum) { PyGILState_STATE gstate = PyGILState_Ensure(); Signal *self; PyObject *result; ASSERT(handle); self = (Signal *)handle->data; ASSERT(self); /* Object could go out of scope in the callback, increase refcount to avoid it */ Py_INCREF(self); result = PyObject_CallFunctionObjArgs(self->callback, self, PyInt_FromLong((long)signum), NULL); if (result == NULL) { handle_uncaught_exception(((Handle *)self)->loop); } Py_XDECREF(result); Py_DECREF(self); PyGILState_Release(gstate); }
static void on_check_callback(uv_check_t *handle, int status) { PyGILState_STATE gstate = PyGILState_Ensure(); Check *self; PyObject *result; ASSERT(handle); ASSERT(status == 0); self = (Check *)handle->data; ASSERT(self); /* Object could go out of scope in the callback, increase refcount to avoid it */ Py_INCREF(self); result = PyObject_CallFunctionObjArgs(self->callback, self, NULL); if (result == NULL) { handle_uncaught_exception(HANDLE(self)->loop); } Py_XDECREF(result); Py_DECREF(self); PyGILState_Release(gstate); }
int main(int argc, char **argv) { mp_stack_set_limit(40000 * (BYTES_PER_WORD / 4)); pre_process_options(argc, argv); #if MICROPY_ENABLE_GC char *heap = malloc(heap_size); gc_init(heap, heap + heap_size); #endif mp_init(); #ifndef _WIN32 // create keyboard interrupt object MP_STATE_VM(keyboard_interrupt_obj) = mp_obj_new_exception(&mp_type_KeyboardInterrupt); #endif char *home = getenv("HOME"); char *path = getenv("MICROPYPATH"); if (path == NULL) { #ifdef MICROPY_PY_SYS_PATH_DEFAULT path = MICROPY_PY_SYS_PATH_DEFAULT; #else path = "~/.micropython/lib:/usr/lib/micropython"; #endif } mp_uint_t path_num = 1; // [0] is for current dir (or base dir of the script) for (char *p = path; p != NULL; p = strchr(p, PATHLIST_SEP_CHAR)) { path_num++; if (p != NULL) { p++; } } mp_obj_list_init(mp_sys_path, path_num); mp_obj_t *path_items; mp_obj_list_get(mp_sys_path, &path_num, &path_items); path_items[0] = MP_OBJ_NEW_QSTR(MP_QSTR_); { char *p = path; for (mp_uint_t i = 1; i < path_num; i++) { char *p1 = strchr(p, PATHLIST_SEP_CHAR); if (p1 == NULL) { p1 = p + strlen(p); } if (p[0] == '~' && p[1] == '/' && home != NULL) { // Expand standalone ~ to $HOME CHECKBUF(buf, PATH_MAX); CHECKBUF_APPEND(buf, home, strlen(home)); CHECKBUF_APPEND(buf, p + 1, (size_t)(p1 - p - 1)); path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(buf, CHECKBUF_LEN(buf))); } else { path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(p, p1 - p)); } p = p1 + 1; } } mp_obj_list_init(mp_sys_argv, 0); #if defined(MICROPY_UNIX_COVERAGE) { MP_DECLARE_CONST_FUN_OBJ(extra_coverage_obj); mp_store_global(QSTR_FROM_STR_STATIC("extra_coverage"), (mp_obj_t)&extra_coverage_obj); } #endif // Here is some example code to create a class and instance of that class. // First is the Python, then the C code. // // class TestClass: // pass // test_obj = TestClass() // test_obj.attr = 42 // // mp_obj_t test_class_type, test_class_instance; // test_class_type = mp_obj_new_type(QSTR_FROM_STR_STATIC("TestClass"), mp_const_empty_tuple, mp_obj_new_dict(0)); // mp_store_name(QSTR_FROM_STR_STATIC("test_obj"), test_class_instance = mp_call_function_0(test_class_type)); // mp_store_attr(test_class_instance, QSTR_FROM_STR_STATIC("attr"), mp_obj_new_int(42)); /* printf("bytes:\n"); printf(" total %d\n", m_get_total_bytes_allocated()); printf(" cur %d\n", m_get_current_bytes_allocated()); printf(" peak %d\n", m_get_peak_bytes_allocated()); */ const int NOTHING_EXECUTED = -2; int ret = NOTHING_EXECUTED; for (int a = 1; a < argc; a++) { if (argv[a][0] == '-') { if (strcmp(argv[a], "-c") == 0) { if (a + 1 >= argc) { return usage(argv); } ret = do_str(argv[a + 1]); if (ret & FORCED_EXIT) { break; } a += 1; } else if (strcmp(argv[a], "-m") == 0) { if (a + 1 >= argc) { return usage(argv); } mp_obj_t import_args[4]; import_args[0] = mp_obj_new_str(argv[a + 1], strlen(argv[a + 1]), false); import_args[1] = import_args[2] = mp_const_none; // Ask __import__ to handle imported module specially - set its __name__ // to __main__, and also return this leaf module, not top-level package // containing it. import_args[3] = mp_const_false; // TODO: https://docs.python.org/3/using/cmdline.html#cmdoption-m : // "the first element of sys.argv will be the full path to // the module file (while the module file is being located, // the first element will be set to "-m")." set_sys_argv(argv, argc, a + 1); mp_obj_t mod; nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { mod = mp_builtin___import__(MP_ARRAY_SIZE(import_args), import_args); nlr_pop(); } else { // uncaught exception return handle_uncaught_exception((mp_obj_t)nlr.ret_val) & 0xff; } if (mp_obj_is_package(mod)) { // TODO fprintf(stderr, "%s: -m for packages not yet implemented\n", argv[0]); exit(1); } ret = 0; break; } else if (strcmp(argv[a], "-X") == 0) { a += 1; } else if (strcmp(argv[a], "-v") == 0) { mp_verbose_flag++; } else if (strncmp(argv[a], "-O", 2) == 0) { if (isdigit(argv[a][2])) { MP_STATE_VM(mp_optimise_value) = argv[a][2] & 0xf; } else { MP_STATE_VM(mp_optimise_value) = 0; for (char *p = argv[a] + 1; *p && *p == 'O'; p++, MP_STATE_VM(mp_optimise_value)++); } } else { return usage(argv); } } else { char *pathbuf = malloc(PATH_MAX); char *basedir = realpath(argv[a], pathbuf); if (basedir == NULL) { fprintf(stderr, "%s: can't open file '%s': [Errno %d] ", argv[0], argv[a], errno); perror(""); // CPython exits with 2 in such case ret = 2; break; } // Set base dir of the script as first entry in sys.path char *p = strrchr(basedir, '/'); path_items[0] = MP_OBJ_NEW_QSTR(qstr_from_strn(basedir, p - basedir)); free(pathbuf); set_sys_argv(argv, argc, a); ret = do_file(argv[a]); break; } } if (ret == NOTHING_EXECUTED) { if (isatty(0)) { prompt_read_history(); ret = do_repl(); prompt_write_history(); } else { mp_lexer_t *lex = mp_lexer_new_from_fd(MP_QSTR__lt_stdin_gt_, 0, false); ret = execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false); } } #if MICROPY_PY_MICROPYTHON_MEM_INFO if (mp_verbose_flag) { mp_micropython_mem_info(0, NULL); } #endif mp_deinit(); #if MICROPY_ENABLE_GC && !defined(NDEBUG) // We don't really need to free memory since we are about to exit the // process, but doing so helps to find memory leaks. free(heap); #endif //printf("total bytes = %d\n", m_get_total_bytes_allocated()); return ret & 0xff; }
static void getaddrinfo_cb(uv_getaddrinfo_t* req, int status, struct addrinfo* res) { PyGILState_STATE gstate = PyGILState_Ensure(); struct addrinfo *ptr; uv_err_t err; Loop *loop; PyObject *callback, *addr, *item, *errorno, *dns_result, *result; ASSERT(req); callback = (PyObject *)req->data; loop = (Loop *)req->loop->data; if (status != 0) { err = uv_last_error(loop->uv_loop); errorno = PyInt_FromLong((long)err.code); dns_result = Py_None; Py_INCREF(Py_None); goto callback; } dns_result = PyList_New(0); if (!dns_result) { errorno = PyInt_FromLong((long)UV_ENOMEM); dns_result = Py_None; Py_INCREF(Py_None); goto callback; } for (ptr = res; ptr; ptr = ptr->ai_next) { addr = makesockaddr(ptr->ai_addr, ptr->ai_addrlen); if (!addr) { PyErr_Clear(); break; } item = PyStructSequence_New(&AddrinfoResultType); if (!item) { PyErr_Clear(); break; } PyStructSequence_SET_ITEM(item, 0, PyInt_FromLong((long)ptr->ai_family)); PyStructSequence_SET_ITEM(item, 1, PyInt_FromLong((long)ptr->ai_socktype)); PyStructSequence_SET_ITEM(item, 2, PyInt_FromLong((long)ptr->ai_protocol)); PyStructSequence_SET_ITEM(item, 3, Py_BuildValue("s", ptr->ai_canonname ? ptr->ai_canonname : "")); PyStructSequence_SET_ITEM(item, 4, addr); PyList_Append(dns_result, item); Py_DECREF(item); } errorno = Py_None; Py_INCREF(Py_None); callback: result = PyObject_CallFunctionObjArgs(callback, dns_result, errorno, NULL); if (result == NULL) { handle_uncaught_exception(loop); } Py_XDECREF(result); Py_DECREF(dns_result); Py_DECREF(errorno); Py_DECREF(loop); Py_DECREF(callback); uv_freeaddrinfo(res); PyMem_Free(req); PyGILState_Release(gstate); }
/** * @return false iff all threads are dead. */ void throw_exception (Object *exception) { Thread *auxThread; #ifdef VERIFY assert (exception != null, EXCEPTIONS0); #endif // VERIFY #if DEBUG_EXCEPTIONS printf("Throw exception\n"); #endif if (currentThread == null) { // No threads have started probably return; } else if (exception == interruptedException) { // Throwing an interrupted exception clears the flag currentThread->interruptState = INTERRUPT_CLEARED; } #ifdef VERIFY assert (currentThread->state > DEAD, EXCEPTIONS1); #endif // VERIFY gExceptionPc = pc; gExcepMethodRec = null; #if 0 trace (-1, get_class_index(exception), 3); #endif LABEL_PROPAGATE: tempStackFrame = current_stackframe(); tempMethodRecord = tempStackFrame->methodRecord; if (gExcepMethodRec == null) gExcepMethodRec = tempMethodRecord; gExceptionRecord = (ExceptionRecord *) (get_binary_base() + tempMethodRecord->exceptionTable); tempCurrentOffset = ptr2word(pc) - ptr2word(get_binary_base() + tempMethodRecord->codeOffset); #if 0 trace (-1, tempCurrentOffset, 5); #endif gNumExceptionHandlers = tempMethodRecord->numExceptionHandlers; #if DEBUG_EXCEPTIONS printf("Num exception handlers=%d\n",gNumExceptionHandlers); #endif while (gNumExceptionHandlers--) { if (gExceptionRecord->start <= tempCurrentOffset /* off by one? < ? */ && tempCurrentOffset <= gExceptionRecord->end) { // Check if exception class applies if (instance_of (exception, gExceptionRecord->classIndex)) { // Clear operand stack init_sp (tempStackFrame, tempMethodRecord); // Push the exception object push_ref (ptr2word (exception)); // Jump to handler: pc = get_binary_base() + tempMethodRecord->codeOffset + gExceptionRecord->handler; #if DEBUG_EXCEPTIONS printf("Found exception handler\n"); #endif return; } } gExceptionRecord++; } // No good handlers in current stack frame - go up. auxThread = currentThread; do_return (0); // Note: return takes care of synchronized methods. if (auxThread->state == DEAD) { #if DEBUG_EXCEPTIONS printf("Thread is dead\n"); #endif if (get_class_index(exception) != JAVA_LANG_THREADDEATH) { #if DEBUG_EXCEPTIONS printf("Handle uncaught exception\n"); #endif handle_uncaught_exception (exception, auxThread, gExcepMethodRec, tempMethodRecord, gExceptionPc); } return; } goto LABEL_PROPAGATE; }