Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

now using 'retry' (in read loop) to switch input objects in input_stack

This is so we can make the switch to the new input object seemless on encountering EOFError in previous input object.
We also detect whether we are going to go into an infinite EOFError state (i.e if Pry.config.input generates EOFError) and so we break out of the REPL with an error message
and a throw(:breakout)
  • Loading branch information...
commit 4e3853ea33f7a647b080eb2c2049c0df9e4cd619 1 parent 207f2c8
@banister banister authored
View
1  CHANGELOG
@@ -13,6 +13,7 @@
* local ./.pryrc now loaded after ~/.pryrc if it exists
* cat --ex N and edit --ex N now can navigate through backtrace, where cat --ex (with no args) moves throuh successive levels of the backtrace automatically with state stored on the exceptino object itself
* new option Pry.config.exception_window_size determines window size for cat --ex
+* input_stack now implemented - pushing objects onto a pry instance's input_stack causes the instance to read from those objects in turn as it encounters EOF on the previous object. On finishing the input_stack the input object for the pry instance is set back to Pry.config.input, if this fails, pry breaks out of the REPL (throw(:breakout)) with an error message
8/9/2011 version 0.9.5
View
15 lib/pry/pry_instance.rb
@@ -395,6 +395,7 @@ def readline(current_prompt="> ")
Pry.history << line.dup if line
line
else
+ should_retry = true
begin
if input.method(:readline).arity == 1
input.readline(current_prompt)
@@ -403,8 +404,18 @@ def readline(current_prompt="> ")
end
rescue EOFError
- self.input = input_stack.empty? ? Pry.config.input : input_stack.pop
- ""
+ if input_stack.empty?
+ self.input = Pry.config.input
+ if !should_retry
+ output.puts "Error: Pry ran out of things to read from! Attempting to break out of REPL."
+ throw(:breakout)
+ end
+
+ should_retry = false
+ else
+ self.input = input_stack.pop
+ end
+ retry
end
end
end
View
45 test/test_input_stack.rb
@@ -12,7 +12,7 @@
Pry.config.input_stack = []
end
- it 'should read from all input objects on stack and exit session' do
+ it 'should read from all input objects on stack and exit session (usingn repl)' do
stack = [b = StringIO.new(":cloister\nexit\n"), c = StringIO.new(":baron\n")]
instance = Pry.new(:input => StringIO.new(":alex\n"),
:output => str_output = StringIO.new,
@@ -24,4 +24,47 @@
str_output.string.should =~ /:cloister/
end
+ it 'input objects should be popped off stack as they are used up' do
+ stack = [StringIO.new(":cloister\n"), StringIO.new(":baron\n")]
+ instance = Pry.new(:input => StringIO.new(":alex\n"),
+ :output => str_output = StringIO.new,
+ :input_stack => stack)
+
+ stack.size.should == 2
+
+ instance.rep
+ str_output.string.should =~ /:alex/
+ instance.rep
+ str_output.string.should =~ /:baron/
+ stack.size.should == 1
+ instance.rep
+ str_output.string.should =~ /:cloister/
+ stack.size.should == 0
+ end
+
+ it 'should revert to Pry.config.input when it runs out of input objects in input_stack' do
+ redirect_pry_io(StringIO.new(":rimbaud\nexit\n"), StringIO.new) do
+ stack = [StringIO.new(":cloister\n"), StringIO.new(":baron\n")]
+ instance = Pry.new(:input => StringIO.new(":alex\n"),
+ :output => str_output = StringIO.new,
+ :input_stack => stack)
+
+ instance.repl
+ str_output.string.should =~ /:alex/
+ str_output.string.should =~ /:baron/
+ str_output.string.should =~ /:cloister/
+ str_output.string.should =~ /:rimbaud/
+ end
+ end
+
+ it 'should display error and throw(:breakout) if at end of input after using up input_stack objects' do
+ str_output = StringIO.new
+ catch(:breakout) do
+ redirect_pry_io(StringIO.new(":rimbaud\n"), str_output) do
+ Pry.new(:input_stack => [StringIO.new(":a\n"), StringIO.new(":b\n")]).repl
+ end
+ end
+ str_output.string.should =~ /Error: Pry ran out of things to read/
+ end
+
end
View
16 test/test_pry.rb
@@ -86,16 +86,14 @@ class Hello
o.instance_variable_get(:@x).should == 10
end
- it 'should not output anything for no input' do
- outp = StringIO.new
-
- # note i could not use mock_pry() for this test for some
- # reason, as i'd always get "\n" as output instead of ""
- redirect_pry_io(StringIO.new(""), outp) do
- Pry.new.rep(self)
+ it 'should display error and throw(:breakout) if Pry instance runs out of input' do
+ str_output = StringIO.new
+ catch(:breakout) do
+ redirect_pry_io(StringIO.new(":nothing\n"), str_output) do
+ Pry.new.repl
+ end
end
-
- outp.string.empty?.should == true
+ str_output.string.should =~ /Error: Pry ran out of things to read/
end
it 'should make self evaluate to the receiver of the rep session' do
Please sign in to comment.
Something went wrong with that request. Please try again.