• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
10
11class Error(Exception):
12    pass
13
14
15__all__ = ["Error", "deepcopy"]
16
17
18def deepcopy(x):
19    """Deep copy operation on gyp objects such as strings, ints, dicts
20  and lists. More than twice as fast as copy.deepcopy but much less
21  generic."""
22
23    try:
24        return _deepcopy_dispatch[type(x)](x)
25    except KeyError:
26        raise Error(
27            "Unsupported type %s for deepcopy. Use copy.deepcopy "
28            + "or expand simple_copy support." % type(x)
29        )
30
31
32_deepcopy_dispatch = d = {}
33
34
35def _deepcopy_atomic(x):
36    return x
37
38
39types = bool, float, int, str, type, type(None)
40
41for x in types:
42    d[x] = _deepcopy_atomic
43
44
45def _deepcopy_list(x):
46    return [deepcopy(a) for a in x]
47
48
49d[list] = _deepcopy_list
50
51
52def _deepcopy_dict(x):
53    y = {}
54    for key, value in x.items():
55        y[deepcopy(key)] = deepcopy(value)
56    return y
57
58
59d[dict] = _deepcopy_dict
60
61del d
62