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

pygame.errror exceptions are too generic #1535

Closed
4 tasks done
bendavis78 opened this issue Dec 19, 2019 · 7 comments
Closed
4 tasks done

pygame.errror exceptions are too generic #1535

bendavis78 opened this issue Dec 19, 2019 · 7 comments
Labels

Comments

@bendavis78
Copy link

bendavis78 commented Dec 19, 2019

  • unit test for FileNotFoundError exception for pygame.image.load (good first issue)
  • investigate where in the pygame code different exceptions could be raised (rwops.c? image_ext.c?)
  • file opening code in src_c/ to raise FileNotFoundError when file isn't there.
  • look at mapping other SDL file related error codes to python exceptions. Not just FiltNotFoundError.

When loading an image with a file that doesn't exist, you get a pygame.error exception with a human-readable string description of the error. This is an anti-pattern. It would be better to allow the built-in exception IOError or FileNotFoundError to bubble up.

Let's say I want to handle an error where an image isn't found on disk. I currently have to check os.path.isfile(path). If the file does exist, and I use pygame.image.load(path), that's two file stats, which is not as efficient as simply catching the exception that is normally thrown when a file doesn't exist.

Either the original exception should be allowed to raise, or pygame should define its own version of FileNotFoundError. There may be other instances of this anti-pattern throughout pygame, but so far this is all I've come across.

@illume illume added image pygame.image mixer pygame.mixer labels Jan 19, 2020
@illume
Copy link
Member

illume commented Jan 19, 2020

This is a good enhancement.

I don't have time to do this myself anytime soon, but PRs are welcome. The first step would be to make some unit tests. I've added a task list.

@krkakade
Copy link

Can I work on it?

@vjs22334
Copy link

I would like to work on this if it's still available

@MightyJosip
Copy link
Contributor

Of course, any help would be appreciated

@jshultz
Copy link

jshultz commented Feb 8, 2020

I would be interested to work on this as well.

@illume
Copy link
Member

illume commented Apr 22, 2020

Here are a couple of error codes that IMG_GetError returns.

>>> import pygame
pygame 2.0.0.dev7 (SDL 2.0.12, python 3.7.4)
Hello from the pygame community. https://www.pygame.org/contribute.html
>>> pygame.image.load("asdf.png")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
pygame.error: Couldn't open asdf.png
>>> pygame.image.load("/etc/passwd")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
pygame.error: Unsupported image format

@illume
Copy link
Member

illume commented Apr 25, 2020

Closing thanks to the PR from @dotmars that was merged. Thanks!

@illume illume closed this as completed Apr 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants