Skip to content

Commit ec2947a

Browse files
committed
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}
1 parent fc3e1d9 commit ec2947a

File tree

2 files changed

+40
-20
lines changed

2 files changed

+40
-20
lines changed

lib/irb/ext/loader.rb

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,18 @@ def search_file_from_ruby_path(fn) # :nodoc:
5050
# See Irb#suspend_input_method for more information.
5151
def source_file(path)
5252
irb.suspend_name(path, File.basename(path)) do
53-
irb.suspend_input_method(FileInputMethod.new(path)) do
54-
|back_io|
55-
irb.signal_status(:IN_LOAD) do
56-
if back_io.kind_of?(FileInputMethod)
57-
irb.eval_input
58-
else
59-
begin
53+
FileInputMethod.open(path) do |io|
54+
irb.suspend_input_method(io) do
55+
|back_io|
56+
irb.signal_status(:IN_LOAD) do
57+
if back_io.kind_of?(FileInputMethod)
6058
irb.eval_input
61-
rescue LoadAbort
62-
print "load abort!!\n"
59+
else
60+
begin
61+
irb.eval_input
62+
rescue LoadAbort
63+
print "load abort!!\n"
64+
end
6365
end
6466
end
6567
end
@@ -79,16 +81,18 @@ def load_file(path, priv = nil)
7981
ws = WorkSpace.new
8082
end
8183
irb.suspend_workspace(ws) do
82-
irb.suspend_input_method(FileInputMethod.new(path)) do
83-
|back_io|
84-
irb.signal_status(:IN_LOAD) do
85-
if back_io.kind_of?(FileInputMethod)
86-
irb.eval_input
87-
else
88-
begin
84+
FileInputMethod.open(path) do |io|
85+
irb.suspend_input_method(io) do
86+
|back_io|
87+
irb.signal_status(:IN_LOAD) do
88+
if back_io.kind_of?(FileInputMethod)
8989
irb.eval_input
90-
rescue LoadAbort
91-
print "load abort!!\n"
90+
else
91+
begin
92+
irb.eval_input
93+
rescue LoadAbort
94+
print "load abort!!\n"
95+
end
9296
end
9397
end
9498
end

lib/irb/input-method.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,22 @@ def inspect
124124

125125
# Use a File for IO with irb, see InputMethod
126126
class FileInputMethod < InputMethod
127+
class << self
128+
def open(file, &block)
129+
begin
130+
io = new(file)
131+
block.call(io)
132+
ensure
133+
io&.close
134+
end
135+
end
136+
end
137+
127138
# Creates a new input method object
128139
def initialize(file)
129140
super
130141
@io = IRB::MagicFile.open(file)
142+
@external_encoding = @io.external_encoding
131143
end
132144
# The file name of this input method, usually given during initialization.
133145
attr_reader :file_name
@@ -137,7 +149,7 @@ def initialize(file)
137149
#
138150
# See IO#eof? for more information.
139151
def eof?
140-
@io.eof?
152+
@io.closed? || @io.eof?
141153
end
142154

143155
# Reads the next line from this input method.
@@ -150,13 +162,17 @@ def gets
150162

151163
# The external encoding for standard input.
152164
def encoding
153-
@io.external_encoding
165+
@external_encoding
154166
end
155167

156168
# For debug message
157169
def inspect
158170
'FileInputMethod'
159171
end
172+
173+
def close
174+
@io.close
175+
end
160176
end
161177

162178
begin

0 commit comments

Comments
 (0)