pygame-ce icon indicating copy to clipboard operation
pygame-ce copied to clipboard

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

Open jeremycryan opened this issue 1 year ago • 2 comments
trafficstars

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 avatar May 13 '24 00:05 jeremycryan

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

oddbookworm avatar May 13 '24 01:05 oddbookworm

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.

jeremycryan avatar May 13 '24 01:05 jeremycryan