Obscure Exception message

Andrew M. Kuchling akuchlin at mems-exchange.org
Fri Jun 9 10:18:28 EDT 2000


Michael Hudson <mwh21 at cam.ac.uk> writes:
> The Rapid-Fire-Python-Error-Message-Improver (me) says:

<K9>Checked in, Master.  Repairs complete.</K9>

The PyUnicode_Contains function needs a bit more work, though, since
it tries to coerce the left-hand operator to a Unicode string first.
This means that None in u'abc' gets "TypeError: coercing to Unicode:
need string or charbuffer".  Here's a proposed patch:

Index: unicodeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/unicodeobject.c,v
retrieving revision 2.24
diff -u -r2.24 unicodeobject.c
--- unicodeobject.c	2000/06/09 14:04:53	2.24
+++ unicodeobject.c	2000/06/09 14:14:48
@@ -2985,8 +2985,12 @@
 
     /* Coerce the two arguments */
     v = (PyUnicodeObject *)PyUnicode_FromObject(element);
-    if (v == NULL)
+    if (v == NULL) {
+	PyErr_Clear();
+	PyErr_SetString(PyExc_TypeError,
+	    "'in <string>' requires character as left operand");
 	goto onError;
+    }
     u = (PyUnicodeObject *)PyUnicode_FromObject(container);
     if (u == NULL) {
 	Py_DECREF(v);

Two questions are: 1) I suspect the PyErr_Clear() is unnecessary; am I
right?  2) Should it use PyErr_Format and say something like 'requires
character as left operand, not %s' % repr( <thing on LHS> ) ?

-- 
A.M. Kuchling			http://starship.python.net/crew/amk/
Poor woman, I suppose she led a dog's life, and it made her disagreeable,
which she mistook for being strong.
  -- Robertson Davies, _The Rebel Angels_





More information about the Python-list mailing list