Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3f41974

Browse files
committed
Add generic codecs.encode() and .decode() APIs that don't impose
any restriction on the return type (like unicode.encode() et al. do).
1 parent 126b44c commit 3f41974

2 files changed

Lines changed: 83 additions & 5 deletions

File tree

Lib/test/test_codecs.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,14 +336,24 @@ class CodecTest(unittest.TestCase):
336336
def test_builtin(self):
337337
self.assertEquals(unicode("python.org", "idna"), u"python.org")
338338

339+
class CodecsModuleTest(unittest.TestCase):
340+
341+
def test_decode(self):
342+
self.assertEquals(codecs.decode('\xe4\xf6\xfc', 'latin-1'),
343+
u'\xe4\xf6\xfc')
344+
def test_encode(self):
345+
self.assertEquals(codecs.encode(u'\xe4\xf6\xfc', 'latin-1'),
346+
'\xe4\xf6\xfc')
347+
339348
def test_main():
340349
test_support.run_unittest(
341350
UTF16Test,
342351
EscapeDecodeTest,
343352
RecodingTest,
344353
PunycodeTest,
345354
NameprepTest,
346-
CodecTest
355+
CodecTest,
356+
CodecsModuleTest
347357
)
348358

349359

Modules/_codecsmodule.c

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ one argument, the encoding name in all lower case letters, and return\n\
4747
a tuple of functions (encoder, decoder, stream_reader, stream_writer).");
4848

4949
static
50-
PyObject *codecregister(PyObject *self, PyObject *args)
50+
PyObject *codec_register(PyObject *self, PyObject *args)
5151
{
5252
PyObject *search_function;
5353

@@ -71,7 +71,7 @@ Looks up a codec tuple in the Python codec registry and returns\n\
7171
a tuple of functions.");
7272

7373
static
74-
PyObject *codeclookup(PyObject *self, PyObject *args)
74+
PyObject *codec_lookup(PyObject *self, PyObject *args)
7575
{
7676
char *encoding;
7777

@@ -84,6 +84,72 @@ PyObject *codeclookup(PyObject *self, PyObject *args)
8484
return NULL;
8585
}
8686

87+
PyDoc_STRVAR(encode__doc__,
88+
"encode(obj, [encoding[,errors]]) -> object\n\
89+
\n\
90+
Encodes obj using the codec registered for encoding. encoding defaults\n\
91+
to the default encoding. errors may be given to set a different error\n\
92+
handling scheme. Default is 'strict' meaning that encoding errors raise\n\
93+
a ValueError. Other possible values are 'ignore', 'replace' and\n\
94+
'xmlcharrefreplace' as well as any other name registered with\n\
95+
codecs.register_error that can handle ValueErrors.");
96+
97+
static PyObject *
98+
codec_encode(PyObject *self, PyObject *args)
99+
{
100+
char *encoding = NULL;
101+
char *errors = NULL;
102+
PyObject *v;
103+
104+
if (!PyArg_ParseTuple(args, "O|ss:encode", &v, &encoding, &errors))
105+
return NULL;
106+
107+
if (encoding == NULL)
108+
encoding = PyUnicode_GetDefaultEncoding();
109+
110+
/* Encode via the codec registry */
111+
v = PyCodec_Encode(v, encoding, errors);
112+
if (v == NULL)
113+
goto onError;
114+
return v;
115+
116+
onError:
117+
return NULL;
118+
}
119+
120+
PyDoc_STRVAR(decode__doc__,
121+
"decode(obj, [encoding[,errors]]) -> object\n\
122+
\n\
123+
Decodes obj using the codec registered for encoding. encoding defaults\n\
124+
to the default encoding. errors may be given to set a different error\n\
125+
handling scheme. Default is 'strict' meaning that encoding errors raise\n\
126+
a ValueError. Other possible values are 'ignore' and 'replace'\n\
127+
as well as any other name registerd with codecs.register_error that is\n\
128+
able to handle ValueErrors.");
129+
130+
static PyObject *
131+
codec_decode(PyObject *self, PyObject *args)
132+
{
133+
char *encoding = NULL;
134+
char *errors = NULL;
135+
PyObject *v;
136+
137+
if (!PyArg_ParseTuple(args, "O|ss:decode", &v, &encoding, &errors))
138+
return NULL;
139+
140+
if (encoding == NULL)
141+
encoding = PyUnicode_GetDefaultEncoding();
142+
143+
/* Decode via the codec registry */
144+
v = PyCodec_Decode(v, encoding, errors);
145+
if (v == NULL)
146+
goto onError;
147+
return v;
148+
149+
onError:
150+
return NULL;
151+
}
152+
87153
/* --- Helpers ------------------------------------------------------------ */
88154

89155
static
@@ -765,10 +831,12 @@ static PyObject *lookup_error(PyObject *self, PyObject *args)
765831
/* --- Module API --------------------------------------------------------- */
766832

767833
static PyMethodDef _codecs_functions[] = {
768-
{"register", codecregister, METH_VARARGS,
834+
{"register", codec_register, METH_VARARGS,
769835
register__doc__},
770-
{"lookup", codeclookup, METH_VARARGS,
836+
{"lookup", codec_lookup, METH_VARARGS,
771837
lookup__doc__},
838+
{"encode", codec_encode, METH_VARARGS},
839+
{"decode", codec_decode, METH_VARARGS},
772840
{"escape_encode", escape_encode, METH_VARARGS},
773841
{"escape_decode", escape_decode, METH_VARARGS},
774842
#ifdef Py_USING_UNICODE

0 commit comments

Comments
 (0)