1 # Copyright 2014 Google Inc. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """A clone of the default copy.deepcopy that doesn't handle cyclic
6 structures or complex types except for dicts and lists. This is
7 because gyp copies so large structure that small copy overhead ends up
8 taking seconds in a project the size of Chromium."""
10 class Error(Exception):
13 __all__ = ["Error", "deepcopy"]
16 """Deep copy operation on gyp objects such as strings, ints, dicts
17 and lists. More than twice as fast as copy.deepcopy but much less
21 return _deepcopy_dispatch[type(x)](x)
23 raise Error('Unsupported type %s for deepcopy. Use copy.deepcopy ' +
24 'or expand simple_copy support.' % type(x))
26 _deepcopy_dispatch = d = {}
28 def _deepcopy_atomic(x):
31 for x in (type(None), int, long, float,
32 bool, str, unicode, type):
33 d[x] = _deepcopy_atomic
35 def _deepcopy_list(x):
36 return [deepcopy(a) for a in x]
37 d[list] = _deepcopy_list
39 def _deepcopy_dict(x):
41 for key, value in x.iteritems():
42 y[deepcopy(key)] = deepcopy(value)
44 d[dict] = _deepcopy_dict