11
11
import _pytest ._code
12
12
from ..compat import Sequence
13
13
14
- u = six .text_type
15
-
16
14
# The _reprcompare attribute on the util module is used by the new assertion
17
15
# interpretation code and assertion rewriter to detect this plugin was
18
16
# loaded and in turn call the hooks defined here as part of the
23
21
# the re-encoding is needed for python2 repr
24
22
# with non-ascii characters (see issue 877 and 1379)
25
23
def ecu (s ):
26
- try :
27
- return u ( s , "utf -8" , "replace" )
28
- except TypeError :
24
+ if isinstance ( s , bytes ) :
25
+ return s . decode ( "UTF -8" , "replace" )
26
+ else :
29
27
return s
30
28
31
29
@@ -42,7 +40,7 @@ def format_explanation(explanation):
42
40
explanation = ecu (explanation )
43
41
lines = _split_explanation (explanation )
44
42
result = _format_lines (lines )
45
- return u ( "\n " ) .join (result )
43
+ return u"\n " .join (result )
46
44
47
45
48
46
def _split_explanation (explanation ):
@@ -52,7 +50,7 @@ def _split_explanation(explanation):
52
50
Any other newlines will be escaped and appear in the line as the
53
51
literal '\n ' characters.
54
52
"""
55
- raw_lines = (explanation or u ( "" ) ).split ("\n " )
53
+ raw_lines = (explanation or u"" ).split ("\n " )
56
54
lines = [raw_lines [0 ]]
57
55
for values in raw_lines [1 :]:
58
56
if values and values [0 ] in ["{" , "}" , "~" , ">" ]:
@@ -77,13 +75,13 @@ def _format_lines(lines):
77
75
for line in lines [1 :]:
78
76
if line .startswith ("{" ):
79
77
if stackcnt [- 1 ]:
80
- s = u ( "and " )
78
+ s = u"and "
81
79
else :
82
- s = u ( "where " )
80
+ s = u"where "
83
81
stack .append (len (result ))
84
82
stackcnt [- 1 ] += 1
85
83
stackcnt .append (0 )
86
- result .append (u ( " +" ) + u ( " " ) * (len (stack ) - 1 ) + s + line [1 :])
84
+ result .append (u" +" + u" " * (len (stack ) - 1 ) + s + line [1 :])
87
85
elif line .startswith ("}" ):
88
86
stack .pop ()
89
87
stackcnt .pop ()
@@ -92,7 +90,7 @@ def _format_lines(lines):
92
90
assert line [0 ] in ["~" , ">" ]
93
91
stack [- 1 ] += 1
94
92
indent = len (stack ) if line .startswith ("~" ) else len (stack ) - 1
95
- result .append (u ( " " ) * indent + line [1 :])
93
+ result .append (u" " * indent + line [1 :])
96
94
assert len (stack ) == 1
97
95
return result
98
96
@@ -110,7 +108,7 @@ def assertrepr_compare(config, op, left, right):
110
108
left_repr = py .io .saferepr (left , maxsize = int (width // 2 ))
111
109
right_repr = py .io .saferepr (right , maxsize = width - len (left_repr ))
112
110
113
- summary = u ( "%s %s %s" ) % (ecu (left_repr ), op , ecu (right_repr ))
111
+ summary = u"%s %s %s" % (ecu (left_repr ), op , ecu (right_repr ))
114
112
115
113
def issequence (x ):
116
114
return isinstance (x , Sequence ) and not isinstance (x , basestring )
@@ -155,11 +153,9 @@ def isiterable(obj):
155
153
explanation = _notin_text (left , right , verbose )
156
154
except Exception :
157
155
explanation = [
158
- u (
159
- "(pytest_assertion plugin: representation of details failed. "
160
- "Probably an object has a faulty __repr__.)"
161
- ),
162
- u (_pytest ._code .ExceptionInfo ()),
156
+ u"(pytest_assertion plugin: representation of details failed. "
157
+ u"Probably an object has a faulty __repr__.)" ,
158
+ six .text_type (_pytest ._code .ExceptionInfo ()),
163
159
]
164
160
165
161
if not explanation :
@@ -203,8 +199,7 @@ def escape_for_readable_diff(binary_text):
203
199
if i > 42 :
204
200
i -= 10 # Provide some context
205
201
explanation = [
206
- u ("Skipping %s identical leading characters in diff, use -v to show" )
207
- % i
202
+ u"Skipping %s identical leading characters in diff, use -v to show" % i
208
203
]
209
204
left = left [i :]
210
205
right = right [i :]
@@ -215,11 +210,8 @@ def escape_for_readable_diff(binary_text):
215
210
if i > 42 :
216
211
i -= 10 # Provide some context
217
212
explanation += [
218
- u (
219
- "Skipping %s identical trailing "
220
- "characters in diff, use -v to show"
221
- )
222
- % i
213
+ u"Skipping {} identical trailing "
214
+ u"characters in diff, use -v to show" .format (i )
223
215
]
224
216
left = left [:- i ]
225
217
right = right [:- i ]
@@ -237,21 +229,21 @@ def escape_for_readable_diff(binary_text):
237
229
238
230
def _compare_eq_iterable (left , right , verbose = False ):
239
231
if not verbose :
240
- return [u ( "Use -v to get the full diff" ) ]
232
+ return [u"Use -v to get the full diff" ]
241
233
# dynamic import to speedup pytest
242
234
import difflib
243
235
244
236
try :
245
237
left_formatting = pprint .pformat (left ).splitlines ()
246
238
right_formatting = pprint .pformat (right ).splitlines ()
247
- explanation = [u ( "Full diff:" ) ]
239
+ explanation = [u"Full diff:" ]
248
240
except Exception :
249
241
# hack: PrettyPrinter.pformat() in python 2 fails when formatting items that can't be sorted(), ie, calling
250
242
# sorted() on a list would raise. See issue #718.
251
243
# As a workaround, the full diff is generated by using the repr() string of each item of each container.
252
244
left_formatting = sorted (repr (x ) for x in left )
253
245
right_formatting = sorted (repr (x ) for x in right )
254
- explanation = [u ( "Full diff (fallback to calling repr on each item):" ) ]
246
+ explanation = [u"Full diff (fallback to calling repr on each item):" ]
255
247
explanation .extend (
256
248
line .strip () for line in difflib .ndiff (left_formatting , right_formatting )
257
249
)
@@ -262,16 +254,16 @@ def _compare_eq_sequence(left, right, verbose=False):
262
254
explanation = []
263
255
for i in range (min (len (left ), len (right ))):
264
256
if left [i ] != right [i ]:
265
- explanation += [u ( "At index %s diff: %r != %r" ) % (i , left [i ], right [i ])]
257
+ explanation += [u"At index %s diff: %r != %r" % (i , left [i ], right [i ])]
266
258
break
267
259
if len (left ) > len (right ):
268
260
explanation += [
269
- u ( "Left contains more items, first extra item: %s" )
261
+ u"Left contains more items, first extra item: %s"
270
262
% py .io .saferepr (left [len (right )])
271
263
]
272
264
elif len (left ) < len (right ):
273
265
explanation += [
274
- u ( "Right contains more items, first extra item: %s" )
266
+ u"Right contains more items, first extra item: %s"
275
267
% py .io .saferepr (right [len (left )])
276
268
]
277
269
return explanation
@@ -282,11 +274,11 @@ def _compare_eq_set(left, right, verbose=False):
282
274
diff_left = left - right
283
275
diff_right = right - left
284
276
if diff_left :
285
- explanation .append (u ( "Extra items in the left set:" ) )
277
+ explanation .append (u"Extra items in the left set:" )
286
278
for item in diff_left :
287
279
explanation .append (py .io .saferepr (item ))
288
280
if diff_right :
289
- explanation .append (u ( "Extra items in the right set:" ) )
281
+ explanation .append (u"Extra items in the right set:" )
290
282
for item in diff_right :
291
283
explanation .append (py .io .saferepr (item ))
292
284
return explanation
@@ -297,26 +289,26 @@ def _compare_eq_dict(left, right, verbose=False):
297
289
common = set (left ).intersection (set (right ))
298
290
same = {k : left [k ] for k in common if left [k ] == right [k ]}
299
291
if same and verbose < 2 :
300
- explanation += [u ( "Omitting %s identical items, use -vv to show" ) % len (same )]
292
+ explanation += [u"Omitting %s identical items, use -vv to show" % len (same )]
301
293
elif same :
302
- explanation += [u ( "Common items:" ) ]
294
+ explanation += [u"Common items:" ]
303
295
explanation += pprint .pformat (same ).splitlines ()
304
296
diff = {k for k in common if left [k ] != right [k ]}
305
297
if diff :
306
- explanation += [u ( "Differing items:" ) ]
298
+ explanation += [u"Differing items:" ]
307
299
for k in diff :
308
300
explanation += [
309
301
py .io .saferepr ({k : left [k ]}) + " != " + py .io .saferepr ({k : right [k ]})
310
302
]
311
303
extra_left = set (left ) - set (right )
312
304
if extra_left :
313
- explanation .append (u ( "Left contains more items:" ) )
305
+ explanation .append (u"Left contains more items:" )
314
306
explanation .extend (
315
307
pprint .pformat ({k : left [k ] for k in extra_left }).splitlines ()
316
308
)
317
309
extra_right = set (right ) - set (left )
318
310
if extra_right :
319
- explanation .append (u ( "Right contains more items:" ) )
311
+ explanation .append (u"Right contains more items:" )
320
312
explanation .extend (
321
313
pprint .pformat ({k : right [k ] for k in extra_right }).splitlines ()
322
314
)
@@ -329,14 +321,14 @@ def _notin_text(term, text, verbose=False):
329
321
tail = text [index + len (term ) :]
330
322
correct_text = head + tail
331
323
diff = _diff_text (correct_text , text , verbose )
332
- newdiff = [u ( "%s is contained here:" ) % py .io .saferepr (term , maxsize = 42 )]
324
+ newdiff = [u"%s is contained here:" % py .io .saferepr (term , maxsize = 42 )]
333
325
for line in diff :
334
- if line .startswith (u ( "Skipping" ) ):
326
+ if line .startswith (u"Skipping" ):
335
327
continue
336
- if line .startswith (u ( "- " ) ):
328
+ if line .startswith (u"- " ):
337
329
continue
338
- if line .startswith (u ( "+ " ) ):
339
- newdiff .append (u ( " " ) + line [2 :])
330
+ if line .startswith (u"+ " ):
331
+ newdiff .append (u" " + line [2 :])
340
332
else :
341
333
newdiff .append (line )
342
334
return newdiff
0 commit comments