Skip to content

Commit

Permalink
config: Raise ValueError if Path is asked to serialize unicode
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jodal committed Jun 26, 2013
1 parent 59e3b9a commit 2ad1bb8
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 7 deletions.
3 changes: 1 addition & 2 deletions mopidy/config/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import logging
import re
import socket
import sys

from mopidy.utils import path
from mopidy.config import validators
Expand Down Expand Up @@ -257,7 +256,7 @@ def deserialize(self, value):

def serialize(self, value, display=False):
if isinstance(value, unicode):
value = value.encode(sys.getfilesystemencoding())
raise ValueError('paths should always be bytes')
if isinstance(value, ExpandedPath):
return value.original
return value
6 changes: 1 addition & 5 deletions tests/config/types_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import logging
import mock
import socket
import sys
import unittest

from mopidy.config import types
Expand Down Expand Up @@ -367,7 +366,4 @@ def test_serialize_plain_string(self):

def test_serialize_unicode_string(self):
value = types.Path()
expected = 'æøå'.encode(sys.getfilesystemencoding())
result = value.serialize('æøå')
self.assertEqual(expected, result)
self.assertIsInstance(result, bytes)
self.assertRaises(ValueError, value.serialize, 'æøå')

0 comments on commit 2ad1bb8

Please sign in to comment.