##// END OF EJS Templates
fix handling of unicode in KV loader...
MinRK -
Show More
@@ -379,7 +379,20 b' class KeyValueConfigLoader(CommandLineConfigLoader):'
379 super(KeyValueConfigLoader, self).clear()
379 super(KeyValueConfigLoader, self).clear()
380 self.extra_args = []
380 self.extra_args = []
381
381
382
382
383 def _decode_argv(self, argv, enc=None):
384 """decode argv if bytes, using stin.encoding, falling back on default enc"""
385 uargv = []
386 if enc is None:
387 enc = sys.stdin.encoding or sys.getdefaultencoding()
388 for arg in argv:
389 if not isinstance(arg, unicode):
390 # only decode if not already decoded
391 arg = arg.decode(enc)
392 uargv.append(arg)
393 return uargv
394
395
383 def load_config(self, argv=None, aliases=None, flags=None):
396 def load_config(self, argv=None, aliases=None, flags=None):
384 """Parse the configuration and generate the Config object.
397 """Parse the configuration and generate the Config object.
385
398
@@ -413,7 +426,7 b' class KeyValueConfigLoader(CommandLineConfigLoader):'
413 if flags is None:
426 if flags is None:
414 flags = self.flags
427 flags = self.flags
415
428
416 for item in argv:
429 for item in self._decode_argv(argv):
417 if kv_pattern.match(item):
430 if kv_pattern.match(item):
418 lhs,rhs = item.split('=',1)
431 lhs,rhs = item.split('=',1)
419 # Substitute longnames for aliases.
432 # Substitute longnames for aliases.
@@ -427,9 +440,10 b' class KeyValueConfigLoader(CommandLineConfigLoader):'
427 exec exec_str in locals(), globals()
440 exec exec_str in locals(), globals()
428 except (NameError, SyntaxError):
441 except (NameError, SyntaxError):
429 # This case happens if the rhs is a string but without
442 # This case happens if the rhs is a string but without
430 # the quote marks. We add the quote marks and see if
443 # the quote marks. Use repr, to get quote marks, and
444 # 'u' prefix and see if
431 # it succeeds. If it still fails, we let it raise.
445 # it succeeds. If it still fails, we let it raise.
432 exec_str = 'self.config.' + lhs + '="' + rhs + '"'
446 exec_str = u'self.config.' + lhs + '=' + repr(rhs)
433 exec exec_str in locals(), globals()
447 exec exec_str in locals(), globals()
434 elif flag_pattern.match(item):
448 elif flag_pattern.match(item):
435 # trim leading '--'
449 # trim leading '--'
@@ -21,9 +21,12 b' Authors:'
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22
22
23 import os
23 import os
24 import sys
24 from tempfile import mkstemp
25 from tempfile import mkstemp
25 from unittest import TestCase
26 from unittest import TestCase
26
27
28 from nose import SkipTest
29
27 from IPython.utils.traitlets import Int, Unicode
30 from IPython.utils.traitlets import Int, Unicode
28 from IPython.config.configurable import Configurable
31 from IPython.config.configurable import Configurable
29 from IPython.config.loader import (
32 from IPython.config.loader import (
@@ -130,6 +133,23 b' class TestKeyValueCL(TestCase):'
130 self.assertEquals(cl.extra_args, ['b', 'd'])
133 self.assertEquals(cl.extra_args, ['b', 'd'])
131 self.assertEquals(config.a, 5)
134 self.assertEquals(config.a, 5)
132 self.assertEquals(config.c, 10)
135 self.assertEquals(config.c, 10)
136
137 def test_unicode_args(self):
138 cl = KeyValueConfigLoader()
139 argv = [u'a=épsîlön']
140 config = cl.load_config(argv)
141 self.assertEquals(config.a, u'épsîlön')
142
143 def test_unicode_bytes_args(self):
144 uarg = u'a=é'
145 try:
146 barg = uarg.encode(sys.stdin.encoding)
147 except (TypeError, UnicodeEncodeError):
148 raise SkipTest("sys.stdin.encoding can't handle 'é'")
149
150 cl = KeyValueConfigLoader()
151 config = cl.load_config([barg])
152 self.assertEquals(config.a, u'é')
133
153
134
154
135 class TestConfig(TestCase):
155 class TestConfig(TestCase):
@@ -1099,7 +1099,7 b' class CaselessStrEnum(Enum):'
1099 if self._allow_none:
1099 if self._allow_none:
1100 return value
1100 return value
1101
1101
1102 if not isinstance(value, str):
1102 if not isinstance(value, basestring):
1103 self.error(obj, value)
1103 self.error(obj, value)
1104
1104
1105 for v in self.values:
1105 for v in self.values:
General Comments 0
You need to be logged in to leave comments. Login now