Show More
@@ -11,6 +11,7 b' import os' | |||||
11 | import re |
|
11 | import re | |
12 | import sys |
|
12 | import sys | |
13 | import json |
|
13 | import json | |
|
14 | from ast import literal_eval | |||
14 |
|
15 | |||
15 | from IPython.utils.path import filefind, get_ipython_dir |
|
16 | from IPython.utils.path import filefind, get_ipython_dir | |
16 | from IPython.utils import py3compat |
|
17 | from IPython.utils import py3compat | |
@@ -487,7 +488,7 b' class CommandLineConfigLoader(ConfigLoader):' | |||||
487 | """execute self.config.<lhs> = <rhs> |
|
488 | """execute self.config.<lhs> = <rhs> | |
488 |
|
489 | |||
489 | * expands ~ with expanduser |
|
490 | * expands ~ with expanduser | |
490 |
* tries to assign with |
|
491 | * tries to assign with literal_eval, otherwise assigns with just the string, | |
491 | allowing `--C.a=foobar` and `--C.a="foobar"` to be equivalent. *Not* |
|
492 | allowing `--C.a=foobar` and `--C.a="foobar"` to be equivalent. *Not* | |
492 | equivalent are `--C.a=4` and `--C.a='4'`. |
|
493 | equivalent are `--C.a=4` and `--C.a='4'`. | |
493 | """ |
|
494 | """ | |
@@ -496,8 +497,8 b' class CommandLineConfigLoader(ConfigLoader):' | |||||
496 | # Try to see if regular Python syntax will work. This |
|
497 | # Try to see if regular Python syntax will work. This | |
497 | # won't handle strings as the quote marks are removed |
|
498 | # won't handle strings as the quote marks are removed | |
498 | # by the system shell. |
|
499 | # by the system shell. | |
499 | value = eval(rhs) |
|
500 | value = literal_eval(rhs) | |
500 | except (NameError, SyntaxError): |
|
501 | except (NameError, SyntaxError, ValueError): | |
501 | # This case happens if the rhs is a string. |
|
502 | # This case happens if the rhs is a string. | |
502 | value = rhs |
|
503 | value = rhs | |
503 |
|
504 |
@@ -33,7 +33,7 b' c = get_config()' | |||||
33 | c.a=10 |
|
33 | c.a=10 | |
34 | c.b=20 |
|
34 | c.b=20 | |
35 | c.Foo.Bar.value=10 |
|
35 | c.Foo.Bar.value=10 | |
36 | c.Foo.Bam.value=list(range(10)) # list() is just so it's the same on Python 3 |
|
36 | c.Foo.Bam.value=list(range(10)) | |
37 | c.D.C.value='hi there' |
|
37 | c.D.C.value='hi there' | |
38 | """ |
|
38 | """ | |
39 |
|
39 | |||
@@ -188,14 +188,23 b' class TestArgParseCL(TestCase):' | |||||
188 | class TestKeyValueCL(TestCase): |
|
188 | class TestKeyValueCL(TestCase): | |
189 | klass = KeyValueConfigLoader |
|
189 | klass = KeyValueConfigLoader | |
190 |
|
190 | |||
|
191 | def test_eval(self): | |||
|
192 | cl = self.klass(log=log) | |||
|
193 | config = cl.load_config('--Class.str_trait=all --Class.int_trait=5 --Class.list_trait=["hello",5]'.split()) | |||
|
194 | self.assertEqual(config.Class.str_trait, 'all') | |||
|
195 | self.assertEqual(config.Class.int_trait, 5) | |||
|
196 | self.assertEqual(config.Class.list_trait, ["hello", 5]) | |||
|
197 | ||||
191 | def test_basic(self): |
|
198 | def test_basic(self): | |
192 | cl = self.klass(log=log) |
|
199 | cl = self.klass(log=log) | |
193 |
argv = ['--'+s |
|
200 | argv = [ '--' + s[2:] for s in pyfile.split('\n') if s.startswith('c.') ] | |
|
201 | print(argv) | |||
194 | config = cl.load_config(argv) |
|
202 | config = cl.load_config(argv) | |
195 | self.assertEqual(config.a, 10) |
|
203 | self.assertEqual(config.a, 10) | |
196 | self.assertEqual(config.b, 20) |
|
204 | self.assertEqual(config.b, 20) | |
197 | self.assertEqual(config.Foo.Bar.value, 10) |
|
205 | self.assertEqual(config.Foo.Bar.value, 10) | |
198 | self.assertEqual(config.Foo.Bam.value, list(range(10))) |
|
206 | # non-literal expressions are not evaluated | |
|
207 | self.assertEqual(config.Foo.Bam.value, 'list(range(10))') | |||
199 | self.assertEqual(config.D.C.value, 'hi there') |
|
208 | self.assertEqual(config.D.C.value, 'hi there') | |
200 |
|
209 | |||
201 | def test_expanduser(self): |
|
210 | def test_expanduser(self): |
General Comments 0
You need to be logged in to leave comments.
Login now