Issue #21488: Add support of keyword arguments for codecs.encode and codecs.decode

This commit is contained in:
Victor Stinner 2014-05-14 17:13:14 +02:00
parent f7667e3989
commit a57dfd033c
2 changed files with 22 additions and 6 deletions

View File

@ -1596,6 +1596,12 @@ class CodecsModuleTest(unittest.TestCase):
self.assertEqual(codecs.decode(b'abc'), 'abc') self.assertEqual(codecs.decode(b'abc'), 'abc')
self.assertRaises(UnicodeDecodeError, codecs.decode, b'\xff', 'ascii') self.assertRaises(UnicodeDecodeError, codecs.decode, b'\xff', 'ascii')
# test keywords
self.assertEqual(codecs.decode(obj=b'\xe4\xf6\xfc', encoding='latin-1'),
'\xe4\xf6\xfc')
self.assertEqual(codecs.decode(b'[\xff]', 'ascii', errors='ignore'),
'[]')
def test_encode(self): def test_encode(self):
self.assertEqual(codecs.encode('\xe4\xf6\xfc', 'latin-1'), self.assertEqual(codecs.encode('\xe4\xf6\xfc', 'latin-1'),
b'\xe4\xf6\xfc') b'\xe4\xf6\xfc')
@ -1604,6 +1610,12 @@ class CodecsModuleTest(unittest.TestCase):
self.assertEqual(codecs.encode('abc'), b'abc') self.assertEqual(codecs.encode('abc'), b'abc')
self.assertRaises(UnicodeEncodeError, codecs.encode, '\xffff', 'ascii') self.assertRaises(UnicodeEncodeError, codecs.encode, '\xffff', 'ascii')
# test keywords
self.assertEqual(codecs.encode(obj='\xe4\xf6\xfc', encoding='latin-1'),
b'\xe4\xf6\xfc')
self.assertEqual(codecs.encode('[\xff]', 'ascii', errors='ignore'),
b'[]')
def test_register(self): def test_register(self):
self.assertRaises(TypeError, codecs.register) self.assertRaises(TypeError, codecs.register)
self.assertRaises(TypeError, codecs.register, 42) self.assertRaises(TypeError, codecs.register, 42)

View File

@ -89,13 +89,15 @@ a ValueError. Other possible values are 'ignore', 'replace' and\n\
codecs.register_error that can handle ValueErrors."); codecs.register_error that can handle ValueErrors.");
static PyObject * static PyObject *
codec_encode(PyObject *self, PyObject *args) codec_encode(PyObject *self, PyObject *args, PyObject *kwargs)
{ {
static char *kwlist[] = {"obj", "encoding", "errors", NULL};
const char *encoding = NULL; const char *encoding = NULL;
const char *errors = NULL; const char *errors = NULL;
PyObject *v; PyObject *v;
if (!PyArg_ParseTuple(args, "O|ss:encode", &v, &encoding, &errors)) if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|ss:encode", kwlist,
&v, &encoding, &errors))
return NULL; return NULL;
if (encoding == NULL) if (encoding == NULL)
@ -116,13 +118,15 @@ as well as any other name registered with codecs.register_error that is\n\
able to handle ValueErrors."); able to handle ValueErrors.");
static PyObject * static PyObject *
codec_decode(PyObject *self, PyObject *args) codec_decode(PyObject *self, PyObject *args, PyObject *kwargs)
{ {
static char *kwlist[] = {"obj", "encoding", "errors", NULL};
const char *encoding = NULL; const char *encoding = NULL;
const char *errors = NULL; const char *errors = NULL;
PyObject *v; PyObject *v;
if (!PyArg_ParseTuple(args, "O|ss:decode", &v, &encoding, &errors)) if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|ss:decode", kwlist,
&v, &encoding, &errors))
return NULL; return NULL;
if (encoding == NULL) if (encoding == NULL)
@ -1120,9 +1124,9 @@ static PyMethodDef _codecs_functions[] = {
register__doc__}, register__doc__},
{"lookup", codec_lookup, METH_VARARGS, {"lookup", codec_lookup, METH_VARARGS,
lookup__doc__}, lookup__doc__},
{"encode", codec_encode, METH_VARARGS, {"encode", (PyCFunction)codec_encode, METH_VARARGS|METH_KEYWORDS,
encode__doc__}, encode__doc__},
{"decode", codec_decode, METH_VARARGS, {"decode", (PyCFunction)codec_decode, METH_VARARGS|METH_KEYWORDS,
decode__doc__}, decode__doc__},
{"escape_encode", escape_encode, METH_VARARGS}, {"escape_encode", escape_encode, METH_VARARGS},
{"escape_decode", escape_decode, METH_VARARGS}, {"escape_decode", escape_decode, METH_VARARGS},