@@ -235,35 +235,41 @@ def _format(self, object, stream, indent, allowance, context, level):
235235 return
236236
237237 if issubclass (typ , str ) and len (object ) > 0 and r is str .__repr__ :
238- def _str_parts (s ):
239- """
240- Return a list of string literals comprising the repr()
241- of the given string using literal concatenation.
242- """
243- lines = s .splitlines (True )
244- for i , line in enumerate (lines ):
245- rep = repr (line )
246- if len (rep ) <= max_width :
247- yield rep
248- else :
249- # A list of alternating (non-space, space) strings
250- parts = re .split (r'(\s+)' , line ) + ['' ]
251- current = ''
252- for i in range (0 , len (parts ), 2 ):
253- part = parts [i ] + parts [i + 1 ]
254- candidate = current + part
255- if len (repr (candidate )) > max_width :
256- if current :
257- yield repr (current )
258- current = part
259- else :
260- current = candidate
261- if current :
262- yield repr (current )
263- for i , rep in enumerate (_str_parts (object )):
238+ chunks = []
239+ lines = object .splitlines (True )
240+ if level == 1 :
241+ indent += 1
242+ max_width -= 2
243+ for i , line in enumerate (lines ):
244+ rep = repr (line )
245+ if len (rep ) <= max_width :
246+ chunks .append (rep )
247+ else :
248+ # A list of alternating (non-space, space) strings
249+ parts = re .split (r'(\s+)' , line ) + ['' ]
250+ current = ''
251+ for i in range (0 , len (parts ), 2 ):
252+ part = parts [i ] + parts [i + 1 ]
253+ candidate = current + part
254+ if len (repr (candidate )) > max_width :
255+ if current :
256+ chunks .append (repr (current ))
257+ current = part
258+ else :
259+ current = candidate
260+ if current :
261+ chunks .append (repr (current ))
262+ if len (chunks ) == 1 :
263+ write (rep )
264+ return
265+ if level == 1 :
266+ write ('(' )
267+ for i , rep in enumerate (chunks ):
264268 if i > 0 :
265269 write ('\n ' + ' ' * indent )
266270 write (rep )
271+ if level == 1 :
272+ write (')' )
267273 return
268274 write (rep )
269275
0 commit comments