From 91aa8da578e64a1f2b4bd3ea75d9c3c48d113e3c Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 14 Nov 2013 16:06:09 +0000 Subject: 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 --- ext/objspace/objspace_dump.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'ext/objspace/objspace_dump.c') diff --git a/ext/objspace/objspace_dump.c b/ext/objspace/objspace_dump.c index 290a4434b4..df1395d07a 100644 --- a/ext/objspace/objspace_dump.c +++ b/ext/objspace/objspace_dump.c @@ -276,12 +276,11 @@ root_obj_i(const char *category, VALUE obj, void *data) dc->roots++; } -#ifndef HAVE_MKSTEMP -#define dump_output(dc, opts, output, filename) dump_output(dc, opts, output) -#endif 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)) 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; } else if (output == sym_file) { -#ifdef HAVE_MKSTEMP - int fd = mkstemp(filename); - dc->string = rb_filesystem_str_new_cstr(filename); - if (fd == -1) rb_sys_fail_path(dc->string); - dc->stream = fdopen(fd, "w"); -#else - rb_raise(rb_eArgError, "output to temprary file is not supported"); -#endif + rb_io_t *fptr; + rb_require("tempfile"); + tmp = rb_assoc_new(rb_str_new_cstr(filename), rb_str_new_cstr(".json")); + tmp = rb_funcallv(rb_path2class("Tempfile"), rb_intern("create"), 1, &tmp); + io: + dc->string = rb_io_get_write_io(tmp); + rb_io_flush(dc->string); + GetOpenFile(dc->string, fptr); + dc->stream = rb_io_stdio_file(fptr); } else if (output == sym_string) { dc->string = rb_str_new_cstr(""); } + else if (!NIL_P(tmp = rb_io_check_io(output))) { + output = sym_file; + goto io; + } else { rb_raise(rb_eArgError, "wrong output option: %"PRIsVALUE, output); } @@ -315,7 +319,7 @@ dump_result(struct dump_config *dc, VALUE output) return dc->string; } else if (output == sym_file) { - fclose(dc->stream); + rb_io_flush(dc->string); return dc->string; } else { @@ -340,9 +344,7 @@ dump_result(struct dump_config *dc, VALUE output) static VALUE objspace_dump(int argc, VALUE *argv, VALUE os) { -#ifdef HAVE_MKSTEMP - char filename[] = "/tmp/rubyobjXXXXXX"; -#endif + static const char filename[] = "rubyobj"; VALUE obj = Qnil, opts = Qnil, output; struct dump_config dc = {0,}; @@ -372,9 +374,7 @@ objspace_dump(int argc, VALUE *argv, VALUE os) static VALUE objspace_dump_all(int argc, VALUE *argv, VALUE os) { -#ifdef HAVE_MKSTEMP - char filename[] = "/tmp/rubyheapXXXXXX"; -#endif + static const char filename[] = "rubyheap"; VALUE opts = Qnil, output; struct dump_config dc = {0,}; -- cgit v1.2.3