[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

[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

[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. --

[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,

[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

[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

[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

[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 ___ ___

[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

[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 ___ ___

[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) +

[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 ___

[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

[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 ___ ___

[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 ___

[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

[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

[issue28029] Replace and empty strings

2016-09-09 Thread Stéphane Henriot
Changes by Stéphane Henriot : -- nosy: +georg.brandl ___ Python tracker ___ ___

[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

[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

[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

[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 ___

[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

[issue28029] Replace and empty strings

2016-09-08 Thread Eric N. Vander Weele
Changes by Eric N. Vander Weele : -- nosy: +ericvw ___ Python tracker ___ ___

[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

[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

[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 ___

[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

[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

[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