self.assertRaises(ValueError, self.type2test.fromhex, '\x00')
self.assertRaises(ValueError, self.type2test.fromhex, '12 \x00 34')
+ # For odd number of character(s)
+ for value in ("a", "aaa", "deadbee"):
+ with self.assertRaises(ValueError) as cm:
+ self.type2test.fromhex(value)
+ self.assertIn("fromhex() arg must contain an even number of hexadecimal digits", str(cm.exception))
+
for data, pos in (
# invalid first hexadecimal character
('12 x4 56', 3),
bot = _PyLong_DigitValue[*str];
if (bot >= 16) {
- invalid_char = str - PyUnicode_1BYTE_DATA(string);
+ /* Check if we had a second digit */
+ if (str >= end){
+ invalid_char = -1;
+ } else {
+ invalid_char = str - PyUnicode_1BYTE_DATA(string);
+ }
goto error;
}
str++;
return _PyBytesWriter_Finish(&writer, buf);
error:
- PyErr_Format(PyExc_ValueError,
- "non-hexadecimal number found in "
- "fromhex() arg at position %zd", invalid_char);
+ if (invalid_char == -1) {
+ PyErr_SetString(PyExc_ValueError,
+ "fromhex() arg must contain an even number of hexadecimal digits");
+ } else {
+ PyErr_Format(PyExc_ValueError,
+ "non-hexadecimal number found in "
+ "fromhex() arg at position %zd", invalid_char);
+ }
_PyBytesWriter_Dealloc(&writer);
return NULL;
}