This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: replace() method in unicode objects
Type: Stage:
Components: Interpreter Core Versions: Python 2.2
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: faeltir, jvr, nnorwitz
Priority: normal Keywords: patch

Created on 2003-02-05 13:15 by faeltir, last changed 2022-04-10 16:06 by admin. This issue is now closed.

File name Uploaded Description Edit
unicode-replace.patch faeltir, 2003-02-05 13:18
Messages (8)
msg42676 - (view) Author: Fredrik Juhlin (faeltir) Date: 2003-02-05 13:15

I found that unlike the string object, the replace()
method in the unicode object doesn't throw an exception
when you supply an empty pattern string. Instead it
slaps on the replace string a few times at the end. 

I'm supplying a patch to make the unicode object behave
like the string object.

I realize that in 2.3, doing replace() with an empty
pattern string will work differently so maybe you don't
consider throwing an exception the Right Way(TM), but
I'm presenting it to you in case you (like me) thinks
it's just better if unicode and string objects behave
in the same way while not changing the way that string
objects currently work in 2.2.

Best regards,
Fredrik Juhlin
msg42677 - (view) Author: Just van Rossum (jvr) * (Python triager) Date: 2003-02-10 19:36
Logged In: YES 

What I'm not sure of is whether the fact that this:
"x".replace("", "a")
raises an exception in 2.2 and "works" in 2.3 is intentional. It returns this in 2.3: 'axa'.
msg42678 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2003-02-10 20:20
Logged In: YES 

The change for 2.3 was on purpose.  See
Objects/stringobject.c 2.184 which fixed SF bug 595350.

Hmmm, unicode doesn't even do it right in 2.2.2+

>>> u"x".replace("", "a")

Not sure what to do to fix for 2.2.3. :-(
msg42679 - (view) Author: Fredrik Juhlin (faeltir) Date: 2003-02-11 07:30
Logged In: YES 

I checked the CVS and saw that it had not been touched,
which is why I supplied the patch to make it act in the same
manner as strings :)

The two options seems to be to either solve it in the manner
that my patch does or make both behave like in 2.3. 

While I realize that my opinion weighs light, not being part
of the coding team in any way, it seems to me like the road
of least surprise is the better way to go for a minor
release and that would be to not change the obviously
deliberate behaviour of the string object.

Well, that's my two cents :)
msg42680 - (view) Author: Just van Rossum (jvr) * (Python triager) Date: 2003-02-11 08:37
Logged In: YES 

I'm not following you, in 2.3 CVS, "a".replace("", "x") yields "xax", yet with your patch the same thing with a unicode string raises an exception...
msg42681 - (view) Author: Fredrik Juhlin (faeltir) Date: 2003-02-11 09:15
Logged In: YES 

Hello again,

I'm sorry for being unclear, I meant in the release22-maint
branch of the CVS, which I (perhaps mistakenly?) assumed is
the branch that all 2.2.* releases are made from.

Since I thought (apparently correctly) that the change in
2.3 was deliberate, I didn't think it was an issue for the

Again, I'm sorry for not being clear on that.
msg42682 - (view) Author: Just van Rossum (jvr) * (Python triager) Date: 2003-02-11 09:40
Logged In: YES 

Sorry, it was me who was sleeping. I thought there was both an issue with 2.2.2 as well as with 2.3... It seems 2.3 is fine. Sorry for the added confusion!
msg42683 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2004-10-21 02:47
Logged In: YES 

This patch appears to only be useful for 2.2.x which isn't
maintained any longer.  Since it would also change
behaviour, I'm rejecting it.
Date User Action Args
2022-04-10 16:06:33adminsetgithub: 37913
2003-02-05 13:15:47faeltircreate