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. 4 5"""A clone of the default copy.deepcopy that doesn't handle cyclic 6structures or complex types except for dicts and lists. This is 7because gyp copies so large structure that small copy overhead ends up 8taking seconds in a project the size of Chromium.""" 9 10class Error(Exception): 11 pass 12 13__all__ = ["Error", "deepcopy"] 14 15def deepcopy(x): 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 18 generic.""" 19 20 try: 21 return _deepcopy_dispatch[type(x)](x) 22 except KeyError: 23 raise Error('Unsupported type %s for deepcopy. Use copy.deepcopy ' + 24 'or expand simple_copy support.' % type(x)) 25 26_deepcopy_dispatch = d = {} 27 28def _deepcopy_atomic(x): 29 return x 30 31for x in (type(None), int, long, float, 32 bool, str, unicode, type): 33 d[x] = _deepcopy_atomic 34 35def _deepcopy_list(x): 36 return [deepcopy(a) for a in x] 37d[list] = _deepcopy_list 38 39def _deepcopy_dict(x): 40 y = {} 41 for key, value in x.iteritems(): 42 y[deepcopy(key)] = deepcopy(value) 43 return y 44d[dict] = _deepcopy_dict 45 46del d 47