[Python-checkins] python/dist/src/Objects stringobject.c, 2.219, 2.220 unicodeobject.c, 2.213, 2.214

lemburg at users.sourceforge.net lemburg at users.sourceforge.net
Thu Jul 8 19:57:36 CEST 2004


Update of /cvsroot/python/python/dist/src/Objects
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10211/Objects

Modified Files:
	stringobject.c unicodeobject.c 
Log Message:
Allow string and unicode return types from .encode()/.decode()
methods on string and unicode objects. Added unicode.decode()
which was missing for no apparent reason.



Index: stringobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/stringobject.c,v
retrieving revision 2.219
retrieving revision 2.220
diff -C2 -d -r2.219 -r2.220
*** stringobject.c	27 Jun 2004 17:24:49 -0000	2.219
--- stringobject.c	8 Jul 2004 17:57:31 -0000	2.220
***************
*** 2674,2680 ****
      char *encoding = NULL;
      char *errors = NULL;
      if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors))
          return NULL;
!     return PyString_AsEncodedObject((PyObject *)self, encoding, errors);
  }
  
--- 2674,2691 ----
      char *encoding = NULL;
      char *errors = NULL;
+     PyObject *v;
+     
      if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors))
          return NULL;
!     v = PyString_AsEncodedObject((PyObject *)self, encoding, errors);
!     if (!PyString_Check(v) && !PyUnicode_Check(v)) {
!         PyErr_Format(PyExc_TypeError,
!                      "encoder did not return a string/unicode object "
!                      "(type=%.400s)",
!                      v->ob_type->tp_name);
!         Py_DECREF(v);
!         return NULL;
!     }
!     return v;
  }
  
***************
*** 2695,2701 ****
      char *encoding = NULL;
      char *errors = NULL;
      if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors))
          return NULL;
!     return PyString_AsDecodedObject((PyObject *)self, encoding, errors);
  }
  
--- 2706,2723 ----
      char *encoding = NULL;
      char *errors = NULL;
+     PyObject *v;
+     
      if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors))
          return NULL;
!     v = PyString_AsDecodedObject((PyObject *)self, encoding, errors);
!     if (!PyString_Check(v) && !PyUnicode_Check(v)) {
!         PyErr_Format(PyExc_TypeError,
!                      "decoder did not return a string/unicode object "
!                      "(type=%.400s)",
!                      v->ob_type->tp_name);
!         Py_DECREF(v);
!         return NULL;
!     }
!     return v;
  }
  

Index: unicodeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/unicodeobject.c,v
retrieving revision 2.213
retrieving revision 2.214
diff -C2 -d -r2.213 -r2.214
*** unicodeobject.c	17 Jun 2004 18:27:17 -0000	2.213
--- unicodeobject.c	8 Jul 2004 17:57:32 -0000	2.214
***************
*** 562,565 ****
--- 562,589 ----
  }
  
+ PyObject *PyUnicode_AsDecodedObject(PyObject *unicode,
+                                     const char *encoding,
+                                     const char *errors)
+ {
+     PyObject *v;
+ 
+     if (!PyUnicode_Check(unicode)) {
+         PyErr_BadArgument();
+         goto onError;
+     }
+ 
+     if (encoding == NULL)
+ 	encoding = PyUnicode_GetDefaultEncoding();
+ 
+     /* Decode via the codec registry */
+     v = PyCodec_Decode(unicode, encoding, errors);
+     if (v == NULL)
+         goto onError;
+     return v;
+ 
+  onError:
+     return NULL;
+ }
+ 
  PyObject *PyUnicode_Encode(const Py_UNICODE *s,
  			   int size,
***************
*** 577,580 ****
--- 601,628 ----
  }
  
+ PyObject *PyUnicode_AsEncodedObject(PyObject *unicode,
+                                     const char *encoding,
+                                     const char *errors)
+ {
+     PyObject *v;
+ 
+     if (!PyUnicode_Check(unicode)) {
+         PyErr_BadArgument();
+         goto onError;
+     }
+ 
+     if (encoding == NULL)
+ 	encoding = PyUnicode_GetDefaultEncoding();
+ 
+     /* Encode via the codec registry */
+     v = PyCodec_Encode(unicode, encoding, errors);
+     if (v == NULL)
+         goto onError;
+     return v;
+ 
+  onError:
+     return NULL;
+ }
+ 
  PyObject *PyUnicode_AsEncodedString(PyObject *unicode,
                                      const char *encoding,
***************
*** 609,613 ****
      if (v == NULL)
          goto onError;
-     /* XXX Should we really enforce this ? */
      if (!PyString_Check(v)) {
          PyErr_Format(PyExc_TypeError,
--- 657,660 ----
***************
*** 4870,4877 ****
  
  PyDoc_STRVAR(encode__doc__,
! "S.encode([encoding[,errors]]) -> string\n\
  \n\
! Return an encoded string version of S. Default encoding is the current\n\
! default string encoding. errors may be given to set a different error\n\
  handling scheme. Default is 'strict' meaning that encoding errors raise\n\
  a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\
--- 4917,4924 ----
  
  PyDoc_STRVAR(encode__doc__,
! "S.encode([encoding[,errors]]) -> string or unicode\n\
  \n\
! Encodes S 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 UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\
***************
*** 4884,4890 ****
      char *encoding = NULL;
      char *errors = NULL;
      if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors))
          return NULL;
!     return PyUnicode_AsEncodedString((PyObject *)self, encoding, errors);
  }
  
--- 4931,4979 ----
      char *encoding = NULL;
      char *errors = NULL;
+     PyObject *v;
+     
      if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors))
          return NULL;
!     v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors);
!     if (!PyString_Check(v) && !PyUnicode_Check(v)) {
!         PyErr_Format(PyExc_TypeError,
!                      "encoder did not return a string/unicode object "
!                      "(type=%.400s)",
!                      v->ob_type->tp_name);
!         Py_DECREF(v);
!         return NULL;
!     }
!     return v;
! }
! 
! PyDoc_STRVAR(decode__doc__,
! "S.decode([encoding[,errors]]) -> string or unicode\n\
! \n\
! Decodes S 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 UnicodeDecodeError. 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 UnicodeDecodeErrors.");
! 
! static PyObject *
! unicode_decode(PyStringObject *self, PyObject *args)
! {
!     char *encoding = NULL;
!     char *errors = NULL;
!     PyObject *v;
!     
!     if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors))
!         return NULL;
!     v = PyUnicode_AsDecodedObject((PyObject *)self, encoding, errors);
!     if (!PyString_Check(v) && !PyUnicode_Check(v)) {
!         PyErr_Format(PyExc_TypeError,
!                      "decoder did not return a string/unicode object "
!                      "(type=%.400s)",
!                      v->ob_type->tp_name);
!         Py_DECREF(v);
!         return NULL;
!     }
!     return v;
  }
  
***************
*** 6139,6142 ****
--- 6228,6232 ----
      {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__},
      {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__},
+     {"decode", (PyCFunction) unicode_decode, METH_VARARGS, decode__doc__},
  /*  {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */
      {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__},



More information about the Python-checkins mailing list