]> granicus.if.org Git - python/commitdiff
Added new codec APIs and a new interface method .encode() which
authorMarc-André Lemburg <mal@egenix.com>
Thu, 6 Jul 2000 11:29:01 +0000 (11:29 +0000)
committerMarc-André Lemburg <mal@egenix.com>
Thu, 6 Jul 2000 11:29:01 +0000 (11:29 +0000)
works just like the Unicode one. The C APIs match the ones in the Unicode
implementation, but were extended to be able to reuse the existing
Unicode codecs for string purposes too.

Conversions from string to Unicode and back are done using the
default encoding.

Objects/stringobject.c

index e918829b72af320b34d778d7b9a2a5c25a691ad4..80da47d3e9623c0db9afb88493c4123e15890109 100644 (file)
@@ -152,6 +152,100 @@ PyString_FromString(str)
        return (PyObject *) op;
 }
 
+PyObject *PyString_Decode(const char *s,
+                         int size,
+                         const char *encoding,
+                         const char *errors)
+{
+    PyObject *buffer = NULL, *str;
+    
+    if (encoding == NULL) 
+       encoding = PyUnicode_GetDefaultEncoding();
+
+    /* Decode via the codec registry */
+    buffer = PyBuffer_FromMemory((void *)s, size);
+    if (buffer == NULL)
+        goto onError;
+    str = PyCodec_Decode(buffer, encoding, errors);
+    if (str == NULL)
+        goto onError;
+    /* Convert Unicode to a string using the default encoding */
+    if (PyUnicode_Check(str)) {
+       PyObject *temp = str;
+       str = PyUnicode_AsEncodedString(str, NULL, NULL);
+       Py_DECREF(temp);
+       if (str == NULL)
+           goto onError;
+    }
+    if (!PyString_Check(str)) {
+        PyErr_Format(PyExc_TypeError,
+                     "decoder did not return an string object (type=%.400s)",
+                     str->ob_type->tp_name);
+        Py_DECREF(str);
+        goto onError;
+    }
+    Py_DECREF(buffer);
+    return str;
+    
+ onError:
+    Py_XDECREF(buffer);
+    return NULL;
+}
+
+PyObject *PyString_Encode(const char *s,
+                         int size,
+                         const char *encoding,
+                         const char *errors)
+{
+    PyObject *v, *str;
+    
+    str = PyString_FromStringAndSize(s, size);
+    if (str == NULL)
+       return NULL;
+    v = PyString_AsEncodedString(str, encoding, errors);
+    Py_DECREF(str);
+    return v;
+}
+
+PyObject *PyString_AsEncodedString(PyObject *str,
+                                  const char *encoding,
+                                  const char *errors)
+{
+    PyObject *v;
+    
+    if (!PyString_Check(str)) {
+        PyErr_BadArgument();
+        goto onError;
+    }
+
+    if (encoding == NULL) 
+       encoding = PyUnicode_GetDefaultEncoding();
+
+    /* Encode via the codec registry */
+    v = PyCodec_Encode(str, encoding, errors);
+    if (v == NULL)
+        goto onError;
+    /* Convert Unicode to a string using the default encoding */
+    if (PyUnicode_Check(v)) {
+       PyObject *temp = v;
+       v = PyUnicode_AsEncodedString(v, NULL, NULL);
+       Py_DECREF(temp);
+       if (v == NULL)
+           goto onError;
+    }
+    if (!PyString_Check(v)) {
+        PyErr_Format(PyExc_TypeError,
+                     "encoder did not return a string object (type=%.400s)",
+                     v->ob_type->tp_name);
+        Py_DECREF(v);
+        goto onError;
+    }
+    return v;
+    
+ onError:
+    return NULL;
+}
+
 static void
 string_dealloc(op)
        PyObject *op;
@@ -1686,6 +1780,25 @@ string_endswith(self, args)
 }
 
 
+static char 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 ValueError. Other possible values are 'ignore' and 'replace'.";
+
+static PyObject *
+string_encode(PyStringObject *self, PyObject *args)
+{
+    char *encoding = NULL;
+    char *errors = NULL;
+    if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors))
+        return NULL;
+    return PyString_AsEncodedString((PyObject *)self, encoding, errors);
+}
+
+
 static char expandtabs__doc__[] =
 "S.expandtabs([tabsize]) -> string\n\
 \n\
@@ -2252,6 +2365,7 @@ string_methods[] = {
        {"ljust",       (PyCFunction)string_ljust,       1, ljust__doc__},
        {"rjust",       (PyCFunction)string_rjust,       1, rjust__doc__},
        {"center",      (PyCFunction)string_center,      1, center__doc__},
+       {"encode",      (PyCFunction)string_encode,      1, encode__doc__},
        {"expandtabs",  (PyCFunction)string_expandtabs,  1, expandtabs__doc__},
        {"splitlines",  (PyCFunction)string_splitlines,  1, splitlines__doc__},
 #if 0