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
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

View file

@ -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)

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
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

View file

@ -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