home *** CD-ROM | disk | FTP | other *** search
/ com!online 2005 April / com_0405_1.iso / opensource / BTpp-0.5.4-bin.exe / $INSTDIR / BT++.exe / copy.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-04-19  |  9.3 KB  |  391 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.2)
  3.  
  4. import types
  5.  
  6. class Error(Exception):
  7.     pass
  8.  
  9. error = Error
  10.  
  11. try:
  12.     from org.python.core import PyStringMap
  13. except ImportError:
  14.     PyStringMap = None
  15.  
  16. __all__ = [
  17.     'Error',
  18.     'error',
  19.     'copy',
  20.     'deepcopy']
  21.  
  22. def copy(x):
  23.     
  24.     try:
  25.         copierfunction = _copy_dispatch[type(x)]
  26.     except KeyError:
  27.         
  28.         try:
  29.             copier = x.__copy__
  30.         except AttributeError:
  31.             
  32.             try:
  33.                 reductor = x.__reduce__
  34.             except AttributeError:
  35.                 raise error, 'un(shallow)copyable object of type %s' % type(x)
  36.  
  37.             y = _reconstruct(x, reductor(), 0)
  38.  
  39.         y = copier()
  40.  
  41.     y = copierfunction(x)
  42.     return y
  43.  
  44. _copy_dispatch = d = { }
  45.  
  46. def _copy_atomic(x):
  47.     return x
  48.  
  49. d[types.NoneType] = _copy_atomic
  50. d[types.IntType] = _copy_atomic
  51. d[types.LongType] = _copy_atomic
  52. d[types.FloatType] = _copy_atomic
  53.  
  54. try:
  55.     d[types.ComplexType] = _copy_atomic
  56. except AttributeError:
  57.     pass
  58.  
  59. d[types.StringType] = _copy_atomic
  60.  
  61. try:
  62.     d[types.UnicodeType] = _copy_atomic
  63. except AttributeError:
  64.     pass
  65.  
  66.  
  67. try:
  68.     d[types.CodeType] = _copy_atomic
  69. except AttributeError:
  70.     pass
  71.  
  72. d[types.TypeType] = _copy_atomic
  73. d[types.XRangeType] = _copy_atomic
  74. d[types.ClassType] = _copy_atomic
  75.  
  76. def _copy_list(x):
  77.     return x[:]
  78.  
  79. d[types.ListType] = _copy_list
  80.  
  81. def _copy_tuple(x):
  82.     return x[:]
  83.  
  84. d[types.TupleType] = _copy_tuple
  85.  
  86. def _copy_dict(x):
  87.     return x.copy()
  88.  
  89. d[types.DictionaryType] = _copy_dict
  90. if PyStringMap is not None:
  91.     d[PyStringMap] = _copy_dict
  92.  
  93.  
  94. def _copy_inst(x):
  95.     if hasattr(x, '__copy__'):
  96.         return x.__copy__()
  97.     
  98.     if hasattr(x, '__getinitargs__'):
  99.         args = x.__getinitargs__()
  100.         y = apply(x.__class__, args)
  101.     else:
  102.         y = _EmptyClass()
  103.         y.__class__ = x.__class__
  104.     if hasattr(x, '__getstate__'):
  105.         state = x.__getstate__()
  106.     else:
  107.         state = x.__dict__
  108.     if hasattr(y, '__setstate__'):
  109.         y.__setstate__(state)
  110.     else:
  111.         y.__dict__.update(state)
  112.     return y
  113.  
  114. d[types.InstanceType] = _copy_inst
  115. del d
  116.  
  117. def deepcopy(x, memo = None):
  118.     if memo is None:
  119.         memo = { }
  120.     
  121.     d = id(x)
  122.     if memo.has_key(d):
  123.         return memo[d]
  124.     
  125.     
  126.     try:
  127.         copierfunction = _deepcopy_dispatch[type(x)]
  128.     except KeyError:
  129.         
  130.         try:
  131.             issc = issubclass(type(x), type)
  132.         except TypeError:
  133.             issc = 0
  134.  
  135.         if issc:
  136.             y = _deepcopy_dispatch[type](x, memo)
  137.         else:
  138.             
  139.             try:
  140.                 copier = x.__deepcopy__
  141.             except AttributeError:
  142.                 
  143.                 try:
  144.                     reductor = x.__reduce__
  145.                 except AttributeError:
  146.                     raise error, 'un-deep-copyable object of type %s' % type(x)
  147.  
  148.                 y = _reconstruct(x, reductor(), 1, memo)
  149.  
  150.             y = copier(memo)
  151.     except:
  152.         issc
  153.  
  154.     y = copierfunction(x, memo)
  155.     memo[d] = y
  156.     _keep_alive(x, memo)
  157.     return y
  158.  
  159. _deepcopy_dispatch = d = { }
  160.  
  161. def _deepcopy_atomic(x, memo):
  162.     return x
  163.  
  164. d[types.NoneType] = _deepcopy_atomic
  165. d[types.IntType] = _deepcopy_atomic
  166. d[types.LongType] = _deepcopy_atomic
  167. d[types.FloatType] = _deepcopy_atomic
  168.  
  169. try:
  170.     d[types.ComplexType] = _deepcopy_atomic
  171. except AttributeError:
  172.     pass
  173.  
  174. d[types.StringType] = _deepcopy_atomic
  175.  
  176. try:
  177.     d[types.UnicodeType] = _deepcopy_atomic
  178. except AttributeError:
  179.     pass
  180.  
  181.  
  182. try:
  183.     d[types.CodeType] = _deepcopy_atomic
  184. except AttributeError:
  185.     pass
  186.  
  187. d[types.TypeType] = _deepcopy_atomic
  188. d[types.XRangeType] = _deepcopy_atomic
  189.  
  190. def _deepcopy_list(x, memo):
  191.     y = []
  192.     memo[id(x)] = y
  193.     for a in x:
  194.         y.append(deepcopy(a, memo))
  195.     
  196.     return y
  197.  
  198. d[types.ListType] = _deepcopy_list
  199.  
  200. def _deepcopy_tuple(x, memo):
  201.     y = []
  202.     for a in x:
  203.         y.append(deepcopy(a, memo))
  204.     
  205.     d = id(x)
  206.     
  207.     try:
  208.         return memo[d]
  209.     except KeyError:
  210.         pass
  211.  
  212.     for i in range(len(x)):
  213.         if x[i] is not y[i]:
  214.             y = tuple(y)
  215.             break
  216.         
  217.     else:
  218.         y = x
  219.     memo[d] = y
  220.     return y
  221.  
  222. d[types.TupleType] = _deepcopy_tuple
  223.  
  224. def _deepcopy_dict(x, memo):
  225.     y = { }
  226.     memo[id(x)] = y
  227.     for key in x.keys():
  228.         y[deepcopy(key, memo)] = deepcopy(x[key], memo)
  229.     
  230.     return y
  231.  
  232. d[types.DictionaryType] = _deepcopy_dict
  233. if PyStringMap is not None:
  234.     d[PyStringMap] = _deepcopy_dict
  235.  
  236.  
  237. def _keep_alive(x, memo):
  238.     
  239.     try:
  240.         memo[id(memo)].append(x)
  241.     except KeyError:
  242.         memo[id(memo)] = [
  243.             x]
  244.  
  245.  
  246.  
  247. def _deepcopy_inst(x, memo):
  248.     if hasattr(x, '__deepcopy__'):
  249.         return x.__deepcopy__(memo)
  250.     
  251.     if hasattr(x, '__getinitargs__'):
  252.         args = x.__getinitargs__()
  253.         args = deepcopy(args, memo)
  254.         y = apply(x.__class__, args)
  255.     else:
  256.         y = _EmptyClass()
  257.         y.__class__ = x.__class__
  258.     memo[id(x)] = y
  259.     if hasattr(x, '__getstate__'):
  260.         state = x.__getstate__()
  261.     else:
  262.         state = x.__dict__
  263.     state = deepcopy(state, memo)
  264.     if hasattr(y, '__setstate__'):
  265.         y.__setstate__(state)
  266.     else:
  267.         y.__dict__.update(state)
  268.     return y
  269.  
  270. d[types.InstanceType] = _deepcopy_inst
  271.  
  272. def _reconstruct(x, info, deep, memo = None):
  273.     if isinstance(info, str):
  274.         return x
  275.     
  276.     if memo is None:
  277.         memo = { }
  278.     
  279.     n = len(info)
  280.     (callable, args) = info[:2]
  281.     if n > 2:
  282.         state = info[2]
  283.     else:
  284.         state = { }
  285.     if deep:
  286.         args = deepcopy(args, memo)
  287.     
  288.     y = callable(*args)
  289.     if state:
  290.         if deep:
  291.             state = deepcopy(state, memo)
  292.         
  293.         if hasattr(y, '__setstate__'):
  294.             y.__setstate__(state)
  295.         else:
  296.             y.__dict__.update(state)
  297.     
  298.     return y
  299.  
  300. del d
  301. del types
  302.  
  303. class _EmptyClass:
  304.     pass
  305.  
  306.  
  307. def _test():
  308.     l = [
  309.         None,
  310.         1,
  311.         0x2L,
  312.         3.1400000000000001,
  313.         'xyzzy',
  314.         (1, 0x2L),
  315.         [
  316.             3.1400000000000001,
  317.             'abc'],
  318.         {
  319.             'abc': 'ABC' },
  320.         (),
  321.         [],
  322.         { }]
  323.     l1 = copy(l)
  324.     print l1 == l
  325.     l1 = map(copy, l)
  326.     print l1 == l
  327.     l1 = deepcopy(l)
  328.     print l1 == l
  329.     
  330.     class C:
  331.         
  332.         def __init__(self, arg = None):
  333.             self.a = 1
  334.             self.arg = arg
  335.             if __name__ == '__main__':
  336.                 import sys
  337.                 file = sys.argv[0]
  338.             else:
  339.                 file = __file__
  340.             self.fp = open(file)
  341.             self.fp.close()
  342.  
  343.         
  344.         def __getstate__(self):
  345.             return {
  346.                 'a': self.a,
  347.                 'arg': self.arg }
  348.  
  349.         
  350.         def __setstate__(self, state):
  351.             for key in state.keys():
  352.                 setattr(self, key, state[key])
  353.             
  354.  
  355.         
  356.         def __deepcopy__(self, memo = None):
  357.             new = self.__class__(deepcopy(self.arg, memo))
  358.             new.a = self.a
  359.             return new
  360.  
  361.  
  362.     c = C('argument sketch')
  363.     l.append(c)
  364.     l2 = copy(l)
  365.     print l == l2
  366.     print l
  367.     print l2
  368.     l2 = deepcopy(l)
  369.     print l == l2
  370.     print l
  371.     print l2
  372.     l.append({
  373.         l[1]: l,
  374.         'xyz': l[2] })
  375.     l3 = copy(l)
  376.     import repr
  377.     print map(repr.repr, l)
  378.     print map(repr.repr, l1)
  379.     print map(repr.repr, l2)
  380.     print map(repr.repr, l3)
  381.     l3 = deepcopy(l)
  382.     import repr
  383.     print map(repr.repr, l)
  384.     print map(repr.repr, l1)
  385.     print map(repr.repr, l2)
  386.     print map(repr.repr, l3)
  387.  
  388. if __name__ == '__main__':
  389.     _test()
  390.  
  391.