Irb don't start up when Encoding.defalut_internal isn't 'utf-8'

Issue #4281 has been updated by Hiroshi NAKAMURA.

Target version changed from 1.9.2 to 1.9.3


Bug #4281: irb don’t start up when Encoding.defalut_internal isn’t
‘utf-8’
http://redmine.ruby-lang.org/issues/4281

Author: Narihiro N.
Status: Assigned
Priority: Normal
Assignee: Keiju Ishitsuka
Category: lib
Target version: 1.9.3
ruby -v: ruby 1.9.2p136 (2010-12-25 revision 30365) [i686-linux]

=begin
nariです。

以下のようにすると irb が起動しません。
$ ruby -S -E cp932:cp932 irb
/home/nari/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/irb/locale.rb:135:in
eval': /home/nari/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/irb/lc/ja/error.rb:16: invalid multibyte char (UTF-8) (SyntaxError) /home/nari/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/irb/lc/ja/error.rb:16: invalid multibyte char (UTF-8) /home/nari/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/irb/lc/ja/error.rb:16: syntax error, unexpected $end, expecting keyword_end def_exception :UnrecognizedSwitch, 'スイッチ(%s)が分りません' ^ from /home/nari/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/irb/locale.rb:135:inreal_load’
from
/home/nari/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/irb/locale.rb:125:in
block in load' from /home/nari/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/irb/locale.rb:123:ineach’
from
/home/nari/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/irb/locale.rb:123:in
load' from /home/nari/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/irb/init.rb:122:ininit_error’
from
/home/nari/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/irb/init.rb:17:in
setup' from /home/nari/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/irb.rb:53:instart’
from /home/nari/.rvm/rubies/ruby-1.9.2-p136/bin/irb:16:in
`’

再現コードは以下です。
$ cat /tmp/t.rb
require “irb”
Encoding.default_internal = “cp932”
Encoding.default_external = “cp932”

pathはRubyのインストール先に合わせて変えてください

path =
“/home/nari/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/irb/lc/ja/error.rb”
src = IRB::MagicFile.open(path){|f| f.read}
p src
eval(src)

$ ruby /tmp/t.rb
#Encoding:Windows-31J
/tmp/t.rb:8:in eval': (eval):16: invalid multibyte char (UTF-8) (SyntaxError) (eval):16: invalid multibyte char (UTF-8) (eval):16: syntax error, unexpected $end, expecting keyword_end def_exception :UnrecognizedSwitch, 'スイッチ(%s)が分りません' ^ from /tmp/t.rb:8:in

cp932に変換された文字列(src)の中に、utf-8を指定するマジックコメントが入っ
ており、utf-8の文字列としてeval()されるのが原因のようです。

Encoding.default_internalが指定されるようなケースまで標準ライブラリが対
応する必要はないのかもしれませんが、情報共有の意味も込めて報告しておき
ます。

この問題については以下のパッチで直ることを確認しています。

diff --git a/lib/irb/magic-file.rb b/lib/irb/magic-file.rb
index 8612620…339ed60 100644
— a/lib/irb/magic-file.rb
+++ b/lib/irb/magic-file.rb
@@ -8,9 +8,10 @@ module IRB
line = io.gets
line = io.gets if line[0,2] == “#!”
encoding = detect_encoding(line)

  •  internal_encoding = encoding
     encoding ||= default_src_encoding
     io.rewind
    
  •  io.set_encoding(encoding, nil)
    
  •  io.set_encoding(encoding, internal_encoding)
    
     if block_given?
       begin
    

=end

$B$1$$$8$e!w$$$7$D$+$G$9(B.

mame (Yusuke E.) [email protected] wrote:

Issue #4281 has been updated by mame (Yusuke E.).

Yugui $B$5$s$+$iH?1~$J$$$_$?$$$J$s$G!"$$$7$D$+$5$s$NJ}$GBP1~$7$FD:$1$^(B
$B$9$G$7$g$&$+!#$H$j$"$($:(B trunk $B$G$*4j$$$7$^$9!#(B

$BN;2r(B.
$B$J$s$+(B, $B:G6a%A%1%C%H$,0lGU$?$^$C$F$-$F$$$k$N$G(B,
$B4hD%$C$F>C2=$7$F$$$-$^(B
$B$9(B.

__
---------------------------------------------------->> [email protected](B $B7=<y(B
<<—
---------------------------------->> e-mail: [email protected] <<—

$B$1$$$8$e!w$$$7$D$+$G$9(B.

mame (Yusuke E.) [email protected] wrote:

Yugui $B$5$s$+$i$b$$$7$D$+$5$s$+$i$bH?1~$,$J$$!#(B

$B$3$A$i$GBP1~$7$^$9$H2sEz$7$?3P$($,$"$j$^$9$,(B,
$B$=$N$^$^$K$J$C$F$$$^$7$?(B.
$B?=$7Lu$J$$(B.

$B$$$m$$$m$HN/$C$F$$$k$N$G(B,
$B$=$m$=$m$d$i$J$/$A$c$$$1$J$$$J$H;W$C$F$$$k$H(B
$B$3$m$G$9(B.

$B$H$3$m$G(B,
$B%j%j!<%9$K;j$k$^$G$N%9%1%8%e!<%k$,$h$/J,$+$C$F$$$J$$$N$G$9$,(B,
$B$$$D$^$G$KBP1~$7$J$/$F$O$J$i$J$$$G$7$g$&(B?

__
---------------------------------------------------->> [email protected](B $B7=<y(B
<<—
---------------------------------->> e-mail: [email protected] <<—

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs