From 4427322b34e9f05dcd65188918a583faeb5f8d08 2011-10-25 00:37:01 From: MinRK Date: 2011-10-25 00:37:01 Subject: [PATCH] expand '~' with path.expanduser in KVLoader tests included --- diff --git a/IPython/config/loader.py b/IPython/config/loader.py index 588251f..7bb8cf3 100644 --- a/IPython/config/loader.py +++ b/IPython/config/loader.py @@ -19,6 +19,7 @@ Authors #----------------------------------------------------------------------------- import __builtin__ as builtin_mod +import os import re import sys @@ -332,6 +333,14 @@ class CommandLineConfigLoader(ConfigLoader): """ def _exec_config_str(self, lhs, rhs): + """execute self.config.= + + * expands ~ with expanduser + * tries to assign with raw exec, otherwise assigns with just the string, + allowing `--C.a=foobar` and `--C.a="foobar"` to be equivalent. *Not* + equivalent are `--C.a=4` and `--C.a='4'`. + """ + rhs = os.path.expanduser(rhs) exec_str = 'self.config.' + lhs + '=' + rhs try: # Try to see if regular Python syntax will work. This @@ -343,7 +352,7 @@ class CommandLineConfigLoader(ConfigLoader): # the quote marks. Use repr, to get quote marks, and # 'u' prefix and see if # it succeeds. If it still fails, we let it raise. - exec_str = u'self.config.' + lhs + '=' + repr(rhs) + exec_str = u'self.config.' + lhs + '= rhs' exec exec_str in locals(), globals() def _load_flag(self, cfg): diff --git a/IPython/config/tests/test_loader.py b/IPython/config/tests/test_loader.py index c37cac8..6b4330e 100644 --- a/IPython/config/tests/test_loader.py +++ b/IPython/config/tests/test_loader.py @@ -131,6 +131,16 @@ class TestKeyValueCL(TestCase): self.assertEquals(config.Foo.Bam.value, range(10)) self.assertEquals(config.D.C.value, 'hi there') + def test_expanduser(self): + cl = self.klass() + argv = ['--a=~/1/2/3', '--b=~', '--c=~/', '--d="~/"'] + with mute_warn(): + config = cl.load_config(argv) + self.assertEquals(config.a, os.path.expanduser('~/1/2/3')) + self.assertEquals(config.b, os.path.expanduser('~')) + self.assertEquals(config.c, os.path.expanduser('~/')) + self.assertEquals(config.d, '~/') + def test_extra_args(self): cl = self.klass() with mute_warn(): @@ -172,6 +182,14 @@ class TestKeyValueCL(TestCase): class TestArgParseKVCL(TestKeyValueCL): klass = KVArgParseConfigLoader + def test_expanduser2(self): + cl = self.klass() + argv = ['-a', '~/1/2/3', '--b', "'~/1/2/3'"] + with mute_warn(): + config = cl.load_config(argv, aliases=dict(a='A.a', b='A.b')) + self.assertEquals(config.A.a, os.path.expanduser('~/1/2/3')) + self.assertEquals(config.A.b, '~/1/2/3') + class TestConfig(TestCase): def test_setget(self):