why not "'in' in 'in'"?
Bjorn Pettersen
BPettersen at NAREX.com
Fri Jun 14 19:59:56 EDT 2002
> From: Tim Peters [mailto:tim.one at comcast.net]
[snip]
> So if somebody wants this change enough to submit a patch
> (code, doc, + test suite changes), it's likely to be accepted
> for 2.3. As code changes go, this one should be particularly
> easy. We have an internal bet going as to whether this
> invitation is enough to kill the idea <wink>.
>
> some-parts-of-this-were-actually-true-ly y'rs - tim
Here are the code changes. (This patch is public domain, etc. etc.)
I'm getting a two warnings:
1: "incompatible types - from 'struct _object*' to 'struct
PyStringObject'" on the line
result = string_find_internal(a, args, +1);
2: "incompatible types - from 'struct _object*' to 'struct
PyUnicodeObject'" on the line
findResult = (PyObject*) unicode_find(container, args);
which I can't understand (anyone?)
If anyone feels like writing the doc/test suite changes feel free to use
my patch.
-- bjorn
*** stringobject.orig.c Fri Jun 14 16:46:58 2002
--- stringobject.c Fri Jun 14 17:51:16 2002
***************
*** 46,51 ****
--- 46,56 ----
not counting the null terminating character, and is therefore equal
to the
`size' parameter.
*/
+
+ /* forward declarations */
+ long
+ string_find_internal(PyStringObject *self, PyObject *args, int dir);
+
PyObject *
PyString_FromStringAndSize(const char *str, int size)
{
***************
*** 824,848 ****
static int
string_contains(PyObject *a, PyObject *el)
{
! register char *s, *end;
! register char c;
#ifdef Py_USING_UNICODE
! if (PyUnicode_Check(el))
! return PyUnicode_Contains(a, el);
#endif
! if (!PyString_Check(el) || PyString_Size(el) != 1) {
! PyErr_SetString(PyExc_TypeError,
! "'in <string>' requires character as left operand");
! return -1;
! }
! c = PyString_AsString(el)[0];
! s = PyString_AsString(a);
! end = s + PyString_Size(a);
! while (s < end) {
! if (c == *s++)
! return 1;
! }
! return 0;
}
static PyObject *
--- 829,855 ----
static int
string_contains(PyObject *a, PyObject *el)
{
! long result;
! PyObject* args;
!
#ifdef Py_USING_UNICODE
! if (PyUnicode_Check(el))
! return PyUnicode_Contains(a, el);
#endif
! if (!PyString_Check(el)) {
! PyErr_SetString(PyExc_TypeError,
! "'in <string>' requires string as left operand");
! return -1;
! }
! args = Py_BuildValue("(O)", el);
! result = string_find_internal(a, args, +1);
! Py_DECREF(args);
! if (result >= 0) {
! return 1;
! }
! else {
! return 0;
! }
}
static PyObject *
***************
*** 1329,1335 ****
return -1;
}
-
static char find__doc__[] =
"S.find(sub [,start [,end]]) -> int\n\
--- 1336,1341 ----
*** unicodeobject.orig.c Fri Jun 14 17:12:09 2002
--- unicodeobject.c Fri Jun 14 17:53:44 2002
***************
*** 76,81 ****
--- 76,85 ----
# define BYTEORDER_IS_LITTLE_ENDIAN
#endif
+ /* forward declaration */
+ PyObject *
+ unicode_find(PyUnicodeObject *self, PyObject *args);
+
/* --- Globals
------------------------------------------------------------
The globals are initialized by the _PyUnicode_Init() API and should
***************
*** 3789,3830 ****
return -1;
}
! int PyUnicode_Contains(PyObject *container,
! PyObject *element)
{
PyUnicodeObject *u = NULL, *v = NULL;
int result;
! register const Py_UNICODE *p, *e;
! register Py_UNICODE ch;
/* Coerce the two arguments */
v = (PyUnicodeObject *)PyUnicode_FromObject(element);
if (v == NULL) {
! PyErr_SetString(PyExc_TypeError,
! "'in <string>' requires character as left operand");
! goto onError;
}
u = (PyUnicodeObject *)PyUnicode_FromObject(container);
if (u == NULL) {
! Py_DECREF(v);
! goto onError;
}
/* Check v in u */
! if (PyUnicode_GET_SIZE(v) != 1) {
! PyErr_SetString(PyExc_TypeError,
! "'in <string>' requires character as left operand");
! goto onError;
}
! ch = *PyUnicode_AS_UNICODE(v);
! p = PyUnicode_AS_UNICODE(u);
! e = p + PyUnicode_GET_SIZE(u);
! result = 0;
! while (p < e) {
! if (*p++ == ch) {
! result = 1;
! break;
! }
}
Py_DECREF(u);
--- 3793,3835 ----
return -1;
}
! int PyUnicode_Contains(PyObject *container, PyObject *element)
{
PyUnicodeObject *u = NULL, *v = NULL;
int result;
! long tmpresult;
! PyObject *args;
! PyObject *findResult = NULL;
/* Coerce the two arguments */
v = (PyUnicodeObject *)PyUnicode_FromObject(element);
if (v == NULL) {
! PyErr_SetString(PyExc_TypeError,
! "'in <string>' requires string as left operand");
! goto onError;
}
u = (PyUnicodeObject *)PyUnicode_FromObject(container);
if (u == NULL) {
! Py_DECREF(v);
! goto onError;
}
/* Check v in u */
! args = Py_BuildValue("(O)", element);
! findResult = (PyObject*) unicode_find(container, args);
! Py_DECREF(args);
! if (findResult == NULL) {
! goto onError;
}
! else {
! tmpresult = PyInt_AsLong(findResult);
! Py_DECREF(findResult);
! if (tmpresult >= 0) {
! result = 1;
! }
! else {
! result = 0;
! }
}
Py_DECREF(u);
***************
*** 3832,3837 ****
--- 3837,3843 ----
return result;
onError:
+ Py_XDECREF(findResult);
Py_XDECREF(u);
Py_XDECREF(v);
return -1;
More information about the Python-list
mailing list