Skip to content

Commit

Permalink
Remove legacy 1-bit api, fix AttributeError
Browse files Browse the repository at this point in the history
PytestUnraisableExceptionWarning: Exception ignored in: <function PhotoImage.__del__>
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
  • Loading branch information
homm committed Sep 2, 2024
1 parent 5a16146 commit f1c54f0
Showing 1 changed file with 5 additions and 23 deletions.
28 changes: 5 additions & 23 deletions src/PIL/ImageTk.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,6 @@
# --------------------------------------------------------------------
# Check for Tkinter interface hooks

_pilbitmap_ok = None


def _pilbitmap_check() -> int:
global _pilbitmap_ok
if _pilbitmap_ok is None:
try:
im = Image.new("1", (1, 1))
tkinter.BitmapImage(data=f"PIL:{im.im.id}")
_pilbitmap_ok = 1
except tkinter.TclError:
_pilbitmap_ok = 0
return _pilbitmap_ok


def _get_image_from_kw(kw: dict[str, Any]) -> ImageFile.ImageFile | None:
source = None
Expand Down Expand Up @@ -142,6 +128,8 @@ def __init__(
self.paste(image)

def __del__(self) -> None:
if not hasattr(self, "__photo"):
return
name = self.__photo.name
self.__photo.name = None
try:
Expand Down Expand Up @@ -225,17 +213,11 @@ def __init__(self, image: Image.Image | None = None, **kw: Any) -> None:
self.__mode = image.mode
self.__size = image.size

if _pilbitmap_check():
# fast way (requires the pilbitmap booster patch)
image.load()
kw["data"] = f"PIL:{image.im.id}"
self.__im = image # must keep a reference
else:
# slow but safe way
kw["data"] = image.tobitmap()
self.__photo = tkinter.BitmapImage(**kw)
self.__photo = tkinter.BitmapImage(data=image.tobitmap(), **kw)

def __del__(self) -> None:
if not hasattr(self, "__photo"):
return
name = self.__photo.name
self.__photo.name = None
try:
Expand Down

0 comments on commit f1c54f0

Please sign in to comment.