[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
This commit is contained in:
parent
c736714de1
commit
5b05b85d85
@ -50,7 +50,8 @@ module IRB # :nodoc:
|
|||||||
# See Irb#suspend_input_method for more information.
|
# See Irb#suspend_input_method for more information.
|
||||||
def source_file(path)
|
def source_file(path)
|
||||||
irb.suspend_name(path, File.basename(path)) do
|
irb.suspend_name(path, File.basename(path)) do
|
||||||
irb.suspend_input_method(FileInputMethod.new(path)) do
|
FileInputMethod.open(path) do |io|
|
||||||
|
irb.suspend_input_method(io) do
|
||||||
|back_io|
|
|back_io|
|
||||||
irb.signal_status(:IN_LOAD) do
|
irb.signal_status(:IN_LOAD) do
|
||||||
if back_io.kind_of?(FileInputMethod)
|
if back_io.kind_of?(FileInputMethod)
|
||||||
@ -66,6 +67,7 @@ module IRB # :nodoc:
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Loads the given file in the current session's context and evaluates it.
|
# Loads the given file in the current session's context and evaluates it.
|
||||||
#
|
#
|
||||||
@ -79,7 +81,8 @@ module IRB # :nodoc:
|
|||||||
ws = WorkSpace.new
|
ws = WorkSpace.new
|
||||||
end
|
end
|
||||||
irb.suspend_workspace(ws) do
|
irb.suspend_workspace(ws) do
|
||||||
irb.suspend_input_method(FileInputMethod.new(path)) do
|
FileInputMethod.open(path) do |io|
|
||||||
|
irb.suspend_input_method(io) do
|
||||||
|back_io|
|
|back_io|
|
||||||
irb.signal_status(:IN_LOAD) do
|
irb.signal_status(:IN_LOAD) do
|
||||||
if back_io.kind_of?(FileInputMethod)
|
if back_io.kind_of?(FileInputMethod)
|
||||||
@ -96,6 +99,7 @@ module IRB # :nodoc:
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def old # :nodoc:
|
def old # :nodoc:
|
||||||
back_io = @io
|
back_io = @io
|
||||||
|
@ -124,10 +124,22 @@ module IRB
|
|||||||
|
|
||||||
# Use a File for IO with irb, see InputMethod
|
# Use a File for IO with irb, see InputMethod
|
||||||
class FileInputMethod < 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
|
# Creates a new input method object
|
||||||
def initialize(file)
|
def initialize(file)
|
||||||
super
|
super
|
||||||
@io = IRB::MagicFile.open(file)
|
@io = IRB::MagicFile.open(file)
|
||||||
|
@external_encoding = @io.external_encoding
|
||||||
end
|
end
|
||||||
# The file name of this input method, usually given during initialization.
|
# The file name of this input method, usually given during initialization.
|
||||||
attr_reader :file_name
|
attr_reader :file_name
|
||||||
@ -137,7 +149,7 @@ module IRB
|
|||||||
#
|
#
|
||||||
# See IO#eof? for more information.
|
# See IO#eof? for more information.
|
||||||
def eof?
|
def eof?
|
||||||
@io.eof?
|
@io.closed? || @io.eof?
|
||||||
end
|
end
|
||||||
|
|
||||||
# Reads the next line from this input method.
|
# Reads the next line from this input method.
|
||||||
@ -150,13 +162,17 @@ module IRB
|
|||||||
|
|
||||||
# The external encoding for standard input.
|
# The external encoding for standard input.
|
||||||
def encoding
|
def encoding
|
||||||
@io.external_encoding
|
@external_encoding
|
||||||
end
|
end
|
||||||
|
|
||||||
# For debug message
|
# For debug message
|
||||||
def inspect
|
def inspect
|
||||||
'FileInputMethod'
|
'FileInputMethod'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def close
|
||||||
|
@io.close
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
begin
|
begin
|
||||||
|
Loading…
x
Reference in New Issue
Block a user