Merge pull request #6081 from dearblue/open-x

Add "x" mode option for `IO.open`
This commit is contained in:
Yukihiro "Matz" Matsumoto
2023-10-17 13:27:07 +09:00
committed by GitHub
2 changed files with 36 additions and 3 deletions
+10 -3
View File
@@ -139,8 +139,7 @@ io_modestr_to_flags(mrb_state *mrb, const char *mode)
flags = O_WRONLY | O_CREAT | O_APPEND;
break;
default:
mrb_raisef(mrb, E_ARGUMENT_ERROR, "illegal access mode %s", mode);
flags = 0; /* not reached */
goto modeerr;
}
while (*m) {
@@ -150,17 +149,25 @@ io_modestr_to_flags(mrb_state *mrb, const char *mode)
flags |= O_BINARY;
#endif
break;
case 'x':
if (mode[0] != 'w') goto modeerr;
flags |= O_EXCL;
break;
case '+':
flags = (flags & ~OPEN_ACCESS_MODE_FLAGS) | O_RDWR;
break;
case ':':
/* XXX: PASSTHROUGH*/
default:
mrb_raisef(mrb, E_ARGUMENT_ERROR, "illegal access mode %s", mode);
goto modeerr;
}
}
return flags;
modeerr:
mrb_raisef(mrb, E_ARGUMENT_ERROR, "illegal access mode %s", mode);
return 0; /* not reached */
}
static int
+26
View File
@@ -260,4 +260,30 @@ assert('File.chmod') do
end
end
assert('File.open with "x" mode') do
File.unlink $mrbtest_io_wfname rescue nil
assert_nothing_raised do
File.open($mrbtest_io_wfname, "wx") {}
end
assert_raise(RuntimeError) do
File.open($mrbtest_io_wfname, "wx") {}
end
File.unlink $mrbtest_io_wfname rescue nil
assert_nothing_raised do
File.open($mrbtest_io_wfname, "w+x") {}
end
assert_raise(RuntimeError) do
File.open($mrbtest_io_wfname, "w+x") {}
end
assert_raise(ArgumentError) do
File.open($mrbtest_io_wfname, "rx") {}
end
assert_raise(ArgumentError) do
File.open($mrbtest_io_wfname, "ax") {}
end
end
MRubyIOTestUtil.io_test_cleanup