No, this isn't a bug. This is normal behaviour. See this answer: the bytes type in python 2.7 and PEP-358
It basically comes down that the 2.7 bytes is just an alias for str to smoothen the transition to 3.x.
No, this isn't a bug. This is normal behaviour. See this answer: the bytes type in python 2.7 and PEP-358
It basically comes down that the 2.7 bytes is just an alias for str to smoothen the transition to 3.x.
bytes doesn't exist as a separate kind of datastructure in Python 2.X so yes, it is entirely normal - str are bytestrings in Python 2 (unlike Python 3, where str are unicode strings).
The issue is that you are positioned at the end of the stream. Think of the position like a cursor. Once you have written b' world', your cursor is at the end of the stream. When you try to .read(), you are reading everything after the position of the cursor - which is nothing, so you get the empty bytestring.
To navigate around the stream you can use the .seek method:
>>> import io
>>> in_memory = io.BytesIO(b'hello', )
>>> in_memory.write(b' world')
>>> in_memory.seek(0) # go to the start of the stream
>>> print(in_memory.read())
b' world'
Note that, just like a filestream in write ('w') mode, the initial bytes b'hello' have been overwritten by your writing of b' world'.
.getvalue() just returns the entire contents of the stream regardless of current position.
this is a memory stream but still a stream. The position is stored, so like any other stream if you try to read after having written, you have to re-position:
import io
in_memory = io.BytesIO(b'hello')
in_memory.seek(0,2) # seek to end, else we overwrite
in_memory.write(b' world')
in_memory.seek(0) # seek to start
print( in_memory.read() )
prints:
b'hello world'
while in_memory.getvalue() doesn't need the final seek(0) as it returns the contents of the stream from position 0.
I'm trying to create a mock image for testing purposes; the scenario where a user uploads a photo on a website. Going through the linked S.O. topics has gotten me closer to that point by using io.BytesIO and the Pillow library.
-
Unit Testing a Django Form with a FileField
-
Django testing model with ImageField.
However, when I execute the test the following error is raised:UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte. The interpreter is pointing at this:
content=open(f.read(), 'rb')
This topic is covered in S.O. as well, but with the most liked answer I can't seem to get it to work.
-
UnicodeDecodeError: 'utf-8' codec can't decode byte
I'm not sure how to go about addressing the error at this point.
**tests**
class RedundantImageUpload(TestCase):
@classmethod
def setUpTestData(cls):
f = BytesIO()
image = Image.new("RGB", (100, 100))
image.save(f, 'png')
f.seek(0)
test_image = SimpleUploadedFile(
"test_image.png",
content=open(f.read(), 'rb'),
content_type="image/png"
)
user = User.objects.create_user("User")
import pdb; pdb.set_trace()
form = PhotoForm({'title': "Image Title"}, {'source': test_image})
instance = form.save(commit=False)
instance.photographer = user
instance.save()
cls.submitted_form = PhotoForm(
{'title': "Image Title"}, {'source': test_image}
)
def test_image_upload_path_exists(self):
print("Here")
print(self.submitted_form.errors)