home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_262 / copy.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-09-09  |  9.0 KB  |  420 lines

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