ruby-changes:65065
From: Nobuhiro <ko1@a...>
Date: Wed, 27 Jan 2021 15:02:08 +0900 (JST)
Subject: [ruby-changes:65065] 5b05b85d85 (master): [ruby/irb] add `IRB::FileInputMethod.open` to ensure closing associated File
https://git.ruby-lang.org/ruby.git/commit/?id=5b05b85d85 From 5b05b85d85002fd47eeb5e28f9f2898e99507b75 Mon Sep 17 00:00:00 2001 From: Nobuhiro IMAI <nov@y...> Date: Sat, 23 Jan 2021 02:12:39 +0900 Subject: [ruby/irb] add `IRB::FileInputMethod.open` to ensure closing associated File * tweak some methods not to raise exception after `#close` * use it in `IRB::IrbLoader#{source_file,load_file} https://github.com/ruby/irb/commit/ec2947acbd --- lib/irb/ext/loader.rb | 40 ++++++++++++++++++++++------------------ lib/irb/input-method.rb | 20 ++++++++++++++++++-- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/lib/irb/ext/loader.rb b/lib/irb/ext/loader.rb index 1b683d8..5a198bb 100644 --- a/lib/irb/ext/loader.rb +++ b/lib/irb/ext/loader.rb @@ -50,16 +50,18 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/ext/loader.rb#L50 # See Irb#suspend_input_method for more information. def source_file(path) irb.suspend_name(path, File.basename(path)) do - irb.suspend_input_method(FileInputMethod.new(path)) do - |back_io| - irb.signal_status(:IN_LOAD) do - if back_io.kind_of?(FileInputMethod) - irb.eval_input - else - begin + FileInputMethod.open(path) do |io| + irb.suspend_input_method(io) do + |back_io| + irb.signal_status(:IN_LOAD) do + if back_io.kind_of?(FileInputMethod) irb.eval_input - rescue LoadAbort - print "load abort!!\n" + else + begin + irb.eval_input + rescue LoadAbort + print "load abort!!\n" + end end end end @@ -79,16 +81,18 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/ext/loader.rb#L81 ws = WorkSpace.new end irb.suspend_workspace(ws) do - irb.suspend_input_method(FileInputMethod.new(path)) do - |back_io| - irb.signal_status(:IN_LOAD) do - if back_io.kind_of?(FileInputMethod) - irb.eval_input - else - begin + FileInputMethod.open(path) do |io| + irb.suspend_input_method(io) do + |back_io| + irb.signal_status(:IN_LOAD) do + if back_io.kind_of?(FileInputMethod) irb.eval_input - rescue LoadAbort - print "load abort!!\n" + else + begin + irb.eval_input + rescue LoadAbort + print "load abort!!\n" + end end end end diff --git a/lib/irb/input-method.rb b/lib/irb/input-method.rb index 61540a1..e223672 100644 --- a/lib/irb/input-method.rb +++ b/lib/irb/input-method.rb @@ -124,10 +124,22 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/input-method.rb#L124 # Use a File for IO with irb, see InputMethod class FileInputMethod < InputMethod + class << self + def open(file, &block) + begin + io = new(file) + block.call(io) + ensure + io&.close + end + end + end + # Creates a new input method object def initialize(file) super @io = IRB::MagicFile.open(file) + @external_encoding = @io.external_encoding end # The file name of this input method, usually given during initialization. attr_reader :file_name @@ -137,7 +149,7 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/input-method.rb#L149 # # See IO#eof? for more information. def eof? - @io.eof? + @io.closed? || @io.eof? end # Reads the next line from this input method. @@ -150,13 +162,17 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/input-method.rb#L162 # The external encoding for standard input. def encoding - @io.external_encoding + @external_encoding end # For debug message def inspect 'FileInputMethod' end + + def close + @io.close + end end begin -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/