From c47c095b9740e7c19d6fdca29ab661c1089221d4 Mon Sep 17 00:00:00 2001 From: knu Date: Sat, 22 Dec 2012 17:22:04 +0000 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] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38563 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 13 ++++ NEWS | 28 ++++++++ ext/stringio/stringio.c | 70 ++++++++++++++------ ext/zlib/zlib.c | 32 ++++++++- io.c | 140 +++++++++++++++++++++++++++++++--------- test/ruby/test_argf.rb | 36 +++++++++-- test/ruby/test_io.rb | 37 ++++++++++- 7 files changed, 300 insertions(+), 56 deletions(-) diff --git a/ChangeLog b/ChangeLog index 53d9a7324b..a2e2cfcc3e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +Sun Dec 23 01:52:01 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] + Sat Dec 23 01:35:00 2012 Zachary Scott * lib/optparse.rb: Documentation for OptionParser to remove 'shadowed diff --git a/NEWS b/NEWS index 9608b8b116..d5b7a0ec6b 100644 --- a/NEWS +++ b/NEWS @@ -67,6 +67,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(). @@ -335,6 +339,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 @@ -358,6 +366,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 @@ -383,6 +393,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 975a30855f..d763854faf 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 * @@ -652,6 +649,18 @@ strio_each_byte(VALUE self) return 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 * @@ -861,11 +867,20 @@ strio_each_char(VALUE self) return 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 @@ -1093,6 +1115,18 @@ strio_each(int argc, VALUE *argv, VALUE self) return 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 @@ -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 6b6299c0d0..d962c0a293 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -3879,6 +3879,20 @@ rb_gzreader_each_byte(VALUE obj) return Qnil; } +/* + * 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 * @@ -4145,6 +4159,20 @@ rb_gzreader_each(int argc, VALUE *argv, VALUE obj) return 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 * @@ -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 9d124c00e9..53539da7b5 100644 --- a/io.c +++ b/io.c @@ -3216,11 +3216,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. @@ -3254,11 +3249,21 @@ rb_io_each_line(int argc, VALUE *argv, VALUE io) return 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 * @@ -3298,6 +3303,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) { @@ -3403,9 +3421,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 * @@ -3438,6 +3453,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: @@ -3535,6 +3563,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); +} /* @@ -10844,10 +10884,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 @@ -10881,6 +10917,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 @@ -10916,11 +10965,21 @@ 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 * @@ -10946,11 +11005,21 @@ 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 * @@ -10976,6 +11045,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 @@ -11556,10 +11638,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); @@ -11674,10 +11756,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 7fb9348c1e..8243a6ed86 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_bytes - ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| + 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('-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 d509a9bfe2..0b3b5591e3 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