[Patches] In overloading: not special casing strings

Moshe Zadka Moshe Zadka <mzadka@geocities.com>
Sun, 5 Mar 2000 00:45:27 +0200 (IST)


Especially with moving unicode into the Python core, it makes little sense
for strings to be treated specially by PySequence_Contains. Here's a patch
(which basically just moves code around) to take care of this issue.

Enjoy.

Checkin message:
Moving the character in string checking code into the string object.

Diff:
diff -c -r python/dist/src/Objects/abstract.c build/Objects/abstract.c
*** python/dist/src/Objects/abstract.c	Sat Mar  4 12:38:54 2000
--- build/Objects/abstract.c	Sat Mar  4 09:00:03 2000
***************
*** 1121,1144 ****
  	PyObject *x;
  	PySequenceMethods *sq;
  
- 	/* Special case for char in string */
- 	if (PyString_Check(w)) {
- 		register char *s, *end;
- 		register char c;
- 		if (!PyString_Check(v) || PyString_Size(v) != 1) {
- 			PyErr_SetString(PyExc_TypeError,
- 			    "string member test needs char left operand");
- 			return -1;
- 		}
- 		c = PyString_AsString(v)[0];
- 		s = PyString_AsString(w);
- 		end = s + PyString_Size(w);
- 		while (s < end) {
- 			if (c == *s++)
- 				return 1;
- 		}
- 		return 0;
- 	}
  	if(PyType_HasFeature(w->ob_type, Py_TPFLAGS_HAVE_SEQUENCE_IN)) {
  		sq = w->ob_type->tp_as_sequence;
  	        if(sq != NULL && sq->sq_contains != NULL)
--- 1121,1126 ----
diff -c -r python/dist/src/Objects/stringobject.c build/Objects/stringobject.c
*** python/dist/src/Objects/stringobject.c	Sat Mar  4 12:39:02 2000
--- build/Objects/stringobject.c	Sat Mar  4 08:55:04 2000
***************
*** 381,386 ****
--- 381,404 ----
  	return PyString_FromStringAndSize(a->ob_sval + i, (int) (j-i));
  }
  
+ static int
+ string_contains(a, el)
+ PyObject *a, *el;
+ {
+ 	register char *s, *end;
+ 	register char c;
+ 	if (!PyString_Check(el) || PyString_Size(el) != 1)
+ 		return 0;
+ 	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 *
  string_item(a, i)
  	PyStringObject *a;
***************
*** 516,521 ****
--- 534,540 ----
  	(intintargfunc)string_slice, /*sq_slice*/
  	0,		/*sq_ass_item*/
  	0,		/*sq_ass_slice*/
+ 	(objobjproc)string_contains /*sq_contains*/
  };
  
  static PyBufferProcs string_as_buffer = {

%%%%%%%%%%%%%%%%%%%%%%% end patch %%%%%%%%%%%%%%%%%%%%%%%%%%
Disclaimer:
I confirm that, to the best of my knowledge and belief, this
contribution is free of any claims of third parties under
copyright, patent or other rights or interests ("claims").  To
the extent that I have any such claims, I hereby grant to CNRI a
nonexclusive, irrevocable, royalty-free, worldwide license to
reproduce, distribute, perform and/or display publicly, prepare
derivative versions, and otherwise use this contribution as part
of the Python software and its related documentation, or any
derivative versions thereof, at no cost to CNRI or its licensed
users, and to authorize others to do so.

I acknowledge that CNRI may, at its sole discretion, decide
whether or not to incorporate this contribution in the Python
software and its related documentation.  I further grant CNRI
permission to use my name and other identifying information
provided to CNRI by me for use in connection with the Python
software and its related documentation.


--
Moshe Zadka <mzadka@geocities.com>. 
http://www.oreilly.com/news/prescod_0300.html