From c3c7d1486ba872009e7a2c4404faef554708c5d8 Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Tue, 27 Nov 2012 23:49:11 +0900 Subject: [PATCH] Deprecate #{lines,bytes,chars,codepoints} of IO-likes. * io.c (rb_io_lines, rb_io_bytes, rb_io_chars, rb_io_codepoints): Deprecate IO#{lines,bytes,chars,codepoints} and those of ARGF. [Feature #6670] * ext/stringio/stringio.c (strio_lines, strio_bytes, strio_chars) (strio_codepoints): Deprecate StringIO#{lines,bytes,chars,codepoints}. [Feature #6670] * ext/zlib/zlib.c (rb_gzreader_lines, rb_gzreader_bytes): Deprecate Zlib::GzipReader#{lines,bytes}. [Feature #6670] --- ChangeLog | 13 +++++ NEWS | 28 ++++++++++ ext/stringio/stringio.c | 70 +++++++++++++++++------- ext/zlib/zlib.c | 32 ++++++++++- io.c | 140 ++++++++++++++++++++++++++++++++++++++---------- test/ruby/test_argf.rb | 34 ++++++++++-- test/ruby/test_io.rb | 37 +++++++++++-- 7 files changed, 299 insertions(+), 55 deletions(-) diff --git a/ChangeLog b/ChangeLog index ef5d5d5..d344d39 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +Tue Nov 27 23:45:47 2012 Akinori MUSHA + + * io.c (rb_io_lines, rb_io_bytes, rb_io_chars, rb_io_codepoints): + Deprecate IO#{lines,bytes,chars,codepoints} and those of ARGF. + [Feature #6670] + + * ext/stringio/stringio.c (strio_lines, strio_bytes, strio_chars) + (strio_codepoints): Deprecate + StringIO#{lines,bytes,chars,codepoints}. [Feature #6670] + + * ext/zlib/zlib.c (rb_gzreader_lines, rb_gzreader_bytes): + Deprecate Zlib::GzipReader#{lines,bytes}. [Feature #6670] + Tue Nov 27 22:03:09 2012 Akinori MUSHA * string.c (rb_str_enumerate_chars, rb_str_enumerate_codepoints) diff --git a/NEWS b/NEWS index 2448264..b9ab5c4 100644 --- a/NEWS +++ b/NEWS @@ -66,6 +66,10 @@ with all sufficient information, see the ChangeLog file. * extended method: * Hash#default_proc= can be passed nil to clear the default proc. + * IO + * deprecated methods: + * IO#lines, #bytes, #chars and #codepoints are deprecated. + * Kernel * added method: * added Kernel#Hash conversion method like Array() or Float(). @@ -306,6 +310,10 @@ with all sufficient information, see the ChangeLog file. * Shellwords#shelljoin() accepts non-string objects in the given array, each of which is stringified using to_s. +* stringio + * deprecated methods: + * StringIO#lines, #bytes, #chars and #codepoints are deprecated. + * syslog * Added Syslog::Logger which provides a Logger API atop Syslog. * Syslog::Priority, Syslog::Level, Syslog::Option and Syslog::Macros @@ -329,6 +337,8 @@ with all sufficient information, see the ChangeLog file. * Added support for the new deflate strategies Zlib::RLE and Zlib::FIXED. * Zlib streams are now processed without the GVL. This allows gzip, zlib and deflate streams to be processed in parallel. + * deprecated methods: + * Zlib::GzipReader#lines and #bytes are deprecated. === Language changes @@ -354,6 +364,24 @@ with all sufficient information, see the ChangeLog file. works because str.lines returns an array. Replace lines with each_line in such cases. + * IO#lines + * IO#chars + * IO#codepoints + * IO#bytes + * ARGF#lines + * ARGF#chars + * ARGF#codepoints + * ARGF#bytes + * StringIO#lines + * StringIO#chars + * StringIO#codepoints + * StringIO#bytes + * Zlib::GzipReader#lines + * Zlib::GzipReader#bytes + + These methods are deprecated in favor of each_line, each_byte, + each_char and each_codepoint. + * Signal.trap See above. diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index 975a308..d763854 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -630,9 +630,6 @@ strio_get_sync(VALUE self) /* * call-seq: - * strio.bytes {|byte| block } -> strio - * strio.bytes -> anEnumerator - * * strio.each_byte {|byte| block } -> strio * strio.each_byte -> anEnumerator * @@ -653,6 +650,18 @@ strio_each_byte(VALUE self) } /* + * This is a deprecated alias for each_byte. + */ +static VALUE +strio_bytes(VALUE self) +{ + rb_warn("StringIO#bytes is deprecated; use #each_byte instead"); + if (!rb_block_given_p()) + return rb_enumeratorize(self, ID2SYM(rb_intern("each_byte")), 0, 0); + return strio_each_byte(self); +} + +/* * call-seq: * strio.getc -> string or nil * @@ -840,9 +849,6 @@ strio_readbyte(VALUE self) /* * call-seq: - * strio.chars {|char| block } -> strio - * strio.chars -> anEnumerator - * * strio.each_char {|char| block } -> strio * strio.each_char -> anEnumerator * @@ -862,10 +868,19 @@ strio_each_char(VALUE self) } /* + * This is a deprecated alias for each_char. + */ +static VALUE +strio_chars(VALUE self) +{ + rb_warn("StringIO#chars is deprecated; use #each_char instead"); + if (!rb_block_given_p()) + return rb_enumeratorize(self, ID2SYM(rb_intern("each_char")), 0, 0); + return strio_each_char(self); +} + +/* * call-seq: - * strio.codepoints {|c| block } -> strio - * strio.codepoints -> anEnumerator - * * strio.each_codepoint {|c| block } -> strio * strio.each_codepoint -> anEnumerator * @@ -896,6 +911,18 @@ strio_each_codepoint(VALUE self) return self; } +/* + * This is a deprecated alias for each_codepoint. + */ +static VALUE +strio_codepoints(VALUE self) +{ + rb_warn("StringIO#codepoints is deprecated; use #each_codepoint instead"); + if (!rb_block_given_p()) + return rb_enumeratorize(self, ID2SYM(rb_intern("each_codepoint")), 0, 0); + return strio_each_codepoint(self); +} + /* Boyer-Moore search: copied from regex.c */ static void bm_init_skip(long *skip, const char *pat, long m) @@ -1067,11 +1094,6 @@ strio_readline(int argc, VALUE *argv, VALUE self) * strio.each_line(sep,limit) {|line| block } -> strio * strio.each_line(...) -> anEnumerator * - * strio.lines(sep=$/) {|line| block } -> strio - * strio.lines(limit) {|line| block } -> strio - * strio.lines(sep,limit) {|line| block } -> strio - * strio.lines(...) -> anEnumerator - * * See IO#each. */ static VALUE @@ -1094,6 +1116,18 @@ strio_each(int argc, VALUE *argv, VALUE self) } /* + * This is a deprecated alias for each_line. + */ +static VALUE +strio_lines(int argc, VALUE *argv, VALUE self) +{ + rb_warn("StringIO#lines is deprecated; use #each_line instead"); + if (!rb_block_given_p()) + return rb_enumeratorize(self, ID2SYM(rb_intern("each_line")), argc, argv); + return strio_each(argc, argv, self); +} + +/* * call-seq: * strio.readlines(sep=$/) -> array * strio.readlines(limit) -> array @@ -1463,13 +1497,13 @@ Init_stringio() rb_define_method(StringIO, "each", strio_each, -1); rb_define_method(StringIO, "each_line", strio_each, -1); - rb_define_method(StringIO, "lines", strio_each, -1); + rb_define_method(StringIO, "lines", strio_lines, -1); rb_define_method(StringIO, "each_byte", strio_each_byte, 0); - rb_define_method(StringIO, "bytes", strio_each_byte, 0); + rb_define_method(StringIO, "bytes", strio_bytes, 0); rb_define_method(StringIO, "each_char", strio_each_char, 0); - rb_define_method(StringIO, "chars", strio_each_char, 0); + rb_define_method(StringIO, "chars", strio_chars, 0); rb_define_method(StringIO, "each_codepoint", strio_each_codepoint, 0); - rb_define_method(StringIO, "codepoints", strio_each_codepoint, 0); + rb_define_method(StringIO, "codepoints", strio_codepoints, 0); rb_define_method(StringIO, "getc", strio_getc, 0); rb_define_method(StringIO, "ungetc", strio_ungetc, 1); rb_define_method(StringIO, "ungetbyte", strio_ungetbyte, 1); diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index 8c3d84d..8e19856 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -3880,6 +3880,20 @@ rb_gzreader_each_byte(VALUE obj) } /* + * Document-method: Zlib::GzipReader#bytes + * + * This is a deprecated alias for each_byte. + */ +static VALUE +rb_gzreader_bytes(VALUE obj) +{ + rb_warn("Zlib::GzipReader#bytes is deprecated; use #each_byte instead"); + if (!rb_block_given_p()) + return rb_enumeratorize(obj, ID2SYM(rb_intern("each_byte")), 0, 0); + return rb_gzreader_each_byte(obj); +} + +/* * Document-method: Zlib::GzipReader#ungetc * * See Zlib::GzipReader documentation for a description. @@ -4146,6 +4160,20 @@ rb_gzreader_each(int argc, VALUE *argv, VALUE obj) } /* + * Document-method: Zlib::GzipReader#lines + * + * This is a deprecated alias for each_line. + */ +static VALUE +rb_gzreader_lines(int argc, VALUE *argv, VALUE obj) +{ + rb_warn("Zlib::GzipReader#lines is deprecated; use #each_line instead"); + if (!rb_block_given_p()) + return rb_enumeratorize(obj, ID2SYM(rb_intern("each_line")), argc, argv); + return rb_gzreader_each_line(argc, argv, obj); +} + +/* * Document-method: Zlib::GzipReader#readlines * * See Zlib::GzipReader documentation for a description. @@ -4420,14 +4448,14 @@ Init_zlib() rb_define_method(cGzipReader, "readbyte", rb_gzreader_readbyte, 0); rb_define_method(cGzipReader, "each_byte", rb_gzreader_each_byte, 0); rb_define_method(cGzipReader, "each_char", rb_gzreader_each_char, 0); - rb_define_method(cGzipReader, "bytes", rb_gzreader_each_byte, 0); + rb_define_method(cGzipReader, "bytes", rb_gzreader_bytes, 0); rb_define_method(cGzipReader, "ungetc", rb_gzreader_ungetc, 1); rb_define_method(cGzipReader, "ungetbyte", rb_gzreader_ungetbyte, 1); rb_define_method(cGzipReader, "gets", rb_gzreader_gets, -1); rb_define_method(cGzipReader, "readline", rb_gzreader_readline, -1); rb_define_method(cGzipReader, "each", rb_gzreader_each, -1); rb_define_method(cGzipReader, "each_line", rb_gzreader_each, -1); - rb_define_method(cGzipReader, "lines", rb_gzreader_each, -1); + rb_define_method(cGzipReader, "lines", rb_gzreader_lines, -1); rb_define_method(cGzipReader, "readlines", rb_gzreader_readlines, -1); /* The OS code of current host */ diff --git a/io.c b/io.c index b89b87b..a29fdbf 100644 --- a/io.c +++ b/io.c @@ -3151,11 +3151,6 @@ rb_io_readlines(int argc, VALUE *argv, VALUE io) * ios.each_line(sep,limit) {|line| block } -> ios * ios.each_line(...) -> an_enumerator * - * ios.lines(sep=$/) {|line| block } -> ios - * ios.lines(limit) {|line| block } -> ios - * ios.lines(sep,limit) {|line| block } -> ios - * ios.lines(...) -> an_enumerator - * * Executes the block for every line in ios, where lines are * separated by sep. ios must be opened for * reading or an IOError will be raised. @@ -3190,10 +3185,20 @@ rb_io_each_line(int argc, VALUE *argv, VALUE io) } /* + * This is a deprecated alias for each_line. + */ + +static VALUE +rb_io_lines(int argc, VALUE *argv, VALUE io) +{ + rb_warn("IO#lines is deprecated; use #each_line instead"); + if (!rb_block_given_p()) + return rb_enumeratorize(io, ID2SYM(rb_intern("each_line")), argc, argv); + return rb_io_each_line(argc, argv, io); +} + +/* * call-seq: - * ios.bytes {|byte| block } -> ios - * ios.bytes -> an_enumerator - * * ios.each_byte {|byte| block } -> ios * ios.each_byte -> an_enumerator * @@ -3233,6 +3238,19 @@ rb_io_each_byte(VALUE io) return io; } +/* + * This is a deprecated alias for each_byte. + */ + +static VALUE +rb_io_bytes(VALUE io) +{ + rb_warn("IO#bytes is deprecated; use #each_byte instead"); + if (!rb_block_given_p()) + return rb_enumeratorize(io, ID2SYM(rb_intern("each_byte")), 0, 0); + return rb_io_each_byte(io); +} + static VALUE io_getc(rb_io_t *fptr, rb_encoding *enc) { @@ -3338,9 +3356,6 @@ io_getc(rb_io_t *fptr, rb_encoding *enc) /* * call-seq: - * ios.chars {|c| block } -> ios - * ios.chars -> an_enumerator - * * ios.each_char {|c| block } -> ios * ios.each_char -> an_enumerator * @@ -3373,6 +3388,19 @@ rb_io_each_char(VALUE io) return io; } +/* + * This is a deprecated alias for each_char. + */ + +static VALUE +rb_io_chars(VALUE io) +{ + rb_warn("IO#chars is deprecated; use #each_char instead"); + if (!rb_block_given_p()) + return rb_enumeratorize(io, ID2SYM(rb_intern("each_char")), 0, 0); + return rb_io_each_char(io); +} + /* * call-seq: @@ -3470,6 +3498,18 @@ rb_io_each_codepoint(VALUE io) return io; } +/* + * This is a deprecated alias for each_codepoint. + */ + +static VALUE +rb_io_codepoints(VALUE io) +{ + rb_warn("IO#codepoints is deprecated; use #each_codepoint instead"); + if (!rb_block_given_p()) + return rb_enumeratorize(io, ID2SYM(rb_intern("each_codepoint")), 0, 0); + return rb_io_each_codepoint(io); +} /* @@ -10758,10 +10798,6 @@ argf_readbyte(VALUE argf) * ARGF.each_line(sep=$/,limit) {|line| block } -> ARGF * ARGF.each_line(...) -> an_enumerator * - * ARGF.lines(sep=$/) {|line| block } -> ARGF - * ARGF.lines(sep=$/,limit) {|line| block } -> ARGF - * ARGF.lines(...) -> an_enumerator - * * Returns an enumerator which iterates over each line (separated by _sep_, * which defaults to your platform's newline character) of each file in * +ARGV+. If a block is supplied, each line in turn will be yielded to the @@ -10796,6 +10832,19 @@ argf_each_line(int argc, VALUE *argv, VALUE argf) } /* + * This is a deprecated alias for each_line. + */ + +static VALUE +argf_lines(int argc, VALUE *argv, VALUE argf) +{ + rb_warn("ARGF#lines is deprecated; use #each_line instead"); + if (!rb_block_given_p()) + return rb_enumeratorize(argf, ID2SYM(rb_intern("each_line")), argc, argv); + return argf_each_line(argc, argv, argf); +} + +/* * call-seq: * ARGF.bytes {|byte| block } -> ARGF * ARGF.bytes -> an_enumerator @@ -10831,10 +10880,20 @@ argf_each_byte(VALUE argf) } /* + * This is a deprecated alias for each_byte. + */ + +static VALUE +argf_bytes(VALUE argf) +{ + rb_warn("ARGF#bytes is deprecated; use #each_byte instead"); + if (!rb_block_given_p()) + return rb_enumeratorize(argf, ID2SYM(rb_intern("each_byte")), 0, 0); + return argf_each_byte(argf); +} + +/* * call-seq: - * ARGF.chars {|char| block } -> ARGF - * ARGF.chars -> an_enumerator - * * ARGF.each_char {|char| block } -> ARGF * ARGF.each_char -> an_enumerator * @@ -10861,10 +10920,20 @@ argf_each_char(VALUE argf) } /* + * This is a deprecated alias for each_char. + */ + +static VALUE +argf_chars(VALUE argf) +{ + rb_warn("ARGF#chars is deprecated; use #each_char instead"); + if (!rb_block_given_p()) + return rb_enumeratorize(argf, ID2SYM(rb_intern("each_char")), 0, 0); + return argf_each_char(argf); +} + +/* * call-seq: - * ARGF.codepoints {|codepoint| block } -> ARGF - * ARGF.codepoints -> an_enumerator - * * ARGF.each_codepoint {|codepoint| block } -> ARGF * ARGF.each_codepoint -> an_enumerator * @@ -10891,6 +10960,19 @@ argf_each_codepoint(VALUE argf) } /* + * This is a deprecated alias for each_codepoint. + */ + +static VALUE +argf_codepoints(VALUE argf) +{ + rb_warn("ARGF#codepoints is deprecated; use #each_codepoint instead"); + if (!rb_block_given_p()) + return rb_enumeratorize(argf, ID2SYM(rb_intern("each_codepoint")), 0, 0); + return argf_each_codepoint(argf); +} + +/* * call-seq: * ARGF.filename -> String * ARGF.path -> String @@ -11470,10 +11552,10 @@ Init_IO(void) rb_define_method(rb_cIO, "each_byte", rb_io_each_byte, 0); rb_define_method(rb_cIO, "each_char", rb_io_each_char, 0); rb_define_method(rb_cIO, "each_codepoint", rb_io_each_codepoint, 0); - rb_define_method(rb_cIO, "lines", rb_io_each_line, -1); - rb_define_method(rb_cIO, "bytes", rb_io_each_byte, 0); - rb_define_method(rb_cIO, "chars", rb_io_each_char, 0); - rb_define_method(rb_cIO, "codepoints", rb_io_each_codepoint, 0); + rb_define_method(rb_cIO, "lines", rb_io_lines, -1); + rb_define_method(rb_cIO, "bytes", rb_io_bytes, 0); + rb_define_method(rb_cIO, "chars", rb_io_chars, 0); + rb_define_method(rb_cIO, "codepoints", rb_io_codepoints, 0); rb_define_method(rb_cIO, "syswrite", rb_io_syswrite, 1); rb_define_method(rb_cIO, "sysread", rb_io_sysread, -1); @@ -11588,10 +11670,10 @@ Init_IO(void) rb_define_method(rb_cARGF, "each_byte", argf_each_byte, 0); rb_define_method(rb_cARGF, "each_char", argf_each_char, 0); rb_define_method(rb_cARGF, "each_codepoint", argf_each_codepoint, 0); - rb_define_method(rb_cARGF, "lines", argf_each_line, -1); - rb_define_method(rb_cARGF, "bytes", argf_each_byte, 0); - rb_define_method(rb_cARGF, "chars", argf_each_char, 0); - rb_define_method(rb_cARGF, "codepoints", argf_each_codepoint, 0); + rb_define_method(rb_cARGF, "lines", argf_lines, -1); + rb_define_method(rb_cARGF, "bytes", argf_bytes, 0); + rb_define_method(rb_cARGF, "chars", argf_chars, 0); + rb_define_method(rb_cARGF, "codepoints", argf_codepoints, 0); rb_define_method(rb_cARGF, "read", argf_read, -1); rb_define_method(rb_cARGF, "readpartial", argf_readpartial, -1); diff --git a/test/ruby/test_argf.rb b/test/ruby/test_argf.rb index 7fb9348..8243a6e 100644 --- a/test/ruby/test_argf.rb +++ b/test/ruby/test_argf.rb @@ -771,26 +771,54 @@ class TestArgf < Test::Unit::TestCase assert_ruby_status(["-e", "2.times {STDIN.tty?; readlines}"], "", bug5952) end + def test_lines + ruby('-W1', '-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| + $stderr = $stdout + s = [] + ARGF.lines {|l| s << l } + p s + SRC + assert_match(/deprecated/, f.gets) + assert_equal("[\"1\\n\", \"2\\n\", \"3\\n\", \"4\\n\", \"5\\n\", \"6\\n\"]\n", f.read) + end + end + + def test_lines + ruby('-W1', '-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| + $stderr = $stdout + print Marshal.dump(ARGF.bytes.to_a) + SRC + assert_match(/deprecated/, f.gets) + assert_equal([49, 10, 50, 10, 51, 10, 52, 10, 53, 10, 54, 10], Marshal.load(f.read)) + end + end + def test_bytes - ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| + ruby('-W1', '-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| + $stderr = $stdout print Marshal.dump(ARGF.bytes.to_a) SRC + assert_match(/deprecated/, f.gets) assert_equal([49, 10, 50, 10, 51, 10, 52, 10, 53, 10, 54, 10], Marshal.load(f.read)) end end def test_chars - ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| + ruby('-W1', '-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| + $stderr = $stdout print [Marshal.dump(ARGF.chars.to_a)].pack('m') SRC + assert_match(/deprecated/, f.gets) assert_equal(["1", "\n", "2", "\n", "3", "\n", "4", "\n", "5", "\n", "6", "\n"], Marshal.load(f.read.unpack('m').first)) end end def test_codepoints - ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| + ruby('-W1', '-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| + $stderr = $stdout print Marshal.dump(ARGF.codepoints.to_a) SRC + assert_match(/deprecated/, f.gets) assert_equal([49, 10, 50, 10, 51, 10, 52, 10, 53, 10, 54, 10], Marshal.load(f.read)) end end diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb index d092484..a6ab3ac 100644 --- a/test/ruby/test_io.rb +++ b/test/ruby/test_io.rb @@ -260,6 +260,19 @@ class TestIO < Test::Unit::TestCase } end + def test_codepoints + make_tempfile {|t| + bug2959 = '[ruby-core:28650]' + a = "" + File.open(t, 'rt') {|f| + assert_warn(/deprecated/) { + f.codepoints {|c| a << c} + } + } + assert_equal("foo\nbar\nbaz\n", a, bug2959) + } + end + def test_rubydev33072 t = make_tempfile path = t.path @@ -1303,21 +1316,28 @@ class TestIO < Test::Unit::TestCase end def test_lines + verbose, $VERBOSE = $VERBOSE, nil pipe(proc do |w| w.puts "foo" w.puts "bar" w.puts "baz" w.close end, proc do |r| - e = r.lines + e = nil + assert_warn(/deprecated/) { + e = r.lines + } assert_equal("foo\n", e.next) assert_equal("bar\n", e.next) assert_equal("baz\n", e.next) assert_raise(StopIteration) { e.next } end) + ensure + $VERBOSE = verbose end def test_bytes + verbose, $VERBOSE = $VERBOSE, nil pipe(proc do |w| w.binmode w.puts "foo" @@ -1325,27 +1345,38 @@ class TestIO < Test::Unit::TestCase w.puts "baz" w.close end, proc do |r| - e = r.bytes + e = nil + assert_warn(/deprecated/) { + e = r.bytes + } (%w(f o o) + ["\n"] + %w(b a r) + ["\n"] + %w(b a z) + ["\n"]).each do |c| assert_equal(c.ord, e.next) end assert_raise(StopIteration) { e.next } end) + ensure + $VERBOSE = verbose end def test_chars + verbose, $VERBOSE = $VERBOSE, nil pipe(proc do |w| w.puts "foo" w.puts "bar" w.puts "baz" w.close end, proc do |r| - e = r.chars + e = nil + assert_warn(/deprecated/) { + e = r.chars + } (%w(f o o) + ["\n"] + %w(b a r) + ["\n"] + %w(b a z) + ["\n"]).each do |c| assert_equal(c, e.next) end assert_raise(StopIteration) { e.next } end) + ensure + $VERBOSE = verbose end def test_readbyte -- 1.8.0