py: Include filename in errors from loading/saving files via "open".
This improves error messages in mpy-cross: - When loading a .py file that doesn't exist (or can't be opened) it now includes the filename in the OSError. - When saving a .mpy file that can't be opened it now raises an exception (prior, it would silently fail), and includes the filename in the OSError. Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
parent
cc588ac3a9
commit
fb77be1506
|
@ -644,6 +644,9 @@ void mp_raw_code_save_file(mp_compiled_module_t *cm, const char *filename) {
|
||||||
MP_THREAD_GIL_EXIT();
|
MP_THREAD_GIL_EXIT();
|
||||||
int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||||
MP_THREAD_GIL_ENTER();
|
MP_THREAD_GIL_ENTER();
|
||||||
|
if (fd < 0) {
|
||||||
|
mp_raise_OSError_with_filename(errno, filename);
|
||||||
|
}
|
||||||
mp_print_t fd_print = {(void *)(intptr_t)fd, fd_print_strn};
|
mp_print_t fd_print = {(void *)(intptr_t)fd, fd_print_strn};
|
||||||
mp_raw_code_save(cm, &fd_print);
|
mp_raw_code_save(cm, &fd_print);
|
||||||
MP_THREAD_GIL_EXIT();
|
MP_THREAD_GIL_EXIT();
|
||||||
|
|
|
@ -139,7 +139,7 @@ void mp_reader_new_file(mp_reader_t *reader, const char *filename) {
|
||||||
int fd = open(filename, O_RDONLY, 0644);
|
int fd = open(filename, O_RDONLY, 0644);
|
||||||
MP_THREAD_GIL_ENTER();
|
MP_THREAD_GIL_ENTER();
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
mp_raise_OSError(errno);
|
mp_raise_OSError_with_filename(errno, filename);
|
||||||
}
|
}
|
||||||
mp_reader_new_file_from_fd(reader, fd, true);
|
mp_reader_new_file_from_fd(reader, fd, true);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue