Skip to content

Commit 504f4ed

Browse files
committed
Add tests and update logic
1 parent 3588d18 commit 504f4ed

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

Objects/bytesobject.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2541,12 +2541,10 @@ _PyBytes_FromHex(PyObject *string, int use_bytearray)
25412541
}
25422542
str++;
25432543

2544-
/* Check if we have a second digit*/
2544+
/* Check if we have a second digit */
25452545
if (str >= end) {
2546-
PyErr_SetString(PyExc_ValueError,
2547-
"fromhex() arg must be of even length");
2548-
_PyBytesWriter_Dealloc(&writer);
2549-
return NULL;
2546+
invalid_char = -2;
2547+
goto error;
25502548
}
25512549

25522550
bot = _PyLong_DigitValue[*str];
@@ -2562,9 +2560,14 @@ _PyBytes_FromHex(PyObject *string, int use_bytearray)
25622560
return _PyBytesWriter_Finish(&writer, buf);
25632561

25642562
error:
2565-
PyErr_Format(PyExc_ValueError,
2566-
"non-hexadecimal number found in "
2567-
"fromhex() arg at position %zd", invalid_char);
2563+
if (invalid_char == -2) {
2564+
PyErr_SetString(PyExc_ValueError,
2565+
"fromhex() arg must be of even length");
2566+
} else {
2567+
PyErr_Format(PyExc_ValueError,
2568+
"non-hexadecimal number found in "
2569+
"fromhex() arg at position %zd", invalid_char);
2570+
}
25682571
_PyBytesWriter_Dealloc(&writer);
25692572
return NULL;
25702573
}

0 commit comments

Comments
 (0)