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

Unhelpful error message in pygame.transform.gaussian_blur() with zero-dimensioned surfaces #2849

Closed
jeremycryan opened this issue May 13, 2024 · 2 comments · Fixed by #2852
Closed
Labels
bug Not working as intended

Comments

@jeremycryan
Copy link

jeremycryan commented May 13, 2024

Environment:

  • Operating system Windows 7
  • Python version 3.9.2
  • SDL version 2.28.5
  • pygame-ce version 2.4.1
  • Relevant hardware N/A

Current behavior:

When a 0-by-0 surface is passed as an argument to pygame.transform.gaussian_blur(), it raises the following exception:

ValueError: Blur routines do not support dest_surfaces that share pixels with the source surface. Likely the surfaces are the same, one of them is a subsurface, or they are sharing the same buffer.

This is doubly misleading because:

  • It doesn't help the user solve the actual problem (a size-zero surface as an argument)
  • It misleadingly directs the user to a specific keyword argument that is not part of the issue, which they might not even be using

This also happens for any surface with 0 as only one of its dimensions (10x0, 0x30).

Code snippet to test this behavior:

import pygame
pygame.init()
surf = pygame.Surface((0, 0))
surf = pygame.transform.gaussian_blur(surf, 20)

Expected behavior:

Two potential fixes:

  1. Don't raise an exception. If a 0-by-0 surface is passed in, just return a new 0-by-0 surface through a copy or similar.
  2. Raise a ValueError with a different error message that specifically mentions the dimensions of the surface argument.

I'd probably personally prefer #1, since it's less likely to cause errors in code flows that might produce surfaces of variable (and sometimes zero) size, and it's a fairly intuitive way to handle size-zero surfaces. I encountered this after trying to blur the output of Pygame.font.Font.render() for an empty string.

@jeremycryan jeremycryan added the bug Not working as intended label May 13, 2024
@oddbookworm
Copy link
Member

I didn’t even think it was possible to construct a surface with a zero dimension

@jeremycryan
Copy link
Author

jeremycryan commented May 13, 2024

I didn’t even think it was possible to construct a surface with a zero dimension

It certainly isn't useful most of the time --- but lots of existing surface operations handle it gracefully and as expected (usually doing nothing).

The big one is probably pygame.font.Font.render() which you might want to draw nothing when a string is empty (such as a user not having typed anything in a text box yet). It currently returns a 0x0 surface when given an empty string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Not working as intended
Projects
None yet
2 participants