config: Raise ValueError if Path is asked to serialize unicode

If we accept unicode and try to encode using sys.getfilesystemencoding() then
it may work most of the time, but will fail if we get non-ASCII chars in the
unicode string and the file system encoding is e.g. ANSI-something because the
locale is C. Thus, I figure it is better to always fail if we try to serialize
Path from unicode strings. Paths should be maintained as bytes all the time.
This commit is contained in:
Stein Magnus Jodal 2013-06-27 00:08:05 +02:00
parent 59e3b9aec3
commit 2ad1bb8bb3
2 changed files with 2 additions and 7 deletions

View File

@ -3,7 +3,6 @@ from __future__ import unicode_literals
import logging import logging
import re import re
import socket import socket
import sys
from mopidy.utils import path from mopidy.utils import path
from mopidy.config import validators from mopidy.config import validators
@ -257,7 +256,7 @@ class Path(ConfigValue):
def serialize(self, value, display=False): def serialize(self, value, display=False):
if isinstance(value, unicode): if isinstance(value, unicode):
value = value.encode(sys.getfilesystemencoding()) raise ValueError('paths should always be bytes')
if isinstance(value, ExpandedPath): if isinstance(value, ExpandedPath):
return value.original return value.original
return value return value

View File

@ -5,7 +5,6 @@ from __future__ import unicode_literals
import logging import logging
import mock import mock
import socket import socket
import sys
import unittest import unittest
from mopidy.config import types from mopidy.config import types
@ -367,7 +366,4 @@ class PathTest(unittest.TestCase):
def test_serialize_unicode_string(self): def test_serialize_unicode_string(self):
value = types.Path() value = types.Path()
expected = 'æøå'.encode(sys.getfilesystemencoding()) self.assertRaises(ValueError, value.serialize, 'æøå')
result = value.serialize('æøå')
self.assertEqual(expected, result)
self.assertIsInstance(result, bytes)