pygame-ce
pygame-ce copied to clipboard
Add suggested paths to FileNotFound errors from C code
Fixes #2485
Test code:
import pygame
cat_surf = pygame.image.load("imajes/kool_staff/gat.png")
Directory structure:
New Output:
Traceback (most recent call last):
File "C:\Users\dan\Programming\scrap\load_cat_png.py", line 2, in <module>
cat_surf = pygame.image.load("imajes/kool_staff/gat.png")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: File not found at path: 'imajes/kool_staff/gat.png', did you mean: 'images/cool_stuff/cat.png'?
I haven't looked this over in detail yet but I notice this PR removes the "not found in working directory xyz" message, which is something I'm fond of (especially since I added it).
I haven't looked this over in detail yet but I notice this PR removes the "not found in working directory xyz" message, which is something I'm fond of (especially since I added it).
I think adding that back in to the relative path will be no problem :)
Since there were a lot of
PyObject_CallMethod(and similar) calls, decided to rewrite this code in python to see if the logic is alright, and even that python code is fairly long.
Thanks for doing this!
Do you think we could (/should) just do this in python instead of c?
Maybe! When I originally started this PR I was going to approach it with a C library for Ratcliff-Obershelp similarity, but then realised how annoying manipulating paths is in C and discovered that I could use the python os module from C code from other code in rwobject.c and then figured I could also use the difflib module in the same way.
This isn't particularly performance sensitive code either way as it only activates when you are in an 'oops there was a fuckup' situation anyway, it is more intended to help out newbies to python & pygame deal with file path mistakes.
Also, is there a reason the
suggest_valid_pathfunction raises errors? We seemingly just ignore that everywhere it is used and set something generic.
I think I added these while I was debugging the function; they are mostly relevant to someone using the function not to the end user - so probably mainly for that, but it has been a long time since I wrote this.
If anybody wants to re-implement this in python based on @zoldalma999's code above - and thinks they make it work with rwobject.c then feel free to do so.
I will leave this open as it is for now as I don't immediately know how to go about that.
This was a fun idea, but I don't think this is going to get merged as I'm not going to rewrite it in python, so I'll close it.