1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Require the first target to be given at initialization

This commit is contained in:
Ryan Fitzgerald 2012-12-17 23:42:08 -08:00
parent d9d0215ce8
commit 6cd88512ec
4 changed files with 27 additions and 65 deletions

View file

@ -135,7 +135,7 @@ class Pry
return return
end end
target = Pry.binding_for(target || toplevel_binding) options[:target] = target
initial_session_setup initial_session_setup
# create the Pry instance to manage the session # create the Pry instance to manage the session
@ -150,12 +150,6 @@ class Pry
# yield the binding_stack to the hook for modification # yield the binding_stack to the hook for modification
pry_instance.exec_hook(:when_started, target, options, pry_instance) 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: # Clear the line before starting Pry. This fixes the issue discussed here:
# https://github.com/pry/pry/issues/566 # https://github.com/pry/pry/issues/566
if Pry.config.auto_indent if Pry.config.auto_indent
@ -163,7 +157,7 @@ class Pry
end end
# Enter the matrix # Enter the matrix
pry_instance.repl(head) pry_instance.repl
rescue Pry::TooSafeException rescue Pry::TooSafeException
puts "ERROR: Pry cannot work with $SAFE > 0" puts "ERROR: Pry cannot work with $SAFE > 0"
raise raise

View file

@ -81,19 +81,20 @@ class Pry
# @option options [Boolean] :quiet If true, omit the whereami banner when starting. # @option options [Boolean] :quiet If true, omit the whereami banner when starting.
# component of the REPL. (see print.rb) # component of the REPL. (see print.rb)
def initialize(options={}) def initialize(options={})
refresh(options)
@binding_stack = [] @binding_stack = []
@indent = Pry::Indent.new @indent = Pry::Indent.new
@command_state = {} @command_state = {}
@eval_string = "" @eval_string = ""
refresh_config(options)
push_initial_binding(options)
end end
# Refresh the Pry instance settings from the Pry class. # Refresh the Pry instance settings from the Pry class.
# Allows options to be specified to override settings from Pry class. # Allows options to be specified to override settings from Pry class.
# @param [Hash] options The options to override Pry class settings # @param [Hash] options The options to override Pry class settings
# for this instance. # for this instance.
def refresh(options={}) def refresh_config(options={})
defaults = {} defaults = {}
attributes = [ attributes = [
:input, :output, :commands, :print, :quiet, :input, :output, :commands, :print, :quiet,
@ -114,6 +115,10 @@ class Pry
true true
end end
def push_initial_binding(options)
push_binding(options[:target] || Pry.toplevel_binding)
end
# The currently active `Binding`. # The currently active `Binding`.
# @return [Binding] The currently active `Binding` for the session. # @return [Binding] The currently active `Binding` for the session.
def current_binding def current_binding
@ -201,8 +206,8 @@ class Pry
# Initialize the repl session. # Initialize the repl session.
# @param [Binding] target The target binding for the session. # @param [Binding] target The target binding for the session.
def repl_prologue(target) def repl_prologue
exec_hook :before_session, output, target, self exec_hook :before_session, output, current_binding, self
set_last_result nil set_last_result nil
@input_array << nil # add empty input so _in_ and _out_ match @input_array << nil # add empty input so _in_ and _out_ match
@ -210,52 +215,29 @@ class Pry
# Clean-up after the repl session. # Clean-up after the repl session.
# @param [Binding] target The target binding for the session. # @param [Binding] target The target binding for the session.
def repl_epilogue(target) def repl_epilogue
exec_hook :after_session, output, target, self exec_hook :after_session, output, current_binding, self
Pry.save_history if Pry.config.history.should_save Pry.save_history if Pry.config.history.should_save
end end
# Start a read-eval-print-loop. # Start a read-eval-print-loop in the current context.
# If no parameter is given, default to top-level (main). def repl
# @param [Object, Binding] target The receiver of the Pry session repl_prologue
# @return [Object] The target of the Pry session or an explictly given rep
# 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)
ensure ensure
repl_epilogue(target) repl_epilogue
end end
# Perform a read-eval-print. def rep
# If no parameter is given, default to top-level (main). re
# @param [Object, Binding] target The receiver of the read-eval-print
# @example
# Pry.new.rep(Object.new)
def rep(target=TOPLEVEL_BINDING)
re(target)
end end
# Perform a read-eval def re
# 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)
break_data = nil break_data = nil
exception = catch(:raise_up) do exception = catch(:raise_up) do
break_data = catch(:breakout) do break_data = catch(:breakout) do
r(target) r
end end
exception = false exception = false
end end
@ -265,19 +247,7 @@ class Pry
break_data break_data
end end
# Perform a read. def r
# 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 = ""
loop do loop do
throw(:breakout) if current_binding.nil? throw(:breakout) if current_binding.nil?
@suppress_output = false @suppress_output = false
@ -297,8 +267,6 @@ class Pry
exec_hook :after_read, @eval_string, self exec_hook :after_read, @eval_string, self
end end
ensure
binding_stack.pop
end end
def accept_line(line) def accept_line(line)

View file

@ -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 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 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 end
pry_tester(0).eval(*(<<-RUBY.split("\n"))).should =~ /instance variables:\s+@x/m pry_tester(0).eval(*(<<-RUBY.split("\n"))).should =~ /instance variables:\s+@x/m

View file

@ -13,13 +13,13 @@ describe "raise-up" do
it "should raise the exception with raise-up" do it "should raise the exception with raise-up" do
redirect_pry_io(InputTester.new("raise NoMethodError", "raise-up NoMethodError")) 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
end end
it "should raise an unamed exception with raise-up" do it "should raise an unamed exception with raise-up" do
redirect_pry_io(InputTester.new("raise 'stop'","raise-up 'noreally'")) 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
end end