From 6cd88512ec705395e814076b15f1bf81ea25e625 Mon Sep 17 00:00:00 2001 From: Ryan Fitzgerald Date: Mon, 17 Dec 2012 23:42:08 -0800 Subject: [PATCH] Require the first target to be given at initialization --- lib/pry/pry_class.rb | 10 +---- lib/pry/pry_instance.rb | 76 +++++++++----------------------- spec/command_integration_spec.rb | 2 +- spec/commands/raise_up_spec.rb | 4 +- 4 files changed, 27 insertions(+), 65 deletions(-) diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index 6f87cd57..7aaae2f7 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -135,7 +135,7 @@ class Pry return end - target = Pry.binding_for(target || toplevel_binding) + options[:target] = target initial_session_setup # create the Pry instance to manage the session @@ -150,12 +150,6 @@ class Pry # yield the binding_stack to the hook for modification pry_instance.exec_hook(:when_started, target, options, pry_instance) - if !pry_instance.binding_stack.empty? - head = pry_instance.binding_stack.pop - else - head = target - end - # Clear the line before starting Pry. This fixes the issue discussed here: # https://github.com/pry/pry/issues/566 if Pry.config.auto_indent @@ -163,7 +157,7 @@ class Pry end # Enter the matrix - pry_instance.repl(head) + pry_instance.repl rescue Pry::TooSafeException puts "ERROR: Pry cannot work with $SAFE > 0" raise diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index e9b34096..9755cf4a 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -81,19 +81,20 @@ class Pry # @option options [Boolean] :quiet If true, omit the whereami banner when starting. # component of the REPL. (see print.rb) def initialize(options={}) - refresh(options) - @binding_stack = [] @indent = Pry::Indent.new @command_state = {} @eval_string = "" + + refresh_config(options) + push_initial_binding(options) end # Refresh the Pry instance settings from the Pry class. # Allows options to be specified to override settings from Pry class. # @param [Hash] options The options to override Pry class settings # for this instance. - def refresh(options={}) + def refresh_config(options={}) defaults = {} attributes = [ :input, :output, :commands, :print, :quiet, @@ -114,6 +115,10 @@ class Pry true end + def push_initial_binding(options) + push_binding(options[:target] || Pry.toplevel_binding) + end + # The currently active `Binding`. # @return [Binding] The currently active `Binding` for the session. def current_binding @@ -201,8 +206,8 @@ class Pry # Initialize the repl session. # @param [Binding] target The target binding for the session. - def repl_prologue(target) - exec_hook :before_session, output, target, self + def repl_prologue + exec_hook :before_session, output, current_binding, self set_last_result nil @input_array << nil # add empty input so _in_ and _out_ match @@ -210,52 +215,29 @@ class Pry # Clean-up after the repl session. # @param [Binding] target The target binding for the session. - def repl_epilogue(target) - exec_hook :after_session, output, target, self + def repl_epilogue + exec_hook :after_session, output, current_binding, self Pry.save_history if Pry.config.history.should_save end - # Start a read-eval-print-loop. - # If no parameter is given, default to top-level (main). - # @param [Object, Binding] target The receiver of the Pry session - # @return [Object] The target of the Pry session or an explictly given - # return value. If given return value is `nil` or no return value - # is specified then `target` will be returned. - # @example - # Pry.new.repl(Object.new) - def repl(target=TOPLEVEL_BINDING) - target = Pry.binding_for(target) - - repl_prologue(target) - - rep(target) + # Start a read-eval-print-loop in the current context. + def repl + repl_prologue + rep ensure - repl_epilogue(target) + repl_epilogue end - # Perform a read-eval-print. - # If no parameter is given, default to top-level (main). - # @param [Object, Binding] target The receiver of the read-eval-print - # @example - # Pry.new.rep(Object.new) - def rep(target=TOPLEVEL_BINDING) - re(target) + def rep + re end - # Perform a read-eval - # If no parameter is given, default to top-level (main). - # @param [Object, Binding] target The receiver of the read-eval-print - # @return [Object] The result of the eval or an `Exception` object in case of - # error. In the latter case, you can check whether the exception was raised - # or is just the result of the expression using #last_result_is_exception? - # @example - # Pry.new.re(Object.new) - def re(target=TOPLEVEL_BINDING) + def re break_data = nil exception = catch(:raise_up) do break_data = catch(:breakout) do - r(target) + r end exception = false end @@ -265,19 +247,7 @@ class Pry break_data end - # Perform a read. - # If no parameter is given, default to top-level (main). - # This is a multi-line read; so the read continues until a valid - # Ruby expression is received. - # Pry commands are also accepted here and operate on the target. - # @param [Object, Binding] target The receiver of the read. - # @return [String] The Ruby expression. - # @example - # Pry.new.r(Object.new) - def r(target_object = TOPLEVEL_BINDING) - push_binding target_object - @eval_string = "" - + def r loop do throw(:breakout) if current_binding.nil? @suppress_output = false @@ -297,8 +267,6 @@ class Pry exec_hook :after_read, @eval_string, self end - ensure - binding_stack.pop end def accept_line(line) diff --git a/spec/command_integration_spec.rb b/spec/command_integration_spec.rb index 071371c1..01e949dd 100644 --- a/spec/command_integration_spec.rb +++ b/spec/command_integration_spec.rb @@ -365,7 +365,7 @@ describe "commands" do it 'should create a command in a nested context and that command should be accessible from the parent' do redirect_pry_io(StringIO.new("@x=nil\ncd 7\n_pry_.commands.instance_eval {\ncommand('bing') { |arg| run arg }\n}\ncd ..\nbing ls\nexit-all"), @str_output) do - Pry.new.repl(0) + Pry.new(:target => 0).repl end pry_tester(0).eval(*(<<-RUBY.split("\n"))).should =~ /instance variables:\s+@x/m diff --git a/spec/commands/raise_up_spec.rb b/spec/commands/raise_up_spec.rb index 8dc3f59b..dcabdbb3 100644 --- a/spec/commands/raise_up_spec.rb +++ b/spec/commands/raise_up_spec.rb @@ -13,13 +13,13 @@ describe "raise-up" do it "should raise the exception with raise-up" do redirect_pry_io(InputTester.new("raise NoMethodError", "raise-up NoMethodError")) do - lambda { Pry.new.repl(0) }.should.raise NoMethodError + lambda { Pry.new(:target => 0).repl }.should.raise NoMethodError end end it "should raise an unamed exception with raise-up" do redirect_pry_io(InputTester.new("raise 'stop'","raise-up 'noreally'")) do - lambda { Pry.new.repl(0) }.should.raise RuntimeError, "noreally" + lambda { Pry.new(:target => 0).repl }.should.raise RuntimeError, "noreally" end end