[Python-bugs-list] [ python-Bugs-635398 ] re.sub() coerces u'' to ''

noreply@sourceforge.net noreply@sourceforge.net
Sat, 09 Nov 2002 10:48:12 -0800


Bugs item #635398, was opened at 2002-11-08 10:32
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=635398&group_id=5470

Category: Regular Expressions
Group: Python 2.2.1
Status: Open
>Resolution: Accepted
Priority: 5
Submitted By: Mike Brown (mike_j_brown)
Assigned to: Fredrik Lundh (effbot)
Summary: re.sub() coerces u'' to ''

Initial Comment:
Using Python 2.2.1 on FreeBSD, these work as 
expected:

>>> re.sub(u'f', u'b', u'foo')  # keep string as Unicode
u'boo'
>>> re.sub(u'f', u'b', 'foo')   # coerce string to Unicode
u'boo'

But this doesn't work the way I think it should:

>>> re.sub(u'f', u'b', u'')     # coerce string to non-
Unicode?!
> ''

That is, an empty Unicode string does not survive as 
Unicode after going through re.sub().

----------------------------------------------------------------------

>Comment By: Fredrik Lundh (effbot)
Date: 2002-11-09 19:48

Message:
Logged In: YES 
user_id=38376

this buglet has already been fixed in the SRE master
repository.  here's the relevant portion:

*** 1802,1808 ****
      switch (PyList_GET_SIZE(list)) {
      case 0:
          Py_DECREF(list);
!         return PyString_FromString("");
      case 1:
          result = PyList_GET_ITEM(list, 0);
          Py_INCREF(result);
--- 1785,1791 ----
      switch (PyList_GET_SIZE(list)) {
      case 0:
          Py_DECREF(list);
!         return PySequence_GetSlice(pattern, 0, 0);
      case 1:
          result = PyList_GET_ITEM(list, 0);
          Py_INCREF(result);

I'll update the Python repository asap (once I've gotten around
to merge in some changes done in the Python repository).

</F>

PS. also see my post on comp.lang.python on this topic.
well-written unicode code shouldn't care about things like
this...

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2002-11-09 19:05

Message:
Logged In: YES 
user_id=21627

Would you like to work on a patch for this bug?

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=635398&group_id=5470