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

Special characters in file name throws error #53

Open
muizzsiddique opened this issue Sep 24, 2020 · 6 comments
Open

Special characters in file name throws error #53

muizzsiddique opened this issue Sep 24, 2020 · 6 comments

Comments

@muizzsiddique
Copy link

Specifically the character that can be created on Windows using Alt+0150: '–'. As soon as I rename a file without this the file can be loaded. I tested this both in PyOpenAL and using pyogg.VorbisFile.

This is the error that happens (running in python interactive environment):

>>> pyogg.VorbisFile("50 Cent – Many Men (Wish Death) [Edit].ogg")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\muizz\AppData\Local\Programs\Python\Python38\lib\site-packages\pyogg\__init__.py", line 33, in __init__
raise PyOggError("file couldn't be opened or doesn't exist. Error code : {}".format(error))
pyogg.ogg.PyOggError: file couldn't be opened or doesn't exist. Error code : -1

@mattgwwalker
Copy link
Collaborator

mattgwwalker commented Sep 24, 2020 via email

@muizzsiddique
Copy link
Author

muizzsiddique commented Sep 25, 2020

I think I only have a beginner's grasp on Python/programming but I would love to try and contribute.

Edit: As I thought, this looks far out my scope. I'm still interested but I don't think my abilities are good enough to contribute.

@mattgwwalker
Copy link
Collaborator

mattgwwalker commented Sep 25, 2020 via email

@mattgwwalker
Copy link
Collaborator

Ah, sorry, I didn't see your edit until after I replied. Feel free to ignore my previous reply, or give it a go, as you wish.

Matthew

@mattgwwalker
Copy link
Collaborator

@Zuzu-Typ, I'm looking into fixing this issue. It turns out to be a lot more hairy than I expected. Would you have any comments in this space?

First, we're currently using ov_fopen, which takes a char *. I'm assuming this is the function that isn't Unicode-friendly.

Another option is ov_open, which contains a specific warning for Windows users not to use this function. So we can eliminate this option.

The final option (if we stick with the vorbisfile library) is ov_open_callbacks, which is specifically Windows-friendly. This function requires a FILE pointer.

However, getting a FILE pointer from Python isn't easy. Instead, the recommended approach seems to be to use the C standard library and call fopen using ctypes. However that's tricky given that there seems to be no standard way to obtain access to the C standard library (there is code for Windows but we'd have to specify the library for Mac and Linux differently).

Have you ever done something similar? Do you know of a better way around all this?

@Zuzu-Typ
Copy link
Collaborator

Zuzu-Typ commented Dec 9, 2020

Well, it would make no sense to use the fopen function to get a FILE pointer, because that's exactly what ov_fopen does anyways.

I've found this thread on StackOverflow, which recommends the use of GetShortPathName.

I haven't had the time to really test this myself, but I've created a simple implementation which looks promising:

def convert_pathname(pathname):
    buffer = (ctypes.c_wchar * 1024)()
    lpwstr = ctypes.cast(buffer, ctypes.wintypes.LPWSTR)
    ctypes.windll.kernel32.GetShortPathNameW(pathname, lpwstr, 1024)
    return lpwstr.value

And I did try it out for some unicode path name:

>>> convert_pathname("C:/ÄÖÜgaß")
'C:/GA3910~1'

It will only work for existing, full paths though (for the latter os.path.realpath could be used if a relative path is given).
Otherwise it simply returns an empty string.

And obviously it's also limited to about 1000 characters, which could be adjusted in the future if that should be necessary, if paths longer than 256 characters are even supported in the first place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants