Show More
@@ -233,13 +233,26 b' class Config(dict):' | |||||
233 |
|
233 | |||
234 | def copy(self): |
|
234 | def copy(self): | |
235 | return type(self)(dict.copy(self)) |
|
235 | return type(self)(dict.copy(self)) | |
|
236 | # copy nested config objects | |||
|
237 | for k, v in self.items(): | |||
|
238 | if isinstance(v, Config): | |||
|
239 | new_config[k] = v.copy() | |||
|
240 | return new_config | |||
236 |
|
241 | |||
237 | def __copy__(self): |
|
242 | def __copy__(self): | |
238 | return self.copy() |
|
243 | return self.copy() | |
239 |
|
244 | |||
240 | def __deepcopy__(self, memo): |
|
245 | def __deepcopy__(self, memo): | |
241 | import copy |
|
246 | new_config = type(self)() | |
242 | return type(self)(copy.deepcopy(list(self.items()))) |
|
247 | for key, value in self.items(): | |
|
248 | if isinstance(value, (Config, LazyConfigValue)): | |||
|
249 | # deep copy config objects | |||
|
250 | value = copy.deepcopy(value, memo) | |||
|
251 | elif type(value) in {dict, list, set, tuple}: | |||
|
252 | # shallow copy plain container traits | |||
|
253 | value = copy.copy(value) | |||
|
254 | new_config[key] = value | |||
|
255 | return new_config | |||
243 |
|
256 | |||
244 | def __getitem__(self, key): |
|
257 | def __getitem__(self, key): | |
245 | try: |
|
258 | try: |
@@ -4,6 +4,8 b'' | |||||
4 | # Copyright (c) IPython Development Team. |
|
4 | # Copyright (c) IPython Development Team. | |
5 | # Distributed under the terms of the Modified BSD License. |
|
5 | # Distributed under the terms of the Modified BSD License. | |
6 |
|
6 | |||
|
7 | import copy | |||
|
8 | import logging | |||
7 | import os |
|
9 | import os | |
8 | import pickle |
|
10 | import pickle | |
9 | import sys |
|
11 | import sys | |
@@ -319,11 +321,15 b' class TestConfig(TestCase):' | |||||
319 | c1.Foo.bam = 30 |
|
321 | c1.Foo.bam = 30 | |
320 | c1.a = 'asdf' |
|
322 | c1.a = 'asdf' | |
321 | c1.b = range(10) |
|
323 | c1.b = range(10) | |
322 | import copy |
|
324 | c1.Test.logger = logging.Logger('test') | |
|
325 | c1.Test.get_logger = logging.getLogger('test') | |||
323 | c2 = copy.deepcopy(c1) |
|
326 | c2 = copy.deepcopy(c1) | |
324 | self.assertEqual(c1, c2) |
|
327 | self.assertEqual(c1, c2) | |
325 | self.assertTrue(c1 is not c2) |
|
328 | self.assertTrue(c1 is not c2) | |
326 | self.assertTrue(c1.Foo is not c2.Foo) |
|
329 | self.assertTrue(c1.Foo is not c2.Foo) | |
|
330 | self.assertTrue(c1.Test is not c2.Test) | |||
|
331 | self.assertTrue(c1.Test.logger is c2.Test.logger) | |||
|
332 | self.assertTrue(c1.Test.get_logger is c2.Test.get_logger) | |||
327 |
|
333 | |||
328 | def test_builtin(self): |
|
334 | def test_builtin(self): | |
329 | c1 = Config() |
|
335 | c1 = Config() |
General Comments 0
You need to be logged in to leave comments.
Login now