Show More
@@ -11,6 +11,7 b' import os' | |||
|
11 | 11 | import re |
|
12 | 12 | import sys |
|
13 | 13 | import json |
|
14 | from ast import literal_eval | |
|
14 | 15 | |
|
15 | 16 | from IPython.utils.path import filefind, get_ipython_dir |
|
16 | 17 | from IPython.utils import py3compat |
@@ -487,7 +488,7 b' class CommandLineConfigLoader(ConfigLoader):' | |||
|
487 | 488 | """execute self.config.<lhs> = <rhs> |
|
488 | 489 | |
|
489 | 490 | * expands ~ with expanduser |
|
490 |
* tries to assign with |
|
|
491 | * tries to assign with literal_eval, otherwise assigns with just the string, | |
|
491 | 492 | allowing `--C.a=foobar` and `--C.a="foobar"` to be equivalent. *Not* |
|
492 | 493 | equivalent are `--C.a=4` and `--C.a='4'`. |
|
493 | 494 | """ |
@@ -496,8 +497,8 b' class CommandLineConfigLoader(ConfigLoader):' | |||
|
496 | 497 | # Try to see if regular Python syntax will work. This |
|
497 | 498 | # won't handle strings as the quote marks are removed |
|
498 | 499 | # by the system shell. |
|
499 | value = eval(rhs) | |
|
500 | except (NameError, SyntaxError): | |
|
500 | value = literal_eval(rhs) | |
|
501 | except (NameError, SyntaxError, ValueError): | |
|
501 | 502 | # This case happens if the rhs is a string. |
|
502 | 503 | value = rhs |
|
503 | 504 |
@@ -33,7 +33,7 b' c = get_config()' | |||
|
33 | 33 | c.a=10 |
|
34 | 34 | c.b=20 |
|
35 | 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 | 37 | c.D.C.value='hi there' |
|
38 | 38 | """ |
|
39 | 39 | |
@@ -188,14 +188,23 b' class TestArgParseCL(TestCase):' | |||
|
188 | 188 | class TestKeyValueCL(TestCase): |
|
189 | 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 | 198 | def test_basic(self): |
|
192 | 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 | 202 | config = cl.load_config(argv) |
|
195 | 203 | self.assertEqual(config.a, 10) |
|
196 | 204 | self.assertEqual(config.b, 20) |
|
197 | 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 | 208 | self.assertEqual(config.D.C.value, 'hi there') |
|
200 | 209 | |
|
201 | 210 | def test_expanduser(self): |
General Comments 0
You need to be logged in to leave comments.
Login now