classification
Title: bytearray partition bug
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.7, Python 3.6, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: berker.peksag, brett.cannon, ezio.melotti, georg.brandl, loewis, mark.dickinson, pitrou, serhiy.storchaka, terry.reedy
Priority: normal Keywords: patch

Created on 2013-12-21 19:52 by BreamoreBoy, last changed 2018-09-13 23:16 by berker.peksag. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 4158 merged serhiy.storchaka, 2017-10-28 16:26
PR 4162 merged serhiy.storchaka, 2017-10-29 08:47
PR 4163 merged serhiy.storchaka, 2017-10-29 08:47
PR 9268 merged ZackerySpytz, 2018-09-13 17:57
Messages (10)
msg206773 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2013-12-21 19:52
If partition is called with a single byte it works correctly but if called with the equivalent integer it returns the same bytearray with two empty arrays as follows.

py> ba = bytearray(range(8))
py> ba
bytearray(b'\x00\x01\x02\x03\x04\x05\x06\x07')
py> 3 in ba
True
py> ba.find(3) == ba.index(3) == ba.find(b'\x03')
True
py> ba.partition(b'\x03')
(bytearray(b'\x00\x01\x02'), bytearray(b'\x03'), bytearray(b'\x04\x05\x06
\x07'))
py> ba.partition(3)
(bytearray(b'\x00\x01\x02\x03\x04\x05\x06\x07'), bytearray(b''), bytearray
(b''))

More background on the thread starting here https://mail.python.org/pipermail/python-list/2013-December/663111.html which refers to Issue 12170.
msg206774 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-12-21 20:00
Similar bug was in 3.2:

>>> ba = bytearray(range(8))
>>> ba[2:6]
bytearray(b'\x02\x03\x04\x05')
>>> ba[2:6] = 2
>>> ba
bytearray(b'\x00\x01\x00\x00\x06\x07')

Now it is fixed.
msg206775 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-12-21 20:12
Bytearray slice assignment bug was fixed in issue8401.
msg206792 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-12-21 22:22
Whatever the change, bytes and bytearray should act the same.

>>> b = bytes(range(8))
>>> b
b'\x00\x01\x02\x03\x04\x05\x06\x07'
>>> b.partition(3)
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    b.partition(3)
TypeError: expected bytes, bytearray or buffer compatible object

As noted in the thread, ba.partition(a) apparently is executed as ba.partition(bytearray(a)) if a is not a bytearray (or maybe not a buffer object). bytearray(3) == bytearray((0,0,0)) and the latter is not in ba and hence the output given is 'correct'.
msg206793 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2013-12-21 22:38
I believe that all methods should act the same, but they don't as a result of the work done in issue12170.  E.g. find will accept integer input but split will not.  Given this comment at the top of test_bytes.py "XXX This is a mess.  Common tests should be moved to buffer_tests.py, which itself ought to be unified with string_tests.py (and the latter should be modernized).", it looks like a thorough review of the code and tests is in order.
msg305167 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2017-10-28 20:35
To answer Mark, even though no longer nosy: In general, sequence methods .count, .index, and .__contains__ take sequence members and only members as arguments.  Unicode sequences are exceptional because codepoints are not Python objects, so string subsequences must be used instead.  Byte-like sequences are also exceptional in that both members and subsequences are accepted for these methods.

String-like sequence methods .split and .partition take subsequences as arguments.  I think the doc should make this clearer.
msg305170 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-10-28 23:12
New changeset a2314283ff87c65e1745a42c2f2b716b1a209128 by Serhiy Storchaka in branch 'master':
bpo-20047: Make bytearray methods partition() and rpartition() rejecting (#4158)
https://github.com/python/cpython/commit/a2314283ff87c65e1745a42c2f2b716b1a209128
msg305183 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-10-29 10:24
New changeset 9ea5a3a45b35d01b602e7e4da4f72b2db407e5c6 by Serhiy Storchaka in branch '3.6':
[3.6] bpo-20047: Make bytearray methods partition() and rpartition() rejecting (GH-4158) (#4162)
https://github.com/python/cpython/commit/9ea5a3a45b35d01b602e7e4da4f72b2db407e5c6
msg305184 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-10-29 10:25
New changeset 107f3cc791d223dc06b7c80f0de672e88ae6a8d1 by Serhiy Storchaka in branch '2.7':
[2.7] bpo-20047: Make bytearray methods partition() and rpartition() rejecting (GH-4158) (#4163)
https://github.com/python/cpython/commit/107f3cc791d223dc06b7c80f0de672e88ae6a8d1
msg325306 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2018-09-13 23:16
New changeset 0b9fe1734168d45861d6dc3022492387dec5a4a2 by Berker Peksag (Zackery Spytz) in branch '2.7':
[2.7] bpo-20047: Remove Objects/bytesobject.c from 2.7 (GH-9268)
https://github.com/python/cpython/commit/0b9fe1734168d45861d6dc3022492387dec5a4a2
History
Date User Action Args
2018-09-13 23:16:44berker.peksagsetnosy: + berker.peksag
messages: + msg325306
2018-09-13 17:57:12ZackerySpytzsetpull_requests: + pull_request8700
2017-10-29 10:26:39serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2017-10-29 10:25:40serhiy.storchakasetmessages: + msg305184
2017-10-29 10:24:50serhiy.storchakasetmessages: + msg305183
2017-10-29 08:47:59serhiy.storchakasetpull_requests: + pull_request4132
2017-10-29 08:47:19serhiy.storchakasetpull_requests: + pull_request4131
2017-10-28 23:12:01serhiy.storchakasetmessages: + msg305170
2017-10-28 20:35:41terry.reedysetmessages: + msg305167
2017-10-28 16:26:52serhiy.storchakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request4127
2017-10-28 16:00:14serhiy.storchakasetassignee: serhiy.storchaka
versions: + Python 2.7, Python 3.6, Python 3.7, - Python 3.3, Python 3.4
2014-02-03 15:39:10BreamoreBoysetnosy: - BreamoreBoy
2013-12-30 16:06:01brett.cannonsetnosy: + brett.cannon
2013-12-21 22:38:39BreamoreBoysetmessages: + msg206793
2013-12-21 22:22:26terry.reedysetnosy: + terry.reedy
messages: + msg206792
2013-12-21 20:12:07serhiy.storchakasetnosy: + loewis, georg.brandl, mark.dickinson, pitrou, ezio.melotti
messages: + msg206775
2013-12-21 20:00:48serhiy.storchakasetnosy: + serhiy.storchaka

messages: + msg206774
versions: + Python 3.4
2013-12-21 19:52:41BreamoreBoycreate