File tree Expand file tree Collapse file tree 2 files changed +35
-4
lines changed Expand file tree Collapse file tree 2 files changed +35
-4
lines changed Original file line number Diff line number Diff line change @@ -183,6 +183,24 @@ def pop_inspect_key(id)
183
183
Thread . current [ :__recursive_key__ ] [ :inspect ] . delete id
184
184
end
185
185
186
+ private def guard_inspect ( object )
187
+ recursive_state = Thread . current [ :__recursive_key__ ]
188
+
189
+ if recursive_state && recursive_state . key? ( :inspect )
190
+ begin
191
+ push_inspect_key ( object )
192
+ yield
193
+ ensure
194
+ pop_inspect_key ( object ) unless PP . sharing_detection
195
+ end
196
+ else
197
+ guard_inspect_key do
198
+ push_inspect_key ( object )
199
+ yield
200
+ end
201
+ end
202
+ end
203
+
186
204
# Adds +obj+ to the pretty printing buffer
187
205
# using Object#pretty_print or Object#pretty_print_cycle.
188
206
#
@@ -198,15 +216,12 @@ def pp(obj)
198
216
return
199
217
end
200
218
201
- begin
202
- push_inspect_key ( obj )
219
+ guard_inspect ( obj ) do
203
220
group do
204
221
obj . pretty_print self
205
222
rescue NoMethodError
206
223
text Kernel . instance_method ( :inspect ) . bind_call ( obj )
207
224
end
208
- ensure
209
- pop_inspect_key ( obj ) unless PP . sharing_detection
210
225
end
211
226
end
212
227
Original file line number Diff line number Diff line change @@ -243,6 +243,22 @@ def test_hash_in_array
243
243
assert_equal ( "[{}]" , PP . singleline_pp ( [ -> ( *a ) { a . last . clear } . ruby2_keywords . call ( a : 1 ) ] , '' . dup ) )
244
244
assert_equal ( "[{}]" , PP . singleline_pp ( [ Hash . ruby2_keywords_hash ( { } ) ] , '' . dup ) )
245
245
end
246
+
247
+ def test_direct_pp
248
+ buffer = String . new
249
+
250
+ a = [ ]
251
+ a << a
252
+
253
+ # Isolate the test from any existing Thread.current[:__recursive_key__][:inspect].
254
+ Thread . new do
255
+ q = PP ::SingleLine . new ( buffer )
256
+ q . pp ( a )
257
+ q . flush
258
+ end . join
259
+
260
+ assert_equal ( "[[...]]" , buffer )
261
+ end
246
262
end
247
263
248
264
class PPDelegateTest < Test ::Unit ::TestCase
You can’t perform that action at this time.
0 commit comments