mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
objspace_dump.c: refine output
* ext/objspace/objspace_dump.c (dump_output): allow IO object as output, and use Tempfile.create and return open file instead of mkstemp() and path name for :file output. [ruby-core:58266] [Bug #9102] * test/objspace/test_objspace.rb (TestObjSpace#dump_my_heap_please): remove temporary output file. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43679 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
6cbe261626
commit
91aa8da578
4 changed files with 55 additions and 24 deletions
10
ChangeLog
10
ChangeLog
|
@ -1,3 +1,13 @@
|
||||||
|
Fri Nov 15 01:06:04 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* ext/objspace/objspace_dump.c (dump_output): allow IO object as
|
||||||
|
output, and use Tempfile.create and return open file instead of
|
||||||
|
mkstemp() and path name for :file output.
|
||||||
|
[ruby-core:58266] [Bug #9102]
|
||||||
|
|
||||||
|
* test/objspace/test_objspace.rb (TestObjSpace#dump_my_heap_please):
|
||||||
|
remove temporary output file.
|
||||||
|
|
||||||
Thu Nov 14 23:39:00 2013 CHIKANAGA Tomoyuki <nagachika@ruby-lang.org>
|
Thu Nov 14 23:39:00 2013 CHIKANAGA Tomoyuki <nagachika@ruby-lang.org>
|
||||||
|
|
||||||
* ext/bigdecimal/lib/bigdecimal/util.rb: [DOC] remove example of
|
* ext/bigdecimal/lib/bigdecimal/util.rb: [DOC] remove example of
|
||||||
|
|
|
@ -1,3 +1,2 @@
|
||||||
$INCFLAGS << " -I$(topdir) -I$(top_srcdir)"
|
$INCFLAGS << " -I$(topdir) -I$(top_srcdir)"
|
||||||
have_func("mkstemp")
|
|
||||||
create_makefile('objspace')
|
create_makefile('objspace')
|
||||||
|
|
|
@ -276,12 +276,11 @@ root_obj_i(const char *category, VALUE obj, void *data)
|
||||||
dc->roots++;
|
dc->roots++;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef HAVE_MKSTEMP
|
|
||||||
#define dump_output(dc, opts, output, filename) dump_output(dc, opts, output)
|
|
||||||
#endif
|
|
||||||
static VALUE
|
static VALUE
|
||||||
dump_output(struct dump_config *dc, VALUE opts, VALUE output, char *filename)
|
dump_output(struct dump_config *dc, VALUE opts, VALUE output, const char *filename)
|
||||||
{
|
{
|
||||||
|
VALUE tmp;
|
||||||
|
|
||||||
if (RTEST(opts))
|
if (RTEST(opts))
|
||||||
output = rb_hash_aref(opts, sym_output);
|
output = rb_hash_aref(opts, sym_output);
|
||||||
|
|
||||||
|
@ -290,18 +289,23 @@ dump_output(struct dump_config *dc, VALUE opts, VALUE output, char *filename)
|
||||||
dc->string = Qnil;
|
dc->string = Qnil;
|
||||||
}
|
}
|
||||||
else if (output == sym_file) {
|
else if (output == sym_file) {
|
||||||
#ifdef HAVE_MKSTEMP
|
rb_io_t *fptr;
|
||||||
int fd = mkstemp(filename);
|
rb_require("tempfile");
|
||||||
dc->string = rb_filesystem_str_new_cstr(filename);
|
tmp = rb_assoc_new(rb_str_new_cstr(filename), rb_str_new_cstr(".json"));
|
||||||
if (fd == -1) rb_sys_fail_path(dc->string);
|
tmp = rb_funcallv(rb_path2class("Tempfile"), rb_intern("create"), 1, &tmp);
|
||||||
dc->stream = fdopen(fd, "w");
|
io:
|
||||||
#else
|
dc->string = rb_io_get_write_io(tmp);
|
||||||
rb_raise(rb_eArgError, "output to temprary file is not supported");
|
rb_io_flush(dc->string);
|
||||||
#endif
|
GetOpenFile(dc->string, fptr);
|
||||||
|
dc->stream = rb_io_stdio_file(fptr);
|
||||||
}
|
}
|
||||||
else if (output == sym_string) {
|
else if (output == sym_string) {
|
||||||
dc->string = rb_str_new_cstr("");
|
dc->string = rb_str_new_cstr("");
|
||||||
}
|
}
|
||||||
|
else if (!NIL_P(tmp = rb_io_check_io(output))) {
|
||||||
|
output = sym_file;
|
||||||
|
goto io;
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
rb_raise(rb_eArgError, "wrong output option: %"PRIsVALUE, output);
|
rb_raise(rb_eArgError, "wrong output option: %"PRIsVALUE, output);
|
||||||
}
|
}
|
||||||
|
@ -315,7 +319,7 @@ dump_result(struct dump_config *dc, VALUE output)
|
||||||
return dc->string;
|
return dc->string;
|
||||||
}
|
}
|
||||||
else if (output == sym_file) {
|
else if (output == sym_file) {
|
||||||
fclose(dc->stream);
|
rb_io_flush(dc->string);
|
||||||
return dc->string;
|
return dc->string;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -340,9 +344,7 @@ dump_result(struct dump_config *dc, VALUE output)
|
||||||
static VALUE
|
static VALUE
|
||||||
objspace_dump(int argc, VALUE *argv, VALUE os)
|
objspace_dump(int argc, VALUE *argv, VALUE os)
|
||||||
{
|
{
|
||||||
#ifdef HAVE_MKSTEMP
|
static const char filename[] = "rubyobj";
|
||||||
char filename[] = "/tmp/rubyobjXXXXXX";
|
|
||||||
#endif
|
|
||||||
VALUE obj = Qnil, opts = Qnil, output;
|
VALUE obj = Qnil, opts = Qnil, output;
|
||||||
struct dump_config dc = {0,};
|
struct dump_config dc = {0,};
|
||||||
|
|
||||||
|
@ -372,9 +374,7 @@ objspace_dump(int argc, VALUE *argv, VALUE os)
|
||||||
static VALUE
|
static VALUE
|
||||||
objspace_dump_all(int argc, VALUE *argv, VALUE os)
|
objspace_dump_all(int argc, VALUE *argv, VALUE os)
|
||||||
{
|
{
|
||||||
#ifdef HAVE_MKSTEMP
|
static const char filename[] = "rubyheap";
|
||||||
char filename[] = "/tmp/rubyheapXXXXXX";
|
|
||||||
#endif
|
|
||||||
VALUE opts = Qnil, output;
|
VALUE opts = Qnil, output;
|
||||||
struct dump_config dc = {0,};
|
struct dump_config dc = {0,};
|
||||||
|
|
||||||
|
|
|
@ -204,17 +204,38 @@ class TestObjSpace < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_dump
|
def test_dump_to_default
|
||||||
|
line = nil
|
||||||
info = nil
|
info = nil
|
||||||
ObjectSpace.trace_object_allocations do
|
ObjectSpace.trace_object_allocations do
|
||||||
|
line = __LINE__ + 1
|
||||||
str = "hello world"
|
str = "hello world"
|
||||||
info = ObjectSpace.dump(str)
|
info = ObjectSpace.dump(str)
|
||||||
end
|
end
|
||||||
|
assert_dump_object(info, line)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_dump_to_io
|
||||||
|
line = nil
|
||||||
|
info = IO.pipe do |r, w|
|
||||||
|
th = Thread.start {r.read}
|
||||||
|
ObjectSpace.trace_object_allocations do
|
||||||
|
line = __LINE__ + 1
|
||||||
|
str = "hello world"
|
||||||
|
ObjectSpace.dump(str, output: w)
|
||||||
|
end
|
||||||
|
w.close
|
||||||
|
th.value
|
||||||
|
end
|
||||||
|
assert_dump_object(info, line)
|
||||||
|
end
|
||||||
|
|
||||||
|
def assert_dump_object(info, line)
|
||||||
|
loc = caller_locations(1, 1)[0]
|
||||||
assert_match /"type":"STRING"/, info
|
assert_match /"type":"STRING"/, info
|
||||||
assert_match /"embedded":true, "bytesize":11, "value":"hello world", "encoding":"UTF-8"/, info
|
assert_match /"embedded":true, "bytesize":11, "value":"hello world", "encoding":"UTF-8"/, info
|
||||||
assert_match /"file":"#{Regexp.escape __FILE__}", "line":#{__LINE__-6}/, info
|
assert_match /"file":"#{Regexp.escape __FILE__}", "line":#{line}/, info
|
||||||
assert_match /"method":"test_dump"/, info
|
assert_match /"method":"#{loc.base_label}"/, info
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_dump_all
|
def test_dump_all
|
||||||
|
@ -235,13 +256,14 @@ class TestObjSpace < Test::Unit::TestCase
|
||||||
ObjectSpace.trace_object_allocations_start
|
ObjectSpace.trace_object_allocations_start
|
||||||
GC.start
|
GC.start
|
||||||
"TEST STRING".force_encoding("UTF-8")
|
"TEST STRING".force_encoding("UTF-8")
|
||||||
ObjectSpace.dump_all()
|
ObjectSpace.dump_all().path
|
||||||
end
|
end
|
||||||
|
|
||||||
puts dump_my_heap_please
|
puts dump_my_heap_please
|
||||||
end;
|
end;
|
||||||
skip if /is not supported/ =~ error
|
skip if /is not supported/ =~ error
|
||||||
assert_match(entry, File.read(output))
|
assert_match(entry, File.read(output))
|
||||||
|
File.unlink(output)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue