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 31try: 32 _string_types = (str, unicode) 33# There's no unicode in python3 34except NameError: 35 _string_types = (str, ) 36 37try: 38 _integer_types = (int, long) 39# There's no long in python3 40except NameError: 41 _integer_types = (int, ) 42 43for x in (type(None), float, bool, type) + _integer_types + _string_types: 44 d[x] = _deepcopy_atomic 45 46def _deepcopy_list(x): 47 return [deepcopy(a) for a in x] 48d[list] = _deepcopy_list 49 50def _deepcopy_dict(x): 51 y = {} 52 for key, value in x.items(): 53 y[deepcopy(key)] = deepcopy(value) 54 return y 55d[dict] = _deepcopy_dict 56 57del d 58