[PRISM] Raise LoadError when file cannot be read

This commit is contained in:
Kevin Newton 2024-04-25 13:32:08 -04:00
Родитель 6f4f360fc4
Коммит af24ba4034
4 изменённых файлов: 19 добавлений и 9 удалений

Просмотреть файл

@ -8785,7 +8785,7 @@ pm_parse_file_script_lines(const pm_scope_node_t *scope_node, const pm_parser_t
* be read.
*/
VALUE
pm_load_file(pm_parse_result_t *result, VALUE filepath)
pm_load_file(pm_parse_result_t *result, VALUE filepath, bool load_error)
{
if (!pm_string_mapped_init(&result->input, RSTRING_PTR(filepath))) {
#ifdef _WIN32
@ -8794,9 +8794,21 @@ pm_load_file(pm_parse_result_t *result, VALUE filepath)
int e = errno;
#endif
VALUE err = rb_syserr_new(e, RSTRING_PTR(filepath));
RB_GC_GUARD(filepath);
return err;
VALUE error;
if (load_error) {
VALUE message = rb_str_buf_new_cstr(strerror(e));
rb_str_cat2(message, " -- ");
rb_str_append(message, filepath);
error = rb_exc_new3(rb_eLoadError, message);
rb_ivar_set(error, rb_intern_const("@path"), filepath);
} else {
error = rb_syserr_new(e, RSTRING_PTR(filepath));
RB_GC_GUARD(filepath);
}
return error;
}
pm_options_frozen_string_literal_init(&result->options);
@ -8843,7 +8855,7 @@ pm_parse_file(pm_parse_result_t *result, VALUE filepath)
VALUE
pm_load_parse_file(pm_parse_result_t *result, VALUE filepath)
{
VALUE error = pm_load_file(result, filepath);
VALUE error = pm_load_file(result, filepath, false);
if (NIL_P(error)) {
error = pm_parse_file(result, filepath);
}

Просмотреть файл

@ -72,7 +72,7 @@ typedef struct {
bool parsed;
} pm_parse_result_t;
VALUE pm_load_file(pm_parse_result_t *result, VALUE filepath);
VALUE pm_load_file(pm_parse_result_t *result, VALUE filepath, bool load_error);
VALUE pm_parse_file(pm_parse_result_t *result, VALUE filepath);
VALUE pm_load_parse_file(pm_parse_result_t *result, VALUE filepath);
VALUE pm_parse_string(pm_parse_result_t *result, VALUE source, VALUE filepath);

2
ruby.c
Просмотреть файл

@ -2157,7 +2157,7 @@ prism_script(ruby_cmdline_options_t *opt, pm_parse_result_t *result)
}
else {
pm_options_command_line_set(options, command_line);
error = pm_load_file(result, opt->script_name);
error = pm_load_file(result, opt->script_name, true);
// If reading the file did not error, at that point we load the command
// line options. We do it in this order so that if the main script fails

Просмотреть файл

@ -19,8 +19,6 @@ MSpec.register(:exclude, "Regexps with encoding modifiers preserves UTF-8 as /u
MSpec.register(:exclude, "A Symbol literal raises an SyntaxError at parse time when Symbol with invalid bytes")
## Core
MSpec.register(:exclude, "IO.popen with a leading Array argument accepts a trailing Hash of Process.exec options")
MSpec.register(:exclude, "IO.popen with a leading Array argument accepts an IO mode argument following the Array")
MSpec.register(:exclude, "TracePoint#inspect returns a String showing the event, method, path and line for a :return event")
MSpec.register(:exclude, "TracePoint.new includes multiple events when multiple event names are passed as params")
MSpec.register(:exclude, "TracePoint#path equals \"(eval at __FILE__:__LINE__)\" inside an eval for :end event")