MCPcopy Index your code
hub / github.com/python/cpython / test_fromhex

Method test_fromhex

Lib/test/test_bytes.py:443–509  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

441 self.assertNotIn(f(b"abd"), b)
442
443 def test_fromhex(self):
444 self.assertRaises(TypeError, self.type2test.fromhex)
445 self.assertRaises(TypeError, self.type2test.fromhex, 1)
446 self.assertEqual(self.type2test.fromhex(''), self.type2test())
447 b = bytearray([0x1a, 0x2b, 0x30])
448 self.assertEqual(self.type2test.fromhex('1a2B30'), b)
449 self.assertEqual(self.type2test.fromhex(' 1A 2B 30 '), b)
450
451 # check that ASCII whitespace is ignored
452 self.assertEqual(self.type2test.fromhex(' 1A\n2B\t30\v'), b)
453 self.assertEqual(self.type2test.fromhex(b' 1A\n2B\t30\v'), b)
454 for c in "\x09\x0A\x0B\x0C\x0D\x20":
455 self.assertEqual(self.type2test.fromhex(c), self.type2test())
456 for c in "\x1C\x1D\x1E\x1F\x85\xa0\u2000\u2002\u2028":
457 self.assertRaises(ValueError, self.type2test.fromhex, c)
458
459 # Check that we can parse bytes and bytearray
460 tests = [
461 ("bytes", bytes),
462 ("bytearray", bytearray),
463 ("memoryview", memoryview),
464 ("array.array", lambda bs: array.array('B', bs)),
465 ]
466 for name, factory in tests:
467 with self.subTest(name=name):
468 self.assertEqual(self.type2test.fromhex(factory(b' 1A 2B 30 ')), b)
469
470 # Invalid bytes are rejected
471 for u8 in b"\0\x1C\x1D\x1E\x1F\x85\xa0":
472 b = bytes([30, 31, u8])
473 self.assertRaises(ValueError, self.type2test.fromhex, b)
474
475 self.assertEqual(self.type2test.fromhex('0000'), b'\0\0')
476 with self.assertRaisesRegex(
477 TypeError,
478 r'fromhex\(\) argument must be str or bytes-like, not tuple',
479 ):
480 self.type2test.fromhex(())
481 self.assertRaises(ValueError, self.type2test.fromhex, 'a')
482 self.assertRaises(ValueError, self.type2test.fromhex, 'rt')
483 self.assertRaises(ValueError, self.type2test.fromhex, '1a b cd')
484 self.assertRaises(ValueError, self.type2test.fromhex, '\x00')
485 self.assertRaises(ValueError, self.type2test.fromhex, '12 \x00 34')
486
487 # For odd number of character(s)
488 for value in ("a", "aaa", "deadbee"):
489 with self.assertRaises(ValueError) as cm:
490 self.type2test.fromhex(value)
491 self.assertIn("fromhex() arg must contain an even number of hexadecimal digits", str(cm.exception))
492 for value, position in (("a ", 1), (" aa a ", 5), (" aa a a ", 5)):
493 with self.assertRaises(ValueError) as cm:
494 self.type2test.fromhex(value)
495 self.assertIn(f"non-hexadecimal number found in fromhex() arg at position {position}", str(cm.exception))
496
497 for data, pos in (
498 # invalid first hexadecimal character
499 ('12 x4 56', 3),
500 # invalid second hexadecimal character

Callers

nothing calls this directly

Calls 8

strFunction · 0.85
type2testMethod · 0.80
assertRaisesRegexMethod · 0.80
assertInMethod · 0.80
factoryFunction · 0.50
assertRaisesMethod · 0.45
assertEqualMethod · 0.45
subTestMethod · 0.45

Tested by

no test coverage detected