なかだです。

パッチも大きいのでruby-extに持ってきます。

At Sun, 6 Jan 2002 08:37:52 +0900,
Tanaka Akira <akr / m17n.org> wrote:
> ドキュメントには GzipWriter.open と GzipWriter.new にブロックを与えた
> 場合には自動的にクローズすると書いてあるんですが、そうなっていません。

ZSTREAM_FLAG_FINALIZEのon/offがいまいちよく分からなかったのです
が、こんな感じでしょうか。

> > 1.7 への対応は、とりあえず 1.6 で安定してから、と考えています。
> > # 単に 1.7 がどうなっているかを知らないだけだったりもする(汗;
> 
> 個人的にはすでに 1.7 に移行してしまっているので、1.7 に install しまし
> た。最小限の変更で済ますなら、NORETURN を消して str2cstr を
> rb_str2cstr に変えるだけで済むようです。

rb_str2cstr()はもう非推奨だったような。あと1.7対応としては、
allocateとかrb_str_buf_new()とか。NORETURNに関しては前から判別
に悩んでたんですが、一応うまくいくようです。

# でもどっちかというと、RB_NORETURNとか名前を変えるなりしてほし
# い。

できれば1.6->1.7の移行ガイドみたいのが欲しいなとは思ってるんで
すが。


diff -u2p ruby-zlib-0.5.0-pre1.orig/extconf.rb ruby-zlib-0.5.0-pre1/extconf.rb --- ruby-zlib-0.5.0-pre1.orig/extconf.rb Sat Jan 5 23:51:59 2002 +++ ruby-zlib-0.5.0-pre1/extconf.rb Sun Jan 6 12:23:30 2002 @@ -6,4 +6,36 @@ if have_library('z', 'deflateReset') and have_header('zlib.h') then + defines = [] + if method(:have_func).arity == 1 + have_library(Config::CONFIG["RUBY_SO_NAME"]) + else + header = ["ruby.h"] + end + have_func("rb_str_buf_new", *header) + + print "checking for new NORETURN... "; $>.flush + if egrep_cpp("^NORETURN", "#include \"config.h\"\nNORETURN(exit);\n") + print "no\n" + else + print "yes\n" + defines << "RUBY_NEW_NORETURN" + end + + print "checking for block_given?... " + if Kernel.respond_to? :block_given? + print "yes\n" + else + print "no\n" + defines << "rb_block_given_p=rb_iterator_p" + end + + print "checking for allocation framework... " + if Object.respond_to? :allocate + print "yes\n" + defines << "HAVE_OBJECT_ALLOCATE" + else + print "no\n" + end + print 'guessing OS_CODE... ' @@ -12,13 +44,20 @@ if have_library('z', 'deflateReset') and when 'os2_emx' print "OS/2\n" - $CFLAGS += ' -DOS_CODE=OS_OS2' + defines << 'OS_CODE=OS_OS2' when 'amigaos' print "Amiga\n" - $CFLAGS += ' -DOS_CODE=OS_AMIGA' + defines << 'OS_CODE=OS_AMIGA' when 'cygwin', 'mingw32' print "Win32\n" - $CFLAGS += ' -DOS_CODE=OS_WIN32' + defines 'OS_CODE=OS_WIN32' else print "Unix\n" + end + + defines = defines.collect {|d| " -D"+d}.join + if $CPPFLAGS + $CPPFLAGS += defines + else + $CFLAGS += defines end diff -u2p ruby-zlib-0.5.0-pre1.orig/zlib.c ruby-zlib-0.5.0-pre1/zlib.c --- ruby-zlib-0.5.0-pre1.orig/zlib.c Sat Jan 5 22:12:08 2002 +++ ruby-zlib-0.5.0-pre1/zlib.c Sun Jan 6 12:22:25 2002 @@ -1,2 +1,3 @@ +/* -*- c-file-style:"ruby"; tab-width:4 -*- */ /* * zlib.c - zlib support for Ruby @@ -15,6 +16,22 @@ #define P(val) rb_funcall(rb_mKernel, rb_intern("p"), 1, (val)) -#if RUBY_VERSION_CODE < 160 -#define rb_block_given_p() rb_iterator_p() +#ifndef StringValuePtr +#define StringValuePtr(s) STR2CSTR(s) +#endif +#ifdef str2cstr +#define StringPtrLen(str, p, l) ((p) = str2cstr((str), &(l))) +#else +#define StringPtrLen(str, p, l) ((p) = StringValuePtr(str), (l) = RSTRING(str)->len) +#endif + +#if !defined HAVE_RB_STR_BUF_NEW +#define rb_str_buf_new(l) rb_str_new(0, (l)) +#define rb_str_buf_new1(p, l) rb_str_new((p), (l)) +#elif !defined HAVE_RB_STR_BUF_NEW1 +#define rb_str_buf_new1(p, l) rb_str_buf_cat(rb_str_buf_new(0), (p), (l)) +#endif + +#ifndef OBJ_INFECT +#define OBJ_INFECT(target, source) do {if (OBJ_TAINTED(source)) OBJ_TAINT(target);} while (0) #endif @@ -47,5 +64,9 @@ static VALUE cStreamError, cDataError, c static +#ifdef RUBY_NEW_NORETURN +NORETURN(void raise_zlib_error(int err, const char *msg, VALUE next_in)); +#else void raise_zlib_error(int err, const char *msg, VALUE next_in) NORETURN; +#endif static @@ -135,5 +156,5 @@ VALUE do_checksum(int argc, VALUE *argv, if (!NIL_P(str)) { - buf = str2cstr(str, &len); + StringPtrLen(str, buf, len); } else { buf = Z_NULL; @@ -248,5 +269,5 @@ void zstream_expand_buffer(struct zstrea if (NIL_P(z->buf)) { - z->buf = rb_str_new(0, ZSTREAM_INITIAL_BUFSIZE); + z->buf = rb_str_buf_new(ZSTREAM_INITIAL_BUFSIZE); z->buf_filled = 0; z->stream.next_out = RSTRING(z->buf)->ptr; @@ -274,5 +295,5 @@ void zstream_append_buffer(struct zstrea { if (NIL_P(z->buf)) { - z->buf = rb_str_new(src, len); + z->buf = rb_str_buf_new1(src, len); z->buf_filled = len; z->stream.next_out = RSTRING(z->buf)->ptr; @@ -357,7 +378,7 @@ void zstream_append_input(struct zstream { if (NIL_P(z->input)) - z->input = rb_str_new(src, len); + z->input = rb_str_buf_new1(src, len); else - rb_str_cat(z->input, src, len); + rb_str_buf_cat(z->input, src, len); } @@ -468,6 +489,5 @@ void zstream_run_loop(struct zstream *z, } if (err != Z_OK) { - rest = rb_str_new(z->stream.next_in, - z->stream.avail_in); + rest = rb_str_buf_new1(z->stream.next_in, z->stream.avail_in); if (err != Z_NEED_DICT) z->input = Qnil; @@ -482,5 +502,5 @@ void zstream_run_loop(struct zstream *z, if (z->stream.avail_in > 0) - z->input = rb_str_new(z->stream.next_in, z->stream.avail_in); + z->input = rb_str_buf_new1(z->stream.next_in, z->stream.avail_in); else z->input = Qnil; @@ -517,5 +537,5 @@ void zstream_run2(struct zstream *z, VAL zstream_run(z, 0, 0, Z_FINISH); else { - p = str2cstr(src, &n); + StringPtrLen(src, p, n); zstream_run(z, p, n, flush); } @@ -683,4 +703,11 @@ VALUE rb_zstream_closed_p(VALUE obj) +#ifdef HAVE_OBJECT_ALLOCATE +static +VALUE rb_deflate_s_allocate(VALUE klass) +{ + return zstream_deflate_new(klass); +} +#else static VALUE rb_deflate_s_new(int argc, VALUE *argv, VALUE klass) @@ -692,4 +719,5 @@ VALUE rb_deflate_s_new(int argc, VALUE * return obj; } +#endif @@ -842,5 +870,5 @@ VALUE rb_deflate_set_dictionary(VALUE ob OBJ_INFECT(obj, v_dic); - dic = str2cstr(v_dic, &diclen); + StringPtrLen(v_dic, dic, diclen); err = deflateSetDictionary(&z->stream, dic, diclen); if (err != Z_OK) @@ -855,4 +883,11 @@ VALUE rb_deflate_set_dictionary(VALUE ob /*-------- class Zlib::Inflate --------*/ +#ifdef HAVE_OBJECT_ALLOCATE +static +VALUE rb_inflate_s_allocate(VALUE klass) +{ + return zstream_inflate_new(klass); +} +#else static VALUE rb_inflate_s_new(int argc, VALUE *argv, VALUE klass) @@ -864,4 +899,5 @@ VALUE rb_inflate_s_new(int argc, VALUE * return obj; } +#endif @@ -932,5 +968,5 @@ VALUE rb_inflate_inflate(VALUE obj, VALU dst = zstream_detach_buffer(z); else { - p = str2cstr(src, &len); + StringPtrLen(src, p, len); zstream_append_buffer(z, p, len); dst = rb_str_new(0, 0); @@ -959,5 +995,5 @@ VALUE rb_inflate_addstr(VALUE obj, VALUE if (ZSTREAM_IS_FINISHED(z)) { if (!NIL_P(src)) { - p = str2cstr(src, &len); + StringPtrLen(src, p, len); zstream_append_buffer(z, p, len); } @@ -1011,5 +1047,5 @@ VALUE rb_inflate_set_dictionary(VALUE ob OBJ_INFECT(obj, v_dic); - dic = str2cstr(v_dic, &diclen); + StringPtrLen(v_dic, dic, diclen); err = inflateSetDictionary(&z->stream, dic, diclen); if (err != Z_OK) @@ -1507,12 +1543,12 @@ void gzfile_writer_end(struct gzfile *gz gzfile_make_footer(gz); - if (gz->z.flags & ZSTREAM_FLAG_FINALIZE) + if (gz->z.flags & ZSTREAM_FLAG_FINALIZE) { rb_warn("Zlib::GzipWriter object must be closed explicitly."); - if ((gz->z.flags & ZSTREAM_FLAG_FINALIZE) - && (!rb_respond_to(gz->io, id_write) || OBJ_IS_FREED(gz->io))) { - rb_warn("gzip footer is not written; broken gzip file"); - zstream_end(&gz->z); - return; + if (OBJ_IS_FREED(gz->io) || !rb_respond_to(gz->io, id_write)) { + rb_warn("gzip footer is not written; broken gzip file"); + zstream_end(&gz->z); + return; + } } gzfile_write_raw(gz); @@ -1788,16 +1824,14 @@ VALUE gzfile_ensure_close(VALUE obj) Data_Get_Struct(obj, struct gzfile, gz); if (ZSTREAM_IS_READY(&gz->z)) - rb_funcall(gz->io, id_close, 0); + gzfile_close(gz, 1); return Qnil; } +#ifdef HAVE_OBJECT_ALLOCATE static -VALUE rb_gzwriter_s_new(int argc, VALUE *argv, VALUE klass) +VALUE gzfile_s_new(int argc, VALUE *argv, VALUE klass) { - VALUE obj; - - obj = gzfile_writer_new(klass); - rb_obj_call_init(obj, argc, argv); + VALUE obj = rb_class_new_instance(argc, argv, klass); if (rb_block_given_p()) @@ -1806,10 +1840,10 @@ VALUE rb_gzwriter_s_new(int argc, VALUE return obj; } - +#endif static -VALUE rb_gzwriter_s_open(int argc, VALUE *argv, VALUE klass) +VALUE gzfile_s_open(int argc, VALUE *argv, const char *mode) { - VALUE obj, io, filename; + VALUE filename; if (argc < 1) @@ -1817,8 +1851,23 @@ VALUE rb_gzwriter_s_open(int argc, VALUE filename = argv[0]; Check_SafeStr(filename); - io = rb_file_open(STR2CSTR(filename), "wb"); + return rb_file_open(StringValuePtr(filename), "wb"); +} + + +#ifdef HAVE_OBJECT_ALLOCATE +static +VALUE rb_gzwriter_s_allocate(VALUE klass) +{ + return gzfile_writer_new(klass); +} + +#define rb_gzwriter_s_new gzfile_s_new +#else +static +VALUE rb_gzwriter_s_new(int argc, VALUE *argv, VALUE klass) +{ + VALUE obj; obj = gzfile_writer_new(klass); - argv[0] = io; rb_obj_call_init(obj, argc, argv); @@ -1828,4 +1877,13 @@ VALUE rb_gzwriter_s_open(int argc, VALUE return obj; } +#endif + + +static +VALUE rb_gzwriter_s_open(int argc, VALUE *argv, VALUE klass) +{ + argv[0] = gzfile_s_open(argc, argv, "wb"); + return rb_gzwriter_s_new(argc, argv, klass); +} @@ -1979,4 +2037,13 @@ VALUE rb_gzwriter_puts(int argc, VALUE * /*-------- class Zlib::GzipReader --------*/ +#ifdef HAVE_OBJECT_ALLOCATE +static +VALUE rb_gzreader_s_allocate(VALUE klass) +{ + return gzfile_reader_new(klass); +} + +#define rb_gzreader_s_new gzfile_s_new +#else static VALUE rb_gzreader_s_new(int argc, VALUE *argv, VALUE klass) @@ -1992,4 +2059,5 @@ VALUE rb_gzreader_s_new(int argc, VALUE return obj; } +#endif @@ -1997,20 +2065,6 @@ static VALUE rb_gzreader_s_open(int argc, VALUE *argv, VALUE klass) { - VALUE obj, io, filename; - - if (argc < 1) - rb_raise(rb_eArgError, "wrong number of arguments (0 for 1)"); - filename = argv[0]; - Check_SafeStr(filename); - io = rb_file_open(STR2CSTR(filename), "rb"); - - obj = gzfile_reader_new(klass); - argv[0] = io; - rb_obj_call_init(obj, argc, argv); - - if (rb_block_given_p()) - return rb_ensure(rb_yield, obj, gzfile_ensure_close, obj); - else - return obj; + argv[0] = gzfile_s_open(argc, argv, "rb"); + return rb_gzreader_s_new(argc, argv, klass); } @@ -2336,5 +2390,9 @@ void Init_zlib() rb_define_singleton_method(cDeflate, "deflate", rb_deflate_s_deflate, -1); +#ifdef HAVE_OBJECT_ALLOCATE + rb_define_singleton_method(cDeflate, "allocate", rb_deflate_s_allocate, 0); +#else rb_define_singleton_method(cDeflate, "new", rb_deflate_s_new, -1); +#endif rb_define_method(cDeflate, "initialize", rb_deflate_initialize, -1); rb_define_method(cDeflate, "clone", rb_deflate_clone, 0); @@ -2353,5 +2411,9 @@ void Init_zlib() rb_define_singleton_method(cInflate, "inflate", rb_inflate_s_inflate, 1); +#ifdef HAVE_OBJECT_ALLOCATE + rb_define_singleton_method(cInflate, "allocate", rb_inflate_s_allocate, 0); +#else rb_define_singleton_method(cInflate, "new", rb_inflate_s_new, -1); +#endif rb_define_method(cInflate, "initialize", rb_inflate_initialize, -1); rb_define_method(cInflate, "inflate", rb_inflate_inflate, 1); @@ -2450,4 +2512,7 @@ void Init_zlib() rb_define_method(cGzipWriter, "tell", rb_gzfile_total_in, 0); +#ifdef HAVE_OBJECT_ALLOCATE + rb_define_singleton_method(cGzipWriter, "allocate", rb_gzwriter_s_allocate, 0); +#endif rb_define_singleton_method(cGzipWriter, "new", rb_gzwriter_s_new, -1); rb_define_singleton_method(cGzipWriter, "open", rb_gzwriter_s_open,-1); @@ -2461,4 +2526,7 @@ void Init_zlib() rb_define_method(cGzipWriter, "puts", rb_gzwriter_puts, -1); +#ifdef HAVE_OBJECT_ALLOCATE + rb_define_singleton_method(cGzipReader, "allocate", rb_gzreader_s_allocate, 0); +#endif rb_define_singleton_method(cGzipReader, "new", rb_gzreader_s_new, -1); rb_define_singleton_method(cGzipReader, "open", rb_gzreader_s_open,-1);
-- --- 僕の前にBugはない。 --- 僕の後ろにBugはできる。 中田 伸悦