Skip to content

Commit

Permalink
Fix Ruby 2.6 warning for Binding#source_location
Browse files Browse the repository at this point in the history
eval('[__FILE__, __LINE__]') will not work in future Ruby.
Let's stop using it when Binding#source_location is available because it
prints noisy warnings.

Since Binding#source_location is added in Ruby 2.6
ruby/ruby@571e48b7442, this commit is leaving
old code with branching by binding.respond_to?(:source_location).

Fixes #1871
  • Loading branch information
k0kubun committed Dec 11, 2018
1 parent 54e6ce5 commit 272b329
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
6 changes: 5 additions & 1 deletion lib/pry/commands/edit/file_and_line_locator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ class Command::Edit
module FileAndLineLocator
class << self
def from_binding(target)
[target.eval("__FILE__"), target.eval("__LINE__")]
if target.respond_to?(:source_location)
target.source_location
else
[target.eval("__FILE__"), target.eval("__LINE__")]

This comment has been minimized.

Copy link
@JoshCheek

JoshCheek Jan 23, 2020

Contributor

Seems like target.eval("[__FILE__, __LINE__]") would be better.

end
end

def from_code_object(code_object, filename_argument)
Expand Down
8 changes: 7 additions & 1 deletion lib/pry/commands/play.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ def content
# The file to play from when no code object is specified.
# e.g `play --lines 4..10`
def default_file
target.eval("__FILE__") && File.expand_path(target.eval("__FILE__"))
file =
if target.respond_to?(:source_location)
target.source_location.first
else
target.eval("__FILE__")
end
file && File.expand_path(file)
end

def file_content
Expand Down
8 changes: 7 additions & 1 deletion lib/pry/commands/reload_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ def process
private

def current_file
File.expand_path target.eval("__FILE__")
file =
if target.respond_to?(:source_location)
target.source_location.first
else
target.eval("__FILE__")
end
File.expand_path file
end

def reload_current_file
Expand Down
9 changes: 7 additions & 2 deletions lib/pry/commands/whereami.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,13 @@ class << self
BANNER

def setup
@file = expand_path(target.eval('__FILE__'))
@line = target.eval('__LINE__')
if target.respond_to?(:source_location)
file, @line = target.source_location
@file = expand_path(file)
else
@file = expand_path(target.eval('__FILE__'))
@line = target.eval('__LINE__')
end
@method = Pry::Method.from_binding(target)
end

Expand Down
28 changes: 24 additions & 4 deletions lib/pry/method/weird_method_locator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ class << self
# @return [Boolean]
def normal_method?(method, b)
if method && method.source_file && method.source_range
binding_file, binding_line = b.eval('__FILE__'), b.eval('__LINE__')
if b.respond_to?(:source_location)
binding_file, binding_line = b.source_location
else
binding_file, binding_line = b.eval('__FILE__'), b.eval('__LINE__')
end
(File.expand_path(method.source_file) == File.expand_path(binding_file)) &&
method.source_range.include?(binding_line)
end
Expand Down Expand Up @@ -77,15 +81,31 @@ def target_self
end

def target_file
pry_file? ? target.eval('__FILE__') : File.expand_path(target.eval('__FILE__'))
file =
if target.respond_to?(:source_location)
target.source_location.first
else
target.eval('__FILE__')
end
pry_file? ? file : File.expand_path(file)
end

def target_line
target.eval('__LINE__')
if target.respond_to?(:source_location)
target.source_location.last
else
target.eval('__LINE__')
end
end

def pry_file?
Pry.eval_path == target.eval('__FILE__')
file =
if target.respond_to?(:source_location)
target.source_location.first
else
target.eval('__FILE__')
end
Pry.eval_path == file
end

# it's possible in some cases that the method we find by this approach is a sub-method of
Expand Down

0 comments on commit 272b329

Please sign in to comment.