1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* io.c (rb_io_initialize): accept hash argument.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18756 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2008-08-21 17:09:56 +00:00
parent a0cc5b19b7
commit dd7cf02eab
3 changed files with 32 additions and 13 deletions

View file

@ -1,3 +1,7 @@
Fri Aug 22 02:08:58 2008 Tanaka Akira <akr@fsij.org>
* io.c (rb_io_initialize): accept hash argument.
Thu Aug 21 23:51:51 2008 Shugo Maeda <shugo@ruby-lang.org>
* strftime.c (rb_strftime): supported %F and %<precision>N.

28
io.c
View file

@ -5472,31 +5472,31 @@ rb_io_initialize(int argc, VALUE *argv, VALUE io)
VALUE fnum, mode, orig;
rb_io_t *fp, *ofp = NULL;
int fd, flags, modenum = O_RDONLY;
convconfig_t convconfig;
VALUE opt;
rb_secure(4);
opt = pop_last_hash(&argc, &argv);
rb_scan_args(argc, argv, "11", &fnum, &mode);
if (argc == 2) {
if (FIXNUM_P(mode)) {
modenum = FIX2LONG(mode);
}
else {
SafeStringValue(mode);
modenum = rb_io_mode_modenum(StringValueCStr(mode));
}
}
rb_io_extract_modeenc(mode, opt, &modenum, &flags, &convconfig);
orig = rb_io_check_io(fnum);
if (NIL_P(orig)) {
fd = NUM2INT(fnum);
UPDATE_MAXFD(fd);
if (argc != 2) {
if (NIL_P(mode)) {
#if defined(HAVE_FCNTL) && defined(F_GETFL)
modenum = fcntl(fd, F_GETFL);
if (modenum == -1) rb_sys_fail(0);
flags = rb_io_modenum_flags(modenum);
#endif
}
MakeOpenFile(io, fp);
fp->fd = fd;
fp->mode = rb_io_modenum_flags(modenum);
fp->mode = flags;
fp->enc = convconfig.enc;
fp->enc2 = convconfig.enc2;
clear_codeconv(fp);
io_check_tty(fp);
}
else if (RFILE(io)->fptr) {
@ -5508,8 +5508,7 @@ rb_io_initialize(int argc, VALUE *argv, VALUE io)
VALUE s = rb_inspect(orig);
rb_raise(rb_eIOError, "too many shared IO for %s", StringValueCStr(s));
}
if (argc == 2) {
flags = rb_io_modenum_flags(modenum);
if (!NIL_P(mode)) {
if ((ofp->mode ^ flags) & (FMODE_READWRITE|FMODE_BINMODE)) {
if (FIXNUM_P(mode)) {
rb_raise(rb_eArgError, "incompatible mode 0x%x", modenum);
@ -5519,6 +5518,9 @@ rb_io_initialize(int argc, VALUE *argv, VALUE io)
}
}
}
if (convconfig.enc || convconfig.enc2) {
rb_raise(rb_eArgError, "encoding specified for shared IO");
}
ofp->refcnt++;
RFILE(io)->fptr = ofp;
}

View file

@ -206,6 +206,19 @@ EOT
}
end
def test_io_new_enc
with_tmpdir {
generate_file("tmp", "\xa1")
fd = IO.sysopen("tmp")
f = IO.new(fd, "r:sjis")
begin
assert_equal(Encoding::Shift_JIS, f.read.encoding)
ensure
f.close
end
}
end
def test_stdin
assert_equal(Encoding.default_external, STDIN.external_encoding)
assert_equal(nil, STDIN.internal_encoding)