Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved Surface.fill error message for invalid blend flags #2434

Conversation

Matiiss
Copy link
Member

@Matiiss Matiiss commented Sep 3, 2023

Test code:

import pygame

image = pygame.Surface((5, 5))
try:
    image.fill((255, 0, 0), special_flags=pygame.BLEND_PREMULTIPLIED)
except pygame.error as e:
    print(f"{e = }")
else:
    print("no error")

image = pygame.Surface((5, 5), pygame.SRCALPHA)
try:
    image.fill((255, 0, 0), special_flags=pygame.BLEND_PREMULTIPLIED)
except pygame.error as e:
    print(f"{e = }")
else:
    print("no error")

Currently it either simply raises a pygame.error without any error message or I have managed to get Surface doesn't have a colorkey in this case:

import pygame

pygame.display.set_mode(flags=pygame.HIDDEN)
image = pygame.image.load("scenery.jpg").convert()
try:
    image.fill((255, 0, 0), special_flags=pygame.BLEND_PREMULTIPLIED)
except pygame.error as e:
    print(f"{e = }")
else:
    print("no error")

@Matiiss Matiiss requested a review from a team as a code owner September 3, 2023 19:48
@Matiiss Matiiss added Code quality/robustness Code quality and resilience to changes Surface pygame.Surface labels Sep 3, 2023
@Matiiss
Copy link
Member Author

Matiiss commented Sep 3, 2023

Is code robustness an appropriate label in this case?

@Matiiss
Copy link
Member Author

Matiiss commented Sep 5, 2023

Maybe it should say "given blend flag is not supported for this operation" instead?

Copy link
Member

@ankith26 ankith26 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the PR!

Some other notes up for discussion, but IMO none blockers for this PR

  • Maybe a small unit test would be nice
  • Should the python error returned be a ValueError?

@Matiiss
Copy link
Member Author

Matiiss commented Sep 7, 2023

@ankith26
About the ValueError, as I see it, that would require returning some other value as the result, for example, -2 and then adding an else if after this block

pygame-ce/src_c/surface.c

Lines 1857 to 1858 in 0168adc

if (result == -1)
return RAISE(pgExc_SDLError, SDL_GetError());

to check for that value and raise a separate error. But that would make more sense I guess.

I will add a test though, since currently I couldn't find any that would test for any errors raised by Surface.fill.

Copy link
Member

@zoldalma999 zoldalma999 left a comment

Choose a reason for hiding this comment

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

LGTM!

@zoldalma999 zoldalma999 added this to the 2.4.0 milestone Sep 8, 2023
@Starbuck5
Copy link
Member

What if you set the error message with SDL_SetError and then have it handled by the existing SDL error handler?

@Matiiss
Copy link
Member Author

Matiiss commented Sep 9, 2023

What if you set the error message with SDL_SetError and then have it handled by the existing SDL error handler?

That is what the first commit did, however, Ankith suggested raising a ValueError and I thought it would be more suitable too, so changed to that.

@Starbuck5
Copy link
Member

I think this should be an SDL error, since it should look like it's coming from an SDL fill system. Just like how in alphablit.c we implement a blitter and use SDL_SetError there.

Updated tests

Changing back to an sdl error

Changed to raise a ValueError, added tests

formatting
@Matiiss Matiiss force-pushed the matiiss-improve-surf-fill-spec-flags-err-msg branch from 773ab0f to 458e92f Compare September 9, 2023 10:37
Copy link
Member

@ankith26 ankith26 left a comment

Choose a reason for hiding this comment

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

I'm fine with pygame.error

@ankith26 ankith26 merged commit 3aa1b9d into pygame-community:main Sep 9, 2023
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Code quality/robustness Code quality and resilience to changes Surface pygame.Surface
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants