diff --git a/Lib/test/test_binascii.py b/Lib/test/test_binascii.py index 04d8f9d6804..22d53af1905 100644 --- a/Lib/test/test_binascii.py +++ b/Lib/test/test_binascii.py @@ -162,7 +162,9 @@ class BinASCIITest(unittest.TestCase): self.assertRaises(binascii.Error, binascii.a2b_hex, t[:-1]) self.assertRaises(binascii.Error, binascii.a2b_hex, t[:-1] + b'q') - self.assertEqual(binascii.hexlify(b'a'), b'61') + # Confirm that b2a_hex == hexlify and a2b_hex == unhexlify + self.assertEqual(binascii.hexlify(self.type2test(s)), t) + self.assertEqual(binascii.unhexlify(self.type2test(t)), u) def test_qp(self): # A test for SF bug 534347 (segfaults without the proper fix) diff --git a/Modules/binascii.c b/Modules/binascii.c index 42d08de373f..86b63bb4f54 100644 --- a/Modules/binascii.c +++ b/Modules/binascii.c @@ -1147,6 +1147,20 @@ binascii_b2a_hex_impl(PyModuleDef *module, Py_buffer *data) return retval; } +/*[clinic input] +binascii.hexlify = binascii.b2a_hex + +Hexadecimal representation of binary data. + +The return value is a bytes object. +[clinic start generated code]*/ + +static PyObject * +binascii_hexlify_impl(PyModuleDef *module, Py_buffer *data) +/*[clinic end generated code: output=6098440091fb61dc input=2e3afae7f083f061]*/ +{ + return binascii_b2a_hex_impl(module, data); +} static int to_int(int c) @@ -1221,6 +1235,21 @@ binascii_a2b_hex_impl(PyModuleDef *module, Py_buffer *hexstr) return NULL; } +/*[clinic input] +binascii.unhexlify = binascii.a2b_hex + +Binary data of hexadecimal representation. + +hexstr must contain an even number of hex digits (upper or lower case). +[clinic start generated code]*/ + +static PyObject * +binascii_unhexlify_impl(PyModuleDef *module, Py_buffer *hexstr) +/*[clinic end generated code: output=17cec7544499803e input=dd8c012725f462da]*/ +{ + return binascii_a2b_hex_impl(module, hexstr); +} + static int table_hex[128] = { -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, @@ -1535,10 +1564,8 @@ static struct PyMethodDef binascii_module_methods[] = { BINASCII_B2A_HQX_METHODDEF BINASCII_A2B_HEX_METHODDEF BINASCII_B2A_HEX_METHODDEF - {"unhexlify", (PyCFunction)binascii_a2b_hex, METH_VARARGS, - binascii_a2b_hex__doc__}, - {"hexlify", (PyCFunction)binascii_b2a_hex, METH_VARARGS, - binascii_b2a_hex__doc__}, + BINASCII_HEXLIFY_METHODDEF + BINASCII_UNHEXLIFY_METHODDEF BINASCII_RLECODE_HQX_METHODDEF BINASCII_RLEDECODE_HQX_METHODDEF BINASCII_CRC_HQX_METHODDEF diff --git a/Modules/clinic/binascii.c.h b/Modules/clinic/binascii.c.h index 6147be91795..b2b6e6b4faf 100644 --- a/Modules/clinic/binascii.c.h +++ b/Modules/clinic/binascii.c.h @@ -367,6 +367,40 @@ exit: return return_value; } +PyDoc_STRVAR(binascii_hexlify__doc__, +"hexlify($module, data, /)\n" +"--\n" +"\n" +"Hexadecimal representation of binary data.\n" +"\n" +"The return value is a bytes object."); + +#define BINASCII_HEXLIFY_METHODDEF \ + {"hexlify", (PyCFunction)binascii_hexlify, METH_VARARGS, binascii_hexlify__doc__}, + +static PyObject * +binascii_hexlify_impl(PyModuleDef *module, Py_buffer *data); + +static PyObject * +binascii_hexlify(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer data = {NULL, NULL}; + + if (!PyArg_ParseTuple(args, + "y*:hexlify", + &data)) + goto exit; + return_value = binascii_hexlify_impl(module, &data); + +exit: + /* Cleanup for data */ + if (data.obj) + PyBuffer_Release(&data); + + return return_value; +} + PyDoc_STRVAR(binascii_a2b_hex__doc__, "a2b_hex($module, hexstr, /)\n" "--\n" @@ -402,6 +436,40 @@ exit: return return_value; } +PyDoc_STRVAR(binascii_unhexlify__doc__, +"unhexlify($module, hexstr, /)\n" +"--\n" +"\n" +"Binary data of hexadecimal representation.\n" +"\n" +"hexstr must contain an even number of hex digits (upper or lower case)."); + +#define BINASCII_UNHEXLIFY_METHODDEF \ + {"unhexlify", (PyCFunction)binascii_unhexlify, METH_VARARGS, binascii_unhexlify__doc__}, + +static PyObject * +binascii_unhexlify_impl(PyModuleDef *module, Py_buffer *hexstr); + +static PyObject * +binascii_unhexlify(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_buffer hexstr = {NULL, NULL}; + + if (!PyArg_ParseTuple(args, + "O&:unhexlify", + ascii_buffer_converter, &hexstr)) + goto exit; + return_value = binascii_unhexlify_impl(module, &hexstr); + +exit: + /* Cleanup for hexstr */ + if (hexstr.obj) + PyBuffer_Release(&hexstr); + + return return_value; +} + PyDoc_STRVAR(binascii_a2b_qp__doc__, "a2b_qp($module, /, data, header=False)\n" "--\n" @@ -475,4 +543,4 @@ exit: return return_value; } -/*[clinic end generated code: output=53cd6b379c745220 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=771126f8f53e84e7 input=a9049054013a1b77]*/