diff options
author | aycabta <aycabta@gmail.com> | 2021-05-09 12:12:42 +0900 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2021-05-11 09:32:08 +0900 |
commit | 774cc32b4d4962ef685fb852a2dbf1897f64a8f7 (patch) | |
tree | e350e6944f2bfe815b70c26ad96c8ea58c2d014c /lib/irb.rb | |
parent | 4785d6087aa77a0ee28881ff9fb0737e8d5a97c5 (diff) | |
download | ruby-774cc32b4d4962ef685fb852a2dbf1897f64a8f7.tar.gz |
[ruby/irb] Treat encodings in exception correctly
https://github.com/ruby/irb/commit/4452adbe04
Diffstat (limited to 'lib/irb.rb')
-rw-r--r-- | lib/irb.rb | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/lib/irb.rb b/lib/irb.rb index 93c4d25c92..4b06f897da 100644 --- a/lib/irb.rb +++ b/lib/irb.rb @@ -590,9 +590,15 @@ module IRB end end - def convert_invalid_byte_sequence(str) - str = str.force_encoding(Encoding::ASCII_8BIT) - conv = Encoding::Converter.new(Encoding::ASCII_8BIT, Encoding::UTF_8) + def convert_invalid_byte_sequence(str, enc) + str.force_encoding(enc) + str.scrub { |c| + c.bytes.map{ |b| "\\x#{b.to_s(16).upcase}" }.join + } + end + + def encode_with_invalid_byte_sequence(str, enc) + conv = Encoding::Converter.new(str.encoding, enc) dst = String.new begin ret = conv.primitive_convert(str, dst) @@ -640,7 +646,8 @@ module IRB message = exc.full_message(order: :top) order = :top end - message = convert_invalid_byte_sequence(message) + message = convert_invalid_byte_sequence(message, exc.message.encoding) + message = encode_with_invalid_byte_sequence(message, IRB.conf[:LC_MESSAGES].encoding) if message.encoding != IRB.conf[:LC_MESSAGES].encoding message = message.gsub(/((?:^\t.+$\n)+)/) { |m| case order when :top |