]> granicus.if.org Git - python/commitdiff
Per Georg's suggestion, get rid of str.decode() (which always raises an
authorGuido van Rossum <guido@python.org>
Fri, 31 Aug 2007 13:48:41 +0000 (13:48 +0000)
committerGuido van Rossum <guido@python.org>
Fri, 31 Aug 2007 13:48:41 +0000 (13:48 +0000)
exception) and change bytes.find() to use _getbuffer(), so b"".find("")
will raise TypeError instead of SystemError.

Objects/bytesobject.c
Objects/unicodeobject.c

index ec379281ccce8487278427b3056c277098938d06..930b761f2433ac0b34f4b7a6a301f5ceaf15373d 100644 (file)
@@ -1067,32 +1067,25 @@ Py_LOCAL_INLINE(Py_ssize_t)
 bytes_find_internal(PyBytesObject *self, PyObject *args, int dir)
 {
     PyObject *subobj;
-    const char *sub;
-    Py_ssize_t sub_len;
+    PyBuffer subbuf;
     Py_ssize_t start=0, end=PY_SSIZE_T_MAX;
+    Py_ssize_t res;
 
     if (!PyArg_ParseTuple(args, "O|O&O&:find/rfind/index/rindex", &subobj,
         _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
         return -2;
-    if (PyBytes_Check(subobj)) {
-        sub = PyBytes_AS_STRING(subobj);
-        sub_len = PyBytes_GET_SIZE(subobj);
-    }
-    /* XXX --> use the modern buffer interface */
-    else if (PyObject_AsCharBuffer(subobj, &sub, &sub_len)) {
-        /* XXX - the "expected a character buffer object" is pretty
-           confusing for a non-expert.  remap to something else ? */
+    if (_getbuffer(subobj, &subbuf) < 0)
         return -2;
-    }
-
     if (dir > 0)
-        return stringlib_find_slice(
+        res = stringlib_find_slice(
             PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
-            sub, sub_len, start, end);
+            subbuf.buf, subbuf.len, start, end);
     else
-        return stringlib_rfind_slice(
+        res = stringlib_rfind_slice(
             PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
-            sub, sub_len, start, end);
+            subbuf.buf, subbuf.len, start, end);
+    PyObject_ReleaseBuffer(subobj, &subbuf);
+    return res;
 }
 
 
index e9ce08c2e2cec0e8afc5f30d4cb0895d5df5f3b5..680ed5f5b43263d638a5a20e26c1ff818c5fd49a 100644 (file)
@@ -6462,23 +6462,6 @@ unicode_encode(PyUnicodeObject *self, PyObject *args)
     return NULL;
 }
 
-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(PyUnicodeObject *self, PyObject *args)
-{
-    PyErr_Format(PyExc_TypeError, "decoding str is not supported");
-    return NULL;
-}
-
 PyDoc_STRVAR(expandtabs__doc__,
 "S.expandtabs([tabsize]) -> unicode\n\
 \n\
@@ -7997,8 +7980,6 @@ static PyMethodDef unicode_methods[] = {
     {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__},
     {"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__},
     {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__},
     {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__},