[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
|
Thread.current[:__recursive_key__][:inspect].delete id
|
||||||
end
|
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
|
# Adds +obj+ to the pretty printing buffer
|
||||||
# using Object#pretty_print or Object#pretty_print_cycle.
|
# using Object#pretty_print or Object#pretty_print_cycle.
|
||||||
#
|
#
|
||||||
@ -198,15 +216,12 @@ class PP < PrettyPrint
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
begin
|
guard_inspect(obj) do
|
||||||
push_inspect_key(obj)
|
|
||||||
group do
|
group do
|
||||||
obj.pretty_print self
|
obj.pretty_print self
|
||||||
rescue NoMethodError
|
rescue NoMethodError
|
||||||
text Kernel.instance_method(:inspect).bind_call(obj)
|
text Kernel.instance_method(:inspect).bind_call(obj)
|
||||||
end
|
end
|
||||||
ensure
|
|
||||||
pop_inspect_key(obj) unless PP.sharing_detection
|
|
||||||
end
|
end
|
||||||
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([->(*a){a.last.clear}.ruby2_keywords.call(a: 1)], ''.dup))
|
||||||
assert_equal("[{}]", PP.singleline_pp([Hash.ruby2_keywords_hash({})], ''.dup))
|
assert_equal("[{}]", PP.singleline_pp([Hash.ruby2_keywords_hash({})], ''.dup))
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
class PPDelegateTest < Test::Unit::TestCase
|
class PPDelegateTest < Test::Unit::TestCase
|
||||||
|
Loading…
x
Reference in New Issue
Block a user