Skip to content

Remove unnecessary check - #9874

Open
radarhere wants to merge 1 commit into
python-pillow:mainfrom
radarhere:merge
Open

Remove unnecessary check#9874
radarhere wants to merge 1 commit into
python-pillow:mainfrom
radarhere:merge

Conversation

@radarhere

@radarhere radarhere commented Aug 17, 2026

Copy link
Copy Markdown
Member

In Image.merge(),

if getmodebands(mode) != len(bands) or "*" in mode:

"*" in mode is redundant.

getmodebands(mode) has to be run first for the other condition to matter, and

Pillow/src/PIL/Image.py

Lines 310 to 318 in 6c06fa2

def getmodebands(mode: str) -> int:
"""
Gets the number of individual bands for this mode.
:param mode: Input mode.
:returns: The number of bands in this mode.
:exception KeyError: If the input mode was not a standard mode.
"""
return len(ImageMode.getmode(mode).bands)

none of the modes in ImageMode contain an asterisk.

So with or without this check,

from PIL import Image
Image.merge("RGB;*", [])

gives

Traceback (most recent call last):
  File "demo.py", line 2, in <module>
    Image.merge("RGB;*", [])
  File "PIL/Image.py", line 3792, in merge
    if getmodebands(mode) != len(bands) or "*" in mode:
       ^^^^^^^^^^^^^^^^^^
  File "PIL/Image.py", line 318, in getmodebands
    return len(ImageMode.getmode(mode).bands)
               ^^^^^^^^^^^^^^^^^^^^^^^
  File "PIL/ImageMode.py", line 84, in getmode
    type_str = mapping_modes[mode]
               ~~~~~~~~~~~~~^^^^^^
KeyError: 'RGB;*'

@akx akx left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM.

This code was already in pil-1.1.1 (the oldest commit I have in my grafted history), and I can't quickly see any reference to asterisk-endowed modes there, so who knows what this was meant to do :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants