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.

classification
Title: bytearray changes when function mutates it
Type: behavior Stage: resolved
Components: Windows Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: SilentGhost, eatrawmeat391, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2016-07-30 09:54 by eatrawmeat391, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
unbpe.py eatrawmeat391, 2016-07-30 09:54
Messages (2)
msg271674 - (view) Author: eatrawmeat391 (eatrawmeat391) Date: 2016-07-30 09:54
I converted a function name yuke_bpe from C and ran into an unknown issue after running it.The code works but it emptied all of my bytearray.
You can reproduce the problem by doing it in the command line

from unbpe import yuke_bpe
a = bytearray("This is a byte array")
b = yuke_bpe(a, 100 ,1)

You'll see that bytearray a is empty after the yuke_bpe function calling but the function never touches that variable
msg271677 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2016-07-30 10:20
bytearray is a mutable object and the behaviour is compatible with behaviour of any mutable object in Python. You're passing a into the yuke_bpe function and the original object is being modified (emptied) there. To work around this you could copy the object within the functioin and carry out transformations on the copied object.
History
Date User Action Args
2022-04-11 14:58:34adminsetgithub: 71838
2016-07-30 10:20:15SilentGhostsetstatus: open -> closed

type: behavior
title: About bytearray -> bytearray changes when function mutates it
nosy: + SilentGhost

messages: + msg271677
resolution: not a bug
stage: resolved
2016-07-30 09:54:42eatrawmeat391create