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

* dir.c (dir_inspect), io.c (rb_io_inspect): keep encoding of path.

[Bug #6072]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34795 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2012-02-24 22:53:51 +00:00
parent 85738261a5
commit eec0b2d88a
5 changed files with 44 additions and 15 deletions

View file

@ -1,3 +1,8 @@
Sat Feb 25 07:53:49 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
* dir.c (dir_inspect), io.c (rb_io_inspect): keep encoding of path.
[Bug #6072]
Sat Feb 25 07:53:40 2012 Nobuyoshi Nakada <nobu@ruby-lang.org> Sat Feb 25 07:53:40 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
* dir.c (dir_initialize): keep path in original encoding. * dir.c (dir_initialize): keep path in original encoding.

8
dir.c
View file

@ -485,8 +485,12 @@ dir_inspect(VALUE dir)
TypedData_Get_Struct(dir, struct dir_data, &dir_data_type, dirp); TypedData_Get_Struct(dir, struct dir_data, &dir_data_type, dirp);
if (!NIL_P(dirp->path)) { if (!NIL_P(dirp->path)) {
const char *c = rb_obj_classname(dir); VALUE str = rb_str_new_cstr("#<");
return rb_sprintf("#<%s:%s>", c, RSTRING_PTR(dirp->path)); rb_str_append(str, rb_class_name(CLASS_OF(dir)));
rb_str_cat2(str, ":");
rb_str_append(str, dirp->path);
rb_str_cat2(str, ">");
return str;
} }
return rb_funcall(dir, rb_intern("to_s"), 0, 0); return rb_funcall(dir, rb_intern("to_s"), 0, 0);
} }

22
io.c
View file

@ -1778,31 +1778,29 @@ static VALUE
rb_io_inspect(VALUE obj) rb_io_inspect(VALUE obj)
{ {
rb_io_t *fptr; rb_io_t *fptr;
const char *cname; VALUE result;
char fd_desc[4+sizeof(int)*3]; static const char closed[] = " (closed)";
const char *path;
const char *st = "";
fptr = RFILE(rb_io_taint_check(obj))->fptr; fptr = RFILE(rb_io_taint_check(obj))->fptr;
if (!fptr) return rb_any_to_s(obj); if (!fptr) return rb_any_to_s(obj);
cname = rb_obj_classname(obj); result = rb_str_new_cstr("#<");
rb_str_append(result, rb_class_name(CLASS_OF(obj)));
rb_str_cat2(result, ":");
if (NIL_P(fptr->pathv)) { if (NIL_P(fptr->pathv)) {
if (fptr->fd < 0) { if (fptr->fd < 0) {
path = ""; rb_str_cat(result, closed+1, strlen(closed)-1);
st = "(closed)";
} }
else { else {
snprintf(fd_desc, sizeof(fd_desc), "fd %d", fptr->fd); rb_str_catf(result, "fd %d", fptr->fd);
path = fd_desc;
} }
} }
else { else {
path = RSTRING_PTR(fptr->pathv); rb_str_append(result, fptr->pathv);
if (fptr->fd < 0) { if (fptr->fd < 0) {
st = " (closed)"; rb_str_cat(result, closed, strlen(closed));
} }
} }
return rb_sprintf("#<%s:%s%s>", cname, path, st); return rb_str_cat2(result, ">");
} }
/* /*

View file

@ -224,5 +224,16 @@ class TestDir_M17N < Test::Unit::TestCase
} }
assert_equal(paths.map(&:encoding), encs, bug6071) assert_equal(paths.map(&:encoding), encs, bug6071)
end end
end
def test_inspect_nonascii
bug6072 = '[ruby-dev:45280]'
paths = ["\u{3042}".encode("sjis"), "\u{ff}".encode("iso-8859-1")]
encs = with_tmpdir {
paths.map {|path|
Dir.mkdir(path)
Dir.open(path) {|d| d.inspect.encoding}
}
}
assert_equal(paths.map(&:encoding), encs, bug6072)
end
end

View file

@ -2367,4 +2367,15 @@ EOT
} }
assert_equal(paths.map(&:encoding), encs, bug6071) assert_equal(paths.map(&:encoding), encs, bug6071)
end end
def test_inspect_nonascii
bug6072 = '[ruby-dev:45280]'
paths = ["\u{3042}".encode("sjis"), "\u{ff}".encode("iso-8859-1")]
encs = with_tmpdir {
paths.map {|path|
open(path, "wb") {|f| f.inspect.encoding}
}
}
assert_equal(paths.map(&:encoding), encs, bug6072)
end
end end