[Python-checkins] python/dist/src/Modules _codecsmodule.c, 2.17, 2.18

lemburg at users.sourceforge.net lemburg at users.sourceforge.net
Sat Jul 10 14:06:13 CEST 2004


Update of /cvsroot/python/python/dist/src/Modules
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9544/Modules
Modified Files:
	_codecsmodule.c 
Log Message:
Add generic codecs.encode() and .decode() APIs that don't impose
any restriction on the return type (like unicode.encode() et al. do).
Index: _codecsmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/_codecsmodule.c,v
retrieving revision 2.17
retrieving revision 2.18
diff -C2 -d -r2.17 -r2.18
*** _codecsmodule.c	4 Feb 2003 19:35:03 -0000	2.17
--- _codecsmodule.c	10 Jul 2004 12:06:09 -0000	2.18
***************
*** 48,52 ****
 
 static
! PyObject *codecregister(PyObject *self, PyObject *args)
 {
 PyObject *search_function;
--- 48,52 ----
 
 static
! PyObject *codec_register(PyObject *self, PyObject *args)
 {
 PyObject *search_function;
***************
*** 72,76 ****
 
 static
! PyObject *codeclookup(PyObject *self, PyObject *args)
 {
 char *encoding;
--- 72,76 ----
 
 static
! PyObject *codec_lookup(PyObject *self, PyObject *args)
 {
 char *encoding;
***************
*** 85,88 ****
--- 85,154 ----
 }
 
+ PyDoc_STRVAR(encode__doc__,
+ "encode(obj, [encoding[,errors]]) -> object\n\
+ \n\
+ Encodes obj using the codec registered for encoding. encoding defaults\n\
+ to the default encoding. errors may be given to set a different error\n\
+ handling scheme. Default is 'strict' meaning that encoding errors raise\n\
+ a ValueError. Other possible values are 'ignore', 'replace' and\n\
+ 'xmlcharrefreplace' as well as any other name registered with\n\
+ codecs.register_error that can handle ValueErrors.");
+ 
+ static PyObject *
+ codec_encode(PyObject *self, PyObject *args)
+ {
+ char *encoding = NULL;
+ char *errors = NULL;
+ PyObject *v;
+ 
+ if (!PyArg_ParseTuple(args, "O|ss:encode", &v, &encoding, &errors))
+ return NULL;
+ 
+ if (encoding == NULL)
+ 	encoding = PyUnicode_GetDefaultEncoding();
+ 
+ /* Encode via the codec registry */
+ v = PyCodec_Encode(v, encoding, errors);
+ if (v == NULL)
+ goto onError;
+ return v;
+ 
+ onError:
+ return NULL;
+ }
+ 
+ PyDoc_STRVAR(decode__doc__,
+ "decode(obj, [encoding[,errors]]) -> object\n\
+ \n\
+ Decodes obj using the codec registered for encoding. encoding defaults\n\
+ to the default encoding. errors may be given to set a different error\n\
+ handling scheme. Default is 'strict' meaning that encoding errors raise\n\
+ a ValueError. Other possible values are 'ignore' and 'replace'\n\
+ as well as any other name registerd with codecs.register_error that is\n\
+ able to handle ValueErrors.");
+ 
+ static PyObject *
+ codec_decode(PyObject *self, PyObject *args)
+ {
+ char *encoding = NULL;
+ char *errors = NULL;
+ PyObject *v;
+ 
+ if (!PyArg_ParseTuple(args, "O|ss:decode", &v, &encoding, &errors))
+ return NULL;
+ 
+ if (encoding == NULL)
+ 	encoding = PyUnicode_GetDefaultEncoding();
+ 
+ /* Decode via the codec registry */
+ v = PyCodec_Decode(v, encoding, errors);
+ if (v == NULL)
+ goto onError;
+ return v;
+ 
+ onError:
+ return NULL;
+ }
+ 
 /* --- Helpers ------------------------------------------------------------ */
 
***************
*** 766,773 ****
 
 static PyMethodDef _codecs_functions[] = {
! {"register",		codecregister,			METH_VARARGS,
 register__doc__},
! {"lookup",			codeclookup, 			METH_VARARGS,
 lookup__doc__},
 {"escape_encode",		escape_encode,			METH_VARARGS},
 {"escape_decode",		escape_decode,			METH_VARARGS},
--- 832,841 ----
 
 static PyMethodDef _codecs_functions[] = {
! {"register",		codec_register,			METH_VARARGS,
 register__doc__},
! {"lookup",			codec_lookup, 			METH_VARARGS,
 lookup__doc__},
+ {"encode",			codec_encode,			METH_VARARGS},
+ {"decode",			codec_decode,			METH_VARARGS},
 {"escape_encode",		escape_encode,			METH_VARARGS},
 {"escape_decode",		escape_decode,			METH_VARARGS},


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /