pygame.mixer.music.load doesn't work with unicode symbols in path
== Vasiliy, 2009-09-01 11:32:23 -0700
{{{ This method doesn't work with unicode symbols in os path. For example: audiofile = u'D:/Work/Projects/Programming/iTutor/src/iTutor/dictionaries/Казахо - Русский/3.mp3' pygame.mixer.music.load(audiofile)
return error: pygame.error: Couldn't read from 'D:/Work/Projects/Programming/iTutor/src/iTutor/dictionaries/Казахо - Русский/3.mp3'
if i'm delete 'Казахо - Русский/' from audiofile variable then method working normally. }}}
Comments (10)
-
-
According to the Python 2.7 sys.getfilesystemencoding() docs no encoding is done to the file path. So the UTF-16 Python string is just passed through. I don't know what encoding Windows uses, thought.
-
I think this might be a bytes/unicode misunderstanding: I've tested this only on linux, on an utf8 filesystem, but pygame.mixer.music.load('<utf8 encoded string>') worked, whereas pygame.mixer.music.load(u'<utf8 encoded string>') didn't., but pygame.mixer.music.load(u'<utf8 encoded string>'.encode('utf8')) again did.
-
- edited description
- changed version to 1.9.2 (pre-release)
- changed component to other
-
assigned issue to
- changed milestone to 1.9.2
-
- changed component to mixer.music
- edited description
My plan is to wrap pygame.mixer.music.load and pygame.mixer.music.queue with a function that uses Python's built-in open function to open Unicode paths.
-
Issue
#247was marked as a duplicate of this issue. -
Actually, the <unicode string>.encode('utf8') trick from Anonymous works on Windows for music.
-
I've found the relevant source in
music.c
, and it's usingRWopsEncodeFilePath
to turn a unicode path into a bytes path. This is OK on Unix systems where paths are bytes, but Windows paths are unicode (handled as UTF-16). The 'filesystem encoding' on Windows is'mbcs'
, which refers to the current system codepage. However, the typical default codepages (e.g. cp1252 for Western European languages) cannot represent all characters that might be in a path, so encoding a path is lossy. The right fix would be to open files using the unicode/wchar path on Windows.I haven't tested this, but it looks like it should be possible to work around this by opening the file object in Python first:
with open(path) as f: pygame.mixer.music.load(f)
I think Python's open function uses unicode paths on Windows.
-
- changed version to 1.9.2
-
- changed milestone to 1.9.x
- edited description
- Log in to comment
This works with oggs and wavs on OSX.
I assume this is another smpeg error. Or perhaps a windows related error.
Needs testing on windows.