[DRE-commits] [ruby-gettext] 01/02: Restore compatibility with Ruby 1.8

Jérémy Bobbio lunar at alioth.debian.org
Mon Sep 2 08:05:22 UTC 2013


This is an automated email from the git hooks/post-receive script.

lunar pushed a commit to branch master
in repository ruby-gettext.

commit a6ad024b755edc124101c73949671684771d5451
Author: Jérémy Bobbio <lunar at debian.org>
Date:   Mon Sep 2 09:48:45 2013 +0200

    Restore compatibility with Ruby 1.8
    
    Closes: #721574
---
 debian/control                                     |    4 +-
 .../0005-Restore-compatibility-with-Ruby-1.8.patch |  572 ++++++++++++++++++++
 debian/patches/series                              |    1 +
 3 files changed, 575 insertions(+), 2 deletions(-)

diff --git a/debian/control b/debian/control
index 0024c9d..f2e5674 100644
--- a/debian/control
+++ b/debian/control
@@ -16,12 +16,12 @@ Standards-Version: 3.9.4
 Vcs-Git: git://anonscm.debian.org/pkg-ruby-extras/ruby-gettext.git
 Vcs-Browser: http://anonscm.debian.org/gitweb/?p=pkg-ruby-extras/ruby-gettext.git;a=summary
 Homepage: http://ruby-gettext.github.io/
-XS-Ruby-Versions: ruby1.9.1
+XS-Ruby-Versions: all
 
 Package: ruby-gettext
 Architecture: all
 XB-Ruby-Versions: ${ruby:Versions}
-Depends: ruby1.9.1,
+Depends: ruby | ruby-interpreter,
          ruby-locale (>= 2.0.5),
          ruby-text,
          ${misc:Depends},
diff --git a/debian/patches/0005-Restore-compatibility-with-Ruby-1.8.patch b/debian/patches/0005-Restore-compatibility-with-Ruby-1.8.patch
new file mode 100644
index 0000000..d99f617
--- /dev/null
+++ b/debian/patches/0005-Restore-compatibility-with-Ruby-1.8.patch
@@ -0,0 +1,572 @@
+From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Bobbio?= <lunar at debian.org>
+Date: Mon, 2 Sep 2013 09:48:15 +0200
+Subject: Restore compatibility with Ruby 1.8
+
+---
+ lib/gettext/core_ext/iconv.rb    |  110 ++++++++++++++++++++++++++++++++++++++
+ lib/gettext/core_ext/string.rb   |   91 +++++++++++++++++++++++++++++++
+ lib/gettext/locale_path.rb       |    1 +
+ lib/gettext/mo.rb                |   54 +++++++++++++------
+ lib/gettext/tools/parser/erb.rb  |   16 +++---
+ lib/gettext/tools/parser/ruby.rb |    7 ++-
+ lib/gettext/tools/xgettext.rb    |    6 ++-
+ test/gettext-test-utils.rb       |   23 ++++++++
+ test/test_parser.rb              |    2 +
+ test/test_po_parser.rb           |    4 ++
+ test/tools/parser/test_ruby.rb   |    2 +
+ test/tools/test_xgettext.rb      |   24 +++++----
+ 12 files changed, 304 insertions(+), 36 deletions(-)
+ create mode 100644 lib/gettext/core_ext/iconv.rb
+ create mode 100644 lib/gettext/core_ext/string.rb
+
+diff --git a/lib/gettext/core_ext/iconv.rb b/lib/gettext/core_ext/iconv.rb
+new file mode 100644
+index 0000000..389a2c7
+--- /dev/null
++++ b/lib/gettext/core_ext/iconv.rb
+@@ -0,0 +1,110 @@
++# encoding: utf-8
++=begin
++  iconv.rb - Pseudo Iconv class. Supports Iconv.iconv, Iconv.conv.
++
++  For Matz Ruby:
++  If you don't have iconv but glib2, this library uses glib2 iconv functions.
++
++  For JRuby:
++  Use Java String class to convert strings.
++
++  Copyright (C) 2004-2009  Masao Mutoh
++
++  You may redistribute it and/or modify it under the same
++  license terms as Ruby or LGPL.
++=end
++
++begin
++  require 'iconv.so'
++rescue LoadError
++  # Pseudo Iconv class
++  #
++  # ==== For Matz Ruby:
++  # If you don't have iconv but Ruby/GLib2, this library uses Ruby/GLib2's
++  # iconv functions.
++  #
++  # Ruby/GLib is a module which is provided from Ruby-GNOME2 Project.
++  # You can get binaries for Win32(One-Click Ruby Installer).
++  # <URL: http://ruby-gnome2.sourceforge.jp/>
++  # ==== For JRuby:
++  # Use Java String class to convert strings.
++  class Iconv
++    module Failure; end
++    class InvalidEncoding < ArgumentError;  include Failure; end
++    class IllegalSequence < ArgumentError;  include Failure; end
++    class InvalidCharacter < ArgumentError; include Failure; end
++
++    if RUBY_PLATFORM =~ /java/
++      def self.conv(to, from, str)
++        raise InvalidCharacter, "the 3rd argument is nil" unless str
++        begin
++          str = java.lang.String.new(str.unpack("C*").to_java(:byte), from)
++          str.getBytes(to).to_ary.pack("C*")
++        rescue java.io.UnsupportedEncodingException
++          raise InvalidEncoding
++        end
++      end
++    else
++      begin
++        require 'glib2'
++
++        def self.check_glib_version?(major, minor, micro) # :nodoc:
++          (GLib::BINDING_VERSION[0] > major ||
++           (GLib::BINDING_VERSION[0] == major &&
++            GLib::BINDING_VERSION[1] > minor) ||
++           (GLib::BINDING_VERSION[0] == major &&
++            GLib::BINDING_VERSION[1] == minor &&
++            GLib::BINDING_VERSION[2] >= micro))
++        end
++
++        if check_glib_version?(0, 11, 0)
++          # This is a function equivalent of Iconv.iconv.
++          # * to: encoding name for destination
++          # * from: encoding name for source
++          # * str: strings to be converted
++          # * Returns: Returns an Array of converted strings.
++          def self.conv(to, from, str)
++            begin
++              GLib.convert(str, to, from)
++            rescue GLib::ConvertError => e
++              case e.code
++              when GLib::ConvertError::NO_CONVERSION
++                raise InvalidEncoding.new(str)
++              when GLib::ConvertError::ILLEGAL_SEQUENCE
++                raise IllegalSequence.new(str)
++              else
++                raise InvalidCharacter.new(str)
++              end
++            end
++          end
++        else
++          def self.conv(to, from, str) # :nodoc:
++            begin
++              GLib.convert(str, to, from)
++            rescue
++              raise IllegalSequence.new(str)
++            end
++          end
++        end
++      rescue LoadError
++        def self.conv(to, from, str) # :nodoc:
++          warn "Iconv was not found." if $DEBUG
++          str
++        end
++      end
++    end
++    def self.iconv(to, from, str)
++      conv(to, from, str).split(//)
++    end
++  end
++end
++
++if __FILE__ == $0
++  puts Iconv.iconv("EUC-JP", "UTF-8", "ほげ").join
++  begin
++    puts Iconv.iconv("EUC-JP", "EUC-JP", "ほげ").join
++  rescue Iconv::Failure
++    puts $!
++    puts $!.class
++  end
++end
+diff --git a/lib/gettext/core_ext/string.rb b/lib/gettext/core_ext/string.rb
+new file mode 100644
+index 0000000..900bea0
+--- /dev/null
++++ b/lib/gettext/core_ext/string.rb
+@@ -0,0 +1,91 @@
++# encoding: utf-8
++
++=begin
++  string.rb - Extension for String.
++
++  Copyright (C) 2005-2009 Masao Mutoh
++
++  You may redistribute it and/or modify it under the same
++  license terms as Ruby or LGPL.
++=end
++
++# Extension for String class. This feature is included in Ruby 1.9 or later but not occur TypeError.
++#
++# String#% method which accept "named argument". The translator can know
++# the meaning of the msgids using "named argument" instead of %s/%d style.
++class String
++
++  unless instance_methods.find {|m| m.to_s == 'bytesize'}
++    # For older ruby (such as ruby-1.8.5)
++    alias :bytesize :size
++  end
++
++  begin
++    formatted = "%{key}" % {:key => "value"}
++    raise ArgumentError if formatted != "value"
++  rescue ArgumentError
++    alias :_old_format_m :% # :nodoc:
++
++    PERCENT_MATCH_RE = Regexp.union(
++        /%%/,
++        /%\{(.+?)\}/,
++        /%<(.+?)>(.*?\d*\.?\d*[bBdiouxXeEfgGcps])/
++    )
++
++    # call-seq:
++    #  %(arg)
++    #  %(hash)
++    #
++    # Format - Uses str as a format specification, and returns the result of applying it to arg.
++    # If the format specification contains more than one substitution, then arg must be
++    # an Array containing the values to be substituted. See Kernel::sprintf for details of the
++    # format string. This is the default behavior of the String class.
++    # * arg: an Array or other class except Hash.
++    # * Returns: formatted String
++    #
++    #  (e.g.) "%s, %s" % ["Masao", "Mutoh"]
++    #
++    # Also you can use a Hash as the "named argument". This is recommanded way for Ruby-GetText
++    # because the translators can understand the meanings of the msgids easily.
++    # * hash: {:key1 => value1, :key2 => value2, ... }
++    # * Returns: formatted String
++    #
++    #  (e.g.)
++    #         For strings.
++    #         "%{firstname}, %{familyname}" % {:firstname => "Masao", :familyname => "Mutoh"}
++    #
++    #         With field type to specify format such as d(decimal), f(float),...
++    #         "%<age>d, %<weight>.1f" % {:age => 10, :weight => 43.4}
++    def %(args)
++      if args.kind_of?(Hash)
++        ret = dup
++        ret.gsub!(PERCENT_MATCH_RE) {|match|
++          if match == '%%'
++            '%'
++          elsif $1
++            key = $1.to_sym
++            args.has_key?(key) ? args[key] : match
++          elsif $2
++            key = $2.to_sym
++            args.has_key?(key) ? sprintf("%#{$3}", args[key]) : match
++          end
++        }
++        ret
++      else
++        ret = gsub(/%([{<])/, '%%\1')
++        begin
++          ret._old_format_m(args)
++        rescue ArgumentError => e
++          if $DEBUG
++            $stderr.puts "  The string:#{ret}"
++            $stderr.puts "  args:#{args.inspect}"
++            puts e.backtrace
++          else
++            raise ArgumentError, e.message
++          end
++        end
++      end
++    end
++  end
++end
++
+diff --git a/lib/gettext/locale_path.rb b/lib/gettext/locale_path.rb
+index a0445c3..0b89581 100644
+--- a/lib/gettext/locale_path.rb
++++ b/lib/gettext/locale_path.rb
+@@ -11,6 +11,7 @@
+ =end
+ 
+ require 'rbconfig'
++require 'gettext/core_ext/string'
+ 
+ module GetText
+   # Treats locale-path for mo-files.
+diff --git a/lib/gettext/mo.rb b/lib/gettext/mo.rb
+index 56549dd..5cd7953 100644
+--- a/lib/gettext/mo.rb
++++ b/lib/gettext/mo.rb
+@@ -43,8 +43,12 @@ module GetText
+       :trans_sysdep_tab_offset
+     end
+ 
+-    MAGIC_BIG_ENDIAN    = "\x95\x04\x12\xde".force_encoding("ASCII-8BIT")
+-    MAGIC_LITTLE_ENDIAN = "\xde\x12\x04\x95".force_encoding("ASCII-8BIT")
++    MAGIC_BIG_ENDIAN    = "\x95\x04\x12\xde"
++    MAGIC_LITTLE_ENDIAN = "\xde\x12\x04\x95"
++    if "".respond_to?(:force_encoding)
++      MAGIC_BIG_ENDIAN.force_encoding("ASCII-8BIT")
++      MAGIC_LITTLE_ENDIAN.force_encoding("ASCII-8BIT")
++    end
+ 
+     def self.open(arg = nil, output_charset = nil)
+       result = self.new(output_charset)
+@@ -308,24 +312,40 @@ module GetText
+     attr_reader :charset, :nplurals, :plural
+ 
+     private
+-    def convert_encoding(string, original_string)
+-      return string if @output_charset.nil? or @charset.nil?
++    if "".respond_to?(:encode)
++      def convert_encoding(string, original_string)
++        return string if @output_charset.nil? or @charset.nil?
+ 
+-      if Encoding.find(@output_charset) == Encoding.find(@charset)
+-        string.force_encoding(@output_charset)
+-        return string
+-      end
++        if Encoding.find(@output_charset) == Encoding.find(@charset)
++          string.force_encoding(@output_charset)
++          return string
++        end
+ 
+-      begin
+-        string.encode(@output_charset, @charset)
+-      rescue EncodingError
+-        if $DEBUG
+-          warn "@charset = ", @charset
+-          warn "@output_charset = ", @output_charset
+-          warn "msgid = ", original_string
+-          warn "msgstr = ", string
++        begin
++          string.encode(@output_charset, @charset)
++        rescue EncodingError
++          if $DEBUG
++            warn "@charset = ", @charset
++            warn "@output_charset = ", @output_charset
++            warn "msgid = ", original_string
++            warn "msgstr = ", string
++          end
++          string
++        end
++      end
++    else
++      require 'gettext/core_ext/iconv'
++      def convert_encoding(string, original_string)
++        begin
++          Iconv.conv(@output_charset, @charset, string)
++        rescue Iconv::Failure
++          if $DEBUG
++            warn "@charset = ", @charset
++            warn "@output_charset = ", @output_charset
++            warn "msgid = ", original_string
++            warn "msgstr = ", str
++          end
+         end
+-        string
+       end
+     end
+ 
+diff --git a/lib/gettext/tools/parser/erb.rb b/lib/gettext/tools/parser/erb.rb
+index dc2ad1c..dee952f 100644
+--- a/lib/gettext/tools/parser/erb.rb
++++ b/lib/gettext/tools/parser/erb.rb
+@@ -64,13 +64,15 @@ module GetText
+       content = IO.read(@path)
+       src = ERB.new(content).src
+ 
+-      # Force the src encoding back to the encoding in magic comment
+-      # or original content.
+-      encoding = detect_encoding(src) || content.encoding
+-      src.force_encoding(encoding)
+-
+-      # Remove magic comment prepended by erb in Ruby 1.9.
+-      src = src.gsub(MAGIC_COMMENT, "")
++      if src.respond_to?(:encode)
++        # Force the src encoding back to the encoding in magic comment
++        # or original content.
++        encoding = detect_encoding(src) || content.encoding
++        src.force_encoding(encoding)
++
++        # Remove magic comment prepended by erb in Ruby 1.9.
++        src = src.gsub(MAGIC_COMMENT, "")
++      end
+ 
+       RubyParser.new(@path, @options).parse_source(src)
+     end
+diff --git a/lib/gettext/tools/parser/ruby.rb b/lib/gettext/tools/parser/ruby.rb
+index 2886b77..671d2c1 100644
+--- a/lib/gettext/tools/parser/ruby.rb
++++ b/lib/gettext/tools/parser/ruby.rb
+@@ -211,13 +211,16 @@ module GetText
+     def parse
+       source = IO.read(@path)
+ 
+-      encoding = detect_encoding(source) || source.encoding
+-      source.force_encoding(encoding)
++      if source.respond_to?(:encode)
++        encoding = detect_encoding(source) || source.encoding
++        source.force_encoding(encoding)
++      end
+ 
+       parse_source(source)
+     end
+ 
+     def detect_encoding(source)
++      return nil unless source.respond_to?(:force_encoding)
+       binary_source = source.dup.force_encoding("ASCII-8BIT")
+       if /\A.*coding\s*[=:]\s*([[:alnum:]\-_]+)/ =~ binary_source
+         $1.gsub(/-(?:unix|mac|dos)\z/, "")
+diff --git a/lib/gettext/tools/xgettext.rb b/lib/gettext/tools/xgettext.rb
+index e1098a5..0d36f60 100644
+--- a/lib/gettext/tools/xgettext.rb
++++ b/lib/gettext/tools/xgettext.rb
+@@ -366,7 +366,11 @@ EOH
+       end
+ 
+       def encode(string)
+-        string.encode(@output_encoding)
++        if string.respond_to?(:encode)
++          string.encode(@output_encoding)
++        else
++          string # don't support
++        end
+       end
+     end
+   end
+diff --git a/test/gettext-test-utils.rb b/test/gettext-test-utils.rb
+index 4dfab44..d973c06 100644
+--- a/test/gettext-test-utils.rb
++++ b/test/gettext-test-utils.rb
+@@ -22,6 +22,10 @@ require "tmpdir"
+ require "tempfile"
+ require "time"
+ 
++unless String.method_defined?(:encode)
++  require "iconv"
++end
++
+ require "gettext"
+ 
+ module GetTextTestUtils
+@@ -41,4 +45,23 @@ module GetTextTestUtils
+   def teardown_tmpdir
+     FileUtils.rm_rf(@tmpdir, :secure => true) if @tmpdir
+   end
++
++  def need_encoding
++    unless defined?(Encoding)
++      omit("This test needs encoding.")
++    end
++  end
++
++  def set_encoding(string, encoding)
++    return unless string.respond_to?(:force_encoding)
++    string.force_encoding(encoding)
++  end
++
++  def encode(string, encoding)
++    if string.respond_to?(:encode)
++      string.encode(encoding)
++    else
++      Iconv.iconv(encoding, "UTF-8", string).join("")
++    end
++  end
+ end
+diff --git a/test/test_parser.rb b/test/test_parser.rb
+index 9bbc80c..68cb9aa 100644
+--- a/test/test_parser.rb
++++ b/test/test_parser.rb
+@@ -133,6 +133,8 @@ class TestGetTextParser < Test::Unit::TestCase
+     include GetTextTestUtils
+ 
+     def test_detect_encoding
++      need_encoding
++
+       euc_file = Tempfile.new("euc-jp.rhtml")
+       euc_file.open
+       euc_file.puts("<%#-*- coding: euc-jp -*-%>")
+diff --git a/test/test_po_parser.rb b/test/test_po_parser.rb
+index 88329dd..ea73992 100644
+--- a/test/test_po_parser.rb
++++ b/test/test_po_parser.rb
+@@ -306,6 +306,8 @@ EOP
+     end
+   end
+ 
++  if defined?(Encoding)
++
+   class TestHeader < self
+     class TestEncoding < self
+       def test_known
+@@ -337,4 +339,6 @@ msgstr ""
+       end
+     end
+   end
++
++  end
+ end
+diff --git a/test/tools/parser/test_ruby.rb b/test/tools/parser/test_ruby.rb
+index 1d037f1..7459838 100644
+--- a/test/tools/parser/test_ruby.rb
++++ b/test/tools/parser/test_ruby.rb
+@@ -68,6 +68,8 @@ class TestRubyParser < Test::Unit::TestCase
+   end
+ 
+   class TestDetectEncoding < self
++    setup :need_encoding
++
+     def test_ascii_and_hyphen
+       assert_equal("euc-jp", detect_encoding("# coding: euc-jp"))
+     end
+diff --git a/test/tools/test_xgettext.rb b/test/tools/test_xgettext.rb
+index 9837959..8d5e5ff 100644
+--- a/test/tools/test_xgettext.rb
++++ b/test/tools/test_xgettext.rb
+@@ -119,6 +119,8 @@ EOP
+ 
+   class TestEncoding < self
+     def test_different_encoding_from_current_locale
++      need_encoding
++
+       rhtml = <<-EOR
+ <%#-*- coding: sjis -*-%>
+ <html>
+@@ -131,34 +133,36 @@ EOP
+ </html>
+ EOR
+       File.open(@rhtml_file_path, "w") do |rhtml_file|
+-        rhtml_file.puts(rhtml.encode("sjis"))
++        rhtml_file.puts(encode(rhtml, "sjis"))
+       end
+ 
+       @xgettext.run("--output", @pot_file_path, @rhtml_file_path)
+ 
+       encoding = "UTF-8"
+       pot_content = File.read(@pot_file_path)
+-      pot_content.force_encoding(encoding)
++      set_encoding(pot_content, encoding)
+       expected_content = <<-EOP
+ #{header}
+ #: ../templates/xgettext.rhtml:7
+ msgid "わたし"
+ msgstr ""
+ EOP
+-      expected_content = expected_content.encode(encoding)
++      expected_content = encode(expected_content, encoding)
+       assert_equal(expected_content, pot_content)
+     end
+ 
+     def test_multiple_encodings
++      need_encoding
++
+       File.open(@rb_file_path, "w") do |rb_file|
+-        rb_file.puts(<<-EOR.encode("euc-jp"))
++        rb_file.puts(encode(<<-EOR, "euc-jp"))
+ # -*- coding: euc-jp -*-
+ _("こんにちは")
+ EOR
+       end
+ 
+       File.open(@rhtml_file_path, "w") do |rhtml_file|
+-        rhtml_file.puts(<<-EOR.encode("cp932"))
++        rhtml_file.puts(encode(<<-EOR, "cp932"))
+ <%# -*- coding: cp932 -*-%>
+ <h1><%= _("わたし") %></h1>
+ EOR
+@@ -168,7 +172,7 @@ EOR
+ 
+       encoding = "UTF-8"
+       pot_content = File.read(@pot_file_path)
+-      pot_content.force_encoding(encoding)
++      set_encoding(pot_content, encoding)
+       expected_content = <<-EOP
+ #{header}
+ #: ../lib/xgettext.rb:2
+@@ -179,7 +183,7 @@ msgstr ""
+ msgid "わたし"
+ msgstr ""
+ EOP
+-      expected_content = expected_content.encode(encoding)
++      expected_content = encode(expected_content, encoding)
+       assert_equal(expected_content, pot_content)
+     end
+   end
+@@ -346,13 +350,15 @@ msgstr ""
+     end
+ 
+     def test_to_code
++      need_encoding
++
+       output_encoding = "EUC-JP"
+       pot_content = generate(<<-EOR, "--output-encoding", output_encoding)
+ # -*- coding: utf-8 -*-
+ 
+ _("わたし")
+ EOR
+-      pot_content.force_encoding(output_encoding)
++      set_encoding(pot_content, output_encoding)
+ 
+       options = {:to_code => output_encoding}
+       expected_pot = <<-EOP
+@@ -361,7 +367,7 @@ EOR
+ msgid "わたし"
+ msgstr ""
+ EOP
+-      expected_pot = expected_pot.encode(output_encoding)
++      expected_pot = encode(expected_pot, output_encoding)
+ 
+       assert_equal(expected_pot, pot_content)
+     end
diff --git a/debian/patches/series b/debian/patches/series
index 5674133..5353464 100644
--- a/debian/patches/series
+++ b/debian/patches/series
@@ -2,3 +2,4 @@
 0002-Make-test-unit-notify-and-test-unit-rr-optional-to-r.patch
 0003-Do-not-use-Bundler-in-Rakefile.patch
 0004-Use-system-racc-in-Rakefile.patch
+0005-Restore-compatibility-with-Ruby-1.8.patch

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-ruby-extras/ruby-gettext.git



More information about the Pkg-ruby-extras-commits mailing list