Merge pull request #746 from lbryio/windows_path_fix

Make sure window paths of directories are in string not unicode
This commit is contained in:
Umpei Kay Kurokawa 2017-07-04 14:25:45 -04:00 committed by GitHub
commit c79ecdb0dc
3 changed files with 28 additions and 1 deletions

View file

@ -29,6 +29,7 @@ at anytime.
* Fixed BlobManager causing functional tests to fail, removed its unneeded manage() loop
* Increased max_key_fee
* Fixed unit tests on appveyor Windows build
* Fixed [#692](https://github.com/lbryio/lbry/issues/692)
### Deprecated
*

View file

@ -43,6 +43,21 @@ settings_encoders = {
'.yml': yaml.safe_dump
}
def _win_path_to_bytes(path):
"""
Encode Windows paths to string. appdirs.user_data_dir()
on windows will return unicode path, unlike other platforms
which returns string. This will cause problems
because we use strings for filenames and combining them with
os.path.join() will result in errors.
"""
for encoding in ('ASCII', 'MBCS'):
try:
return path.encode(encoding)
except (UnicodeEncodeError, LookupError):
pass
return path
if sys.platform.startswith('darwin'):
platform = DARWIN
default_download_directory = os.path.join(os.path.expanduser('~'), 'Downloads')
@ -57,6 +72,10 @@ elif sys.platform.startswith('win'):
get_path(FOLDERID.RoamingAppData, UserHandle.current), 'lbrynet')
default_lbryum_dir = os.path.join(
get_path(FOLDERID.RoamingAppData, UserHandle.current), 'lbryum')
default_download_directory = _win_path_to_bytes(default_download_directory)
default_data_dir = _win_path_to_bytes(default_data_dir)
default_lbryum_dir = _win_path_to_bytes(default_lbryum_dir)
else:
platform = LINUX
default_download_directory = os.path.join(os.path.expanduser('~'), 'Downloads')

View file

@ -1,7 +1,6 @@
import os
from twisted.trial import unittest
from lbrynet import conf
@ -54,3 +53,11 @@ class SettingsTest(unittest.TestCase):
self.assertEqual('cli_test_string', settings['test'])
settings.set('test', 'runtime_takes_precedence', data_types=(conf.TYPE_RUNTIME,))
self.assertEqual('runtime_takes_precedence', settings['test'])
def test_data_dir(self):
# check if these directories are returned as string and not unicode
# otherwise there will be problems when calling os.path.join on
# unicode directory names with string file names
self.assertEqual(str, type(conf.default_download_directory))
self.assertEqual(str, type(conf.default_data_dir))
self.assertEqual(str, type(conf.default_lbryum_dir))