diff -r 2eea364c2863 Lib/test/test_binascii.py --- a/Lib/test/test_binascii.py Sun Jan 18 17:39:32 2015 +0100 +++ b/Lib/test/test_binascii.py Mon Jan 19 23:55:43 2015 -0600 @@ -162,7 +162,9 @@ 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 -r 2eea364c2863 Modules/binascii.c --- a/Modules/binascii.c Sun Jan 18 17:39:32 2015 +0100 +++ b/Modules/binascii.c Mon Jan 19 23:55:43 2015 -0600 @@ -1147,6 +1147,20 @@ 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 @@ 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 @@ 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 -r 2eea364c2863 Modules/clinic/binascii.c.h --- a/Modules/clinic/binascii.c.h Sun Jan 18 17:39:32 2015 +0100 +++ b/Modules/clinic/binascii.c.h Mon Jan 19 23:55:43 2015 -0600 @@ -367,6 +367,40 @@ 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 @@ 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 @@ return return_value; } -/*[clinic end generated code: output=68e2bcc6956b6213 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=e46d29f8c9adae7e input=a9049054013a1b77]*/