[issue28029] Replace and empty strings

2020-04-01 Thread Glenn Linderman


Glenn Linderman  added the comment:

Nope:

I guess for   x.replace( a, b, c )  the workaround would be

x.replace( a, b, c ) if x else x.replace( a, b )

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2020-04-01 Thread Glenn Linderman

Glenn Linderman  added the comment:

Thanks Stèphańe and Serhiy, I just discovered this strange behavior in 3.8, and 
wondered how my logic was wrong, until I pinpointed the inconsistent behaviour 
of str.replace with an empty first parameter and replace count of 1.

Glad to see it is fixed in 3.9.

I guess for   x.replace( a, b, c )  the workaround would be

x.replace( a, b, c ) if a else x.replace( a, b )

At least for recent versions of Python 3.

--
nosy: +v+python

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2019-10-30 Thread STINNER Victor


STINNER Victor  added the comment:

> Because of possible compatibility issue (very unlikely) this change is done 
> only in master and will not be backported.

Yeah, I agree. It would be a mistake to change the Python behavior in a minor 
release.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2019-10-30 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

Because of possible compatibility issue (very unlikely) this change is done 
only in master and will not be backported.

--
resolution:  -> fixed
stage: patch review -> resolved
status: open -> closed
versions: +Python 3.9 -Python 2.7, Python 3.5, Python 3.6, Python 3.7

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2019-10-30 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:


New changeset 865c3b257fe38154a4320c7ee6afb416f665b9c2 by Serhiy Storchaka in 
branch 'master':
bpo-28029: Make "".replace("", s, n) returning s for any n != 0. (GH-16981)
https://github.com/python/cpython/commit/865c3b257fe38154a4320c7ee6afb416f665b9c2


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2019-10-29 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
Removed message: https://bugs.python.org/msg355681

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2019-10-29 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
Removed message: https://bugs.python.org/msg355679

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2019-10-29 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

Raymond, the link is about str.split(), not str.replace().

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2019-10-29 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

The current docs could certainly use clarification.

FWIW, here is a link to my explanation for the existing logic in str.replace(): 
 https://stackoverflow.com/questions/16645083 
Perhaps some of that wording may be helpful.

--
nosy: +rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2019-10-29 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

Note that no tests were affected by this change.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2019-10-29 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

There are expected some relations between str methods. For example,

s.replace(a, b, n) == s.replace(a, b)   if n >= s.count(a)
len(s.replace(a, b, n)) == len(s) + (len(b)-len(a)) * n   if 0 <= n <= 
s.count(a)
len(s.replace(a, b, n)) == len(s) + (len(b)-len(a)) * s.count(a)   if n >= 
s.count(a)

Inconsistency between "".replace("", s, n) and "".replace("", s) is just a bug, 
and it should be fixed in the most consistent way. There are precedences, the 
behavior of replace() already was changed 3 times in the past. I think that 
chances to break some code are tiny, we just fix inconsistency why can puzzle 
users.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2019-10-29 Thread Serhiy Storchaka


Change by Serhiy Storchaka :


--
pull_requests: +16507
pull_request: https://github.com/python/cpython/pull/16981

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2019-10-28 Thread STINNER Victor


STINNER Victor  added the comment:

> What result would you expect of `"" in ""` and `"".count("")`?

I don't suggest to change these operator and method:

>>> "" in ""
True
>>> "".count("")
1

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2019-10-28 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

What result would you expect of `"" in ""` and `"".count("")`?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2019-10-28 Thread STINNER Victor


STINNER Victor  added the comment:

Well, in fact, I would expect that "".replace(...) would always return "": for 
any argument passed to replace().

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2019-10-28 Thread STINNER Victor


STINNER Victor  added the comment:

The current behavior is really surprising.

>>> "".replace("", "|")
'|'
>>> "".replace("", "|", -1)
'|'

vs

>>> "".replace("", "|", 0)
''
>>> "".replace("", "|", 1)
''
>>> "".replace("", "|", 1000)
''

I always expect "|".

---

This behavior makes sense to me:

>>> "abc".replace("", "|")
'|a|b|c|'
>>> "abc".replace("", "|", -1)
'|a|b|c|'
>>> "abc".replace("", "|", 0)
'abc'
>>> "abc".replace("", "|", 1)
'|abc'
>>> "abc".replace("", "|", 100)
'|a|b|c|'

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2016-10-25 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

Interestingly, initially string.replace() was implemented in terms of 
split/join. string.replace(s, '', s2, n) returned s for any s, s2 and n.

After making replace() a method of str, in aca7b5eaf5e8 (1999-10-12), it became 
raising ValueError for empty pattern string.

Since 762dd09edb83 (issue599128, 2002-08-23) it supports zero-length pattern 
string. str.replace('', s1, s2, n) returned '' for any s1, s2 and n.

New implementation added in 41809406a35e (2006-05-25) made str.replace('', '', 
'x', -1) returning 'x' while str.replace('', '', 'x', 1) still returns ''.

As you can see, the behavior of replacing with empty pattern is not stable. It 
was changed several times. Maybe it is a time for new change.

--
versions: +Python 3.7

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2016-09-09 Thread Stéphane Henriot

Changes by Stéphane Henriot :


--
nosy: +georg.brandl

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2016-09-08 Thread Martin Panter

Martin Panter added the comment:

There may be related discussion in Issue 24243, also about searching for empty 
strings. A while ago I meant to add documetation and tests for that, but I lost 
momentum after cleaning up the existing tests.

Some of the behaviours are undocumented and surprising, but if you look at the 
implementation it is clear they are not accidental. E.g. I think there is a 
function called replace_interleave() or something.

IMO you can find an unlimited number of instances of an empty string at index 
zero of any string. So calls like "ABC".strip("") are sensible to raise an 
exception, and the interleave mode of "ABC".count("") is unexpected. But I 
don’t see a big need to change this existing behaviour as long as it is 
documented.

--
nosy: +martin.panter
versions: +Python 2.7

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2016-09-08 Thread Stéphane Henriot

Stéphane Henriot added the comment:

I understand it might be a rather rare case.

Nevertheless, don't you think the inconsistency Serhiy pointed out makes it 
look like a bug needing a fix?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2016-09-08 Thread R. David Murray

R. David Murray added the comment:

Victor: because it would break backward compatibility to raise an error where 
one wasn't raised before.  There's not enough motivation for that.  I'm not 
sure about the 1 case; it would again be a change in behavior.  Probably we 
should just document it as there isn't enough reason to risk breaking working 
code.

--
nosy: +r.david.murray

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2016-09-08 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

Agreed with Stéphane. Since count() returns 1, resulting string should contain 
1 replacement.

Using regular expressions:

>>> re.sub('', 'x', '')
'x'
>>> re.sub('', 'x', '', 1)
'x'

--
nosy: +serhiy.storchaka

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2016-09-08 Thread STINNER Victor

STINNER Victor added the comment:

str.replace("", whatever) doesn't make sense. Why not raising an exception in 
this case?

--
nosy: +haypo

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2016-09-08 Thread Eric N. Vander Weele

Changes by Eric N. Vander Weele :


--
nosy: +ericvw

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2016-09-08 Thread Stéphane Henriot

Stéphane Henriot added the comment:

Thanks for your help.

However, I'm not sure I would agree, regarding the correct behavior.

I guess the main question is « What is an occurrence? ».

Are you not convinced that in, count and find indicate occurrences?
To my understanding, the empty string occurs basically everywhere in strings 
(including beginning and end):

>>> "blabla".replace("", "ε")
'εbεlεaεbεlεaε'

To give a bit more (useless?) context, I was implementing a GCD algorithm based 
on replacements by Knuth (very beginning of TAOCP) and it looks like the 
current cpython implementation gives 0 for gcd(1, 1) :(

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2016-09-08 Thread SilentGhost

SilentGhost added the comment:

Just the least intrusive patch. Also, to me PyPy behaviour doesn't seem correct.

--
nosy: +SilentGhost
stage: needs patch -> patch review

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2016-09-08 Thread SilentGhost

Changes by SilentGhost :


--
keywords: +patch
Added file: http://bugs.python.org/file44477/issue28029.diff

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2016-09-08 Thread Stéphane Henriot

Stéphane Henriot added the comment:

For what it's worth, here is the behavior in PyPy 2.2.1

 "".replace("", "prefix", 1)
'prefix'
 "".replace("", "prefix")
'prefix'

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2016-09-08 Thread SilentGhost

Changes by SilentGhost :


--
components: +Interpreter Core
stage:  -> needs patch
versions: +Python 3.5, Python 3.6

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2016-09-08 Thread Stéphane Henriot

New submission from Stéphane Henriot:

A few days ago, the following behavior surprised me.

>>> "".replace("", "prefix", 1)
''
>>> "".replace("", "prefix")
'prefix'

It seems to me this edge case isn't correctly documented/implemented. I tested 
with python 2.7, 3.4 and 3.5, I guess it applies for other versions as well.

Here are a few elements, for reference.

« string.replace(s, old, new[, maxreplace])
Return a copy of string s with all occurrences of substring old replaced by 
new. If the optional argument maxreplace is given, the first maxreplace 
occurrences are replaced. »

>>> "" in ""
True
>>> "".count("")
1
>>> "".find("")
0

https://hg.python.org/cpython/file/2.7/Objects/stringobject.c#l2750
https://hg.python.org/cpython/file/default/Objects/unicodeobject.c#l10479

Thanks,

Stéphane.

--
messages: 275140
nosy: Stéphane Henriot
priority: normal
severity: normal
status: open
title: Replace and empty strings
type: behavior

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com