Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/3730.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
When creating a ``toga.Image`` from a file path, it's now guaranteed that the file won't remain open after construction.
18 changes: 13 additions & 5 deletions testbed/tests/test_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
import shutil
from importlib import import_module
from pathlib import Path

import pytest
from PIL import Image as PIL_Image, ImageDraw as PIL_ImageDraw
Expand All @@ -16,14 +17,21 @@ def image_probe(app, image):


async def test_local_image(app):
"An image can be specified by filename"
"""An image can be specified by filename"""
image = toga.Image("resources/sample.png")
assert image.width == 144
assert image.height == 72


async def test_closed_file_handle(app):
"""The local image file isn't left open once the Image is created."""
_ = toga.Image("resources/sample.png")
# If the file still has an open handle, this should raise an IOError.
_ = Path(app.paths.app / "resources/sample.png").read_bytes()
Comment on lines +26 to +30
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pardon the interjection....but reading the file should be fine; however, deleting the file here should at least cause problems on Windows.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, hm. That makes sense. I suppose I could make a copy, and delete it afterward.



async def test_raw_image(app):
"An image can be created from the platform's raw representation"
"""An image can be created from the platform's raw representation"""
original = toga.Image("resources/sample.png")

image = toga.Image(original._impl.native)
Expand All @@ -33,7 +41,7 @@ async def test_raw_image(app):


async def test_bad_image_file(app):
"If a file isn't a loadable image, an error is raised"
"""If a file isn't a loadable image, an error is raised"""
with pytest.raises(
ValueError,
match=rf"Unable to load image from {re.escape(__file__)}",
Expand All @@ -42,7 +50,7 @@ async def test_bad_image_file(app):


async def test_buffer_image(app):
"An image can be constructed from buffer data"
"""An image can be constructed from buffer data"""
# Generate an image using pillow
pil_image = PIL_Image.new("RGBA", size=(110, 30))
draw_context = PIL_ImageDraw.Draw(pil_image)
Expand Down Expand Up @@ -86,7 +94,7 @@ async def test_pil_raw_and_data_image(app):


async def test_bad_image_data(app):
"If data isn't a valid image, an error is raised"
"""If data isn't a valid image, an error is raised"""
with pytest.raises(
ValueError,
match=r"Unable to load image from data",
Expand Down
Loading