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.

Author steven.daprano
Recipients opensource-assist, rhettinger, steven.daprano, xtreak
Date 2020-01-11.16:47:30
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1578761252.27.0.565633406639.issue39304@roundup.psfhosted.org>
In-reply-to
Content
Sorry, I disagree that this is a mere implementation detail. The term "implementation detail" normally implies behaviour which occurs *by accident* due to the specific implementation, rather than being intentionally chosen.

A good example is early versions of list.sort(), which was stable for small lists only because the implementation happened to use insertion sort for small lists. Insertion sort wasn't chosen because it was stable; had the implementation changed to another sort, the behaviour would have changed. (Later on, the implementation did change, and stability became a documented and guaranteed feature.)

This is not what is happening here. The behaviour of for negative count doesn't "just happen by accident" due to other, unrelated, choices. It happens because the code intentionally tests for a negative count and replaces it with the maximum value possible:

    if (maxcount < 0)
        maxcount = PY_SSIZE_T_MAX;


and it is documented in the C source code as a comment to argument clinic:

    count: Py_ssize_t = -1
        Maximum number of occurrences to replace.
        -1 (the default value) means replace all occurrences.


https://github.com/python/cpython/blob/43682f1e39a3c61f0e8a638b887bcdcbfef766c5/Objects/unicodeobject.c#L12682


Some more evidence that this is intentional behaviour: in test_string.py, there are various tests that -1 behaves the same as sys.maxsize, e.g.:

        EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", -1)
        EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", sys.maxsize)


That's not an isolated test, there are many of them.
History
Date User Action Args
2020-01-11 16:47:32steven.dapranosetrecipients: + steven.daprano, rhettinger, xtreak, opensource-assist
2020-01-11 16:47:32steven.dapranosetmessageid: <1578761252.27.0.565633406639.issue39304@roundup.psfhosted.org>
2020-01-11 16:47:32steven.dapranolinkissue39304 messages
2020-01-11 16:47:30steven.dapranocreate