@@ -333,29 +333,45 @@ def generate_validator_testcases(valid):
333
333
),
334
334
'fail' : (('aardvark' , ValueError ),
335
335
)
336
- },
337
- {'validator' : _validate_linestyle , # NB: case-insensitive
338
- 'success' : (('-' , '-' ), ('solid' , 'solid' ),
339
- ('--' , '--' ), ('dashed' , 'dashed' ),
340
- ('-.' , '-.' ), ('dashdot' , 'dashdot' ),
341
- (':' , ':' ), ('dotted' , 'dotted' ),
342
- ('' , '' ), (' ' , ' ' ),
343
- ('None' , 'none' ), ('none' , 'none' ),
344
- ('DoTtEd' , 'dotted' ),
345
- (['1.23' , '4.56' ], (None , [1.23 , 4.56 ])),
346
- ([1.23 , 456 ], (None , [1.23 , 456.0 ])),
347
- ([1 , 2 , 3 , 4 ], (None , [1.0 , 2.0 , 3.0 , 4.0 ])),
348
- ),
349
- 'fail' : (('aardvark' , ValueError ), # not a valid string
350
- ((None , [1 , 2 ]), ValueError ), # (offset, dashes) is not OK
351
- ((0 , [1 , 2 ]), ValueError ), # idem
352
- ((- 1 , [1 , 2 ]), ValueError ), # idem
353
- ([1 , 2 , 3 ], ValueError ), # not a sequence of even length
354
- (1.23 , ValueError ) # not a sequence
355
- )
356
336
}
357
337
)
358
338
339
+ # The behavior of _validate_linestyle depends on the version of Python.
340
+ # ASCII-compliant bytes arguments should pass on Python 2 because of the
341
+ # automatic conversion between bytes and strings. Python 3 does not
342
+ # perform such a conversion, so the same cases should raise an exception.
343
+ #
344
+ # Common cases:
345
+ ls_test = {'validator' : _validate_linestyle ,
346
+ 'success' : (('-' , '-' ), ('solid' , 'solid' ),
347
+ ('--' , '--' ), ('dashed' , 'dashed' ),
348
+ ('-.' , '-.' ), ('dashdot' , 'dashdot' ),
349
+ (':' , ':' ), ('dotted' , 'dotted' ),
350
+ ('' , '' ), (' ' , ' ' ),
351
+ ('None' , 'none' ), ('none' , 'none' ),
352
+ ('DoTtEd' , 'dotted' ), # case-insensitive
353
+ (['1.23' , '4.56' ], (None , [1.23 , 4.56 ])),
354
+ ([1.23 , 456 ], (None , [1.23 , 456.0 ])),
355
+ ([1 , 2 , 3 , 4 ], (None , [1.0 , 2.0 , 3.0 , 4.0 ])),
356
+ ),
357
+ 'fail' : (('aardvark' , ValueError ), # not a valid string
358
+ ('dotted' .encode ('utf-16' ), ValueError ), # even on PY2
359
+ ((None , [1 , 2 ]), ValueError ), # (offset, dashes) != OK
360
+ ((0 , [1 , 2 ]), ValueError ), # idem
361
+ ((- 1 , [1 , 2 ]), ValueError ), # idem
362
+ ([1 , 2 , 3 ], ValueError ), # sequence with odd length
363
+ (1.23 , ValueError ), # not a sequence
364
+ )
365
+ }
366
+ # Add some cases of bytes arguments that Python 2 can convert silently:
367
+ ls_bytes_args = (b'dotted' , 'dotted' .encode ('ascii' ))
368
+ if six .PY3 :
369
+ ls_test ['fail' ] += tuple ((arg , ValueError ) for arg in ls_bytes_args )
370
+ else :
371
+ ls_test ['success' ] += tuple ((arg , 'dotted' ) for arg in ls_bytes_args )
372
+ # Update the validation test sequence.
373
+ validation_tests += (ls_test ,)
374
+
359
375
for validator_dict in validation_tests :
360
376
validator = validator_dict ['validator' ]
361
377
if valid :
0 commit comments