From b375904b37d07608ffc395cb59fbcb747f2b330f Mon Sep 17 00:00:00 2001 From: tompng Date: Thu, 28 Nov 2024 03:31:27 +0900 Subject: [PATCH] Fix io_gate.encoding raises IOError in ruby <= 3.0 --- lib/reline/io/ansi.rb | 3 +++ lib/reline/io/dumb.rb | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/reline/io/ansi.rb b/lib/reline/io/ansi.rb index 4fd0023259..c51e97b722 100644 --- a/lib/reline/io/ansi.rb +++ b/lib/reline/io/ansi.rb @@ -38,6 +38,9 @@ def initialize def encoding @input.external_encoding || Encoding.default_external + rescue IOError + # STDIN.external_encoding raises IOError in Ruby <= 3.0 when STDIN is closed + Encoding.default_external end def set_default_key_bindings(config) diff --git a/lib/reline/io/dumb.rb b/lib/reline/io/dumb.rb index 34bba98ba1..e58ba0f6d8 100644 --- a/lib/reline/io/dumb.rb +++ b/lib/reline/io/dumb.rb @@ -21,8 +21,11 @@ def encoding elsif RUBY_PLATFORM =~ /mswin|mingw/ Encoding::UTF_8 else - @input.external_encoding || Encoding::default_external + @input.external_encoding || Encoding.default_external end + rescue IOError + # STDIN.external_encoding raises IOError in Ruby <= 3.0 when STDIN is closed + Encoding.default_external end def set_default_key_bindings(_)