Skip to content

Commit

Permalink
Add tests and update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
srinivasreddy committed Dec 9, 2024
1 parent 3588d18 commit 504f4ed
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2541,12 +2541,10 @@ _PyBytes_FromHex(PyObject *string, int use_bytearray)
}
str++;

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

bot = _PyLong_DigitValue[*str];
Expand All @@ -2562,9 +2560,14 @@ _PyBytes_FromHex(PyObject *string, int use_bytearray)
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 == -2) {
PyErr_SetString(PyExc_ValueError,
"fromhex() arg must be of even length");
} else {
PyErr_Format(PyExc_ValueError,
"non-hexadecimal number found in "
"fromhex() arg at position %zd", invalid_char);
}
_PyBytesWriter_Dealloc(&writer);
return NULL;
}
Expand Down

0 comments on commit 504f4ed

Please sign in to comment.