[ruby/pp] Ensure the thread local state is always set up.
(https://github.com/ruby/pp/pull/38) https://github.com/ruby/pp/commit/5b5d483ac2
This commit is contained in:
parent
d32fa5283f
commit
021ccbf7e8
23
lib/pp.rb
23
lib/pp.rb
@ -183,6 +183,24 @@ class PP < PrettyPrint
|
||||
Thread.current[:__recursive_key__][:inspect].delete id
|
||||
end
|
||||
|
||||
private def guard_inspect(object)
|
||||
recursive_state = Thread.current[:__recursive_key__]
|
||||
|
||||
if recursive_state && recursive_state.key?(:inspect)
|
||||
begin
|
||||
push_inspect_key(object)
|
||||
yield
|
||||
ensure
|
||||
pop_inspect_key(object) unless PP.sharing_detection
|
||||
end
|
||||
else
|
||||
guard_inspect_key do
|
||||
push_inspect_key(object)
|
||||
yield
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Adds +obj+ to the pretty printing buffer
|
||||
# using Object#pretty_print or Object#pretty_print_cycle.
|
||||
#
|
||||
@ -198,15 +216,12 @@ class PP < PrettyPrint
|
||||
return
|
||||
end
|
||||
|
||||
begin
|
||||
push_inspect_key(obj)
|
||||
guard_inspect(obj) do
|
||||
group do
|
||||
obj.pretty_print self
|
||||
rescue NoMethodError
|
||||
text Kernel.instance_method(:inspect).bind_call(obj)
|
||||
end
|
||||
ensure
|
||||
pop_inspect_key(obj) unless PP.sharing_detection
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -243,6 +243,22 @@ class PPSingleLineTest < Test::Unit::TestCase
|
||||
assert_equal("[{}]", PP.singleline_pp([->(*a){a.last.clear}.ruby2_keywords.call(a: 1)], ''.dup))
|
||||
assert_equal("[{}]", PP.singleline_pp([Hash.ruby2_keywords_hash({})], ''.dup))
|
||||
end
|
||||
|
||||
def test_direct_pp
|
||||
buffer = String.new
|
||||
|
||||
a = []
|
||||
a << a
|
||||
|
||||
# Isolate the test from any existing Thread.current[:__recursive_key__][:inspect].
|
||||
Thread.new do
|
||||
q = PP::SingleLine.new(buffer)
|
||||
q.pp(a)
|
||||
q.flush
|
||||
end.join
|
||||
|
||||
assert_equal("[[...]]", buffer)
|
||||
end
|
||||
end
|
||||
|
||||
class PPDelegateTest < Test::Unit::TestCase
|
||||
|
Loading…
x
Reference in New Issue
Block a user