From 504f4edc0ad2214ad309235a9bd1909879de9bf0 Mon Sep 17 00:00:00 2001 From: Srinivas Reddy Thatiparthy Date: Mon, 9 Dec 2024 20:38:55 +0530 Subject: [PATCH] Add tests and update logic --- Objects/bytesobject.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index 30f232e2dbb231..832939e5535ae1 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -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]; @@ -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; }