##// END OF EJS Templates
fix handling of unicode in KV loader...
MinRK -
Show More
@@ -380,6 +380,19 b' class KeyValueConfigLoader(CommandLineConfigLoader):'
380 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 396 def load_config(self, argv=None, aliases=None, flags=None):
384 397 """Parse the configuration and generate the Config object.
385 398
@@ -413,7 +426,7 b' class KeyValueConfigLoader(CommandLineConfigLoader):'
413 426 if flags is None:
414 427 flags = self.flags
415 428
416 for item in argv:
429 for item in self._decode_argv(argv):
417 430 if kv_pattern.match(item):
418 431 lhs,rhs = item.split('=',1)
419 432 # Substitute longnames for aliases.
@@ -427,9 +440,10 b' class KeyValueConfigLoader(CommandLineConfigLoader):'
427 440 exec exec_str in locals(), globals()
428 441 except (NameError, SyntaxError):
429 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 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 447 exec exec_str in locals(), globals()
434 448 elif flag_pattern.match(item):
435 449 # trim leading '--'
@@ -21,9 +21,12 b' Authors:'
21 21 #-----------------------------------------------------------------------------
22 22
23 23 import os
24 import sys
24 25 from tempfile import mkstemp
25 26 from unittest import TestCase
26 27
28 from nose import SkipTest
29
27 30 from IPython.utils.traitlets import Int, Unicode
28 31 from IPython.config.configurable import Configurable
29 32 from IPython.config.loader import (
@@ -131,6 +134,23 b' class TestKeyValueCL(TestCase):'
131 134 self.assertEquals(config.a, 5)
132 135 self.assertEquals(config.c, 10)
133 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'é')
153
134 154
135 155 class TestConfig(TestCase):
136 156
@@ -1099,7 +1099,7 b' class CaselessStrEnum(Enum):'
1099 1099 if self._allow_none:
1100 1100 return value
1101 1101
1102 if not isinstance(value, str):
1102 if not isinstance(value, basestring):
1103 1103 self.error(obj, value)
1104 1104
1105 1105 for v in self.values:
General Comments 0
You need to be logged in to leave comments. Login now