annotate rhodecode/lib/vcs/utils/compat.py @ 3797:d7488551578e beta

synced vcs with upstream - moved subprocessio module to VCS - many small changes to make embedded vcs as similar to to external lib - use only absolute imports - patch vcs config during load pylons env
author Marcin Kuzminski <marcin@python-works.com>
date Sat, 27 Apr 2013 11:24:25 +0200
parents 324ac367a4da
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2007
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1 """
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
2 Various utilities to work with Python < 2.7.
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
3
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
4 Those utilities may be deleted once ``vcs`` stops support for older Python
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
5 versions.
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
6 """
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
7 import sys
3797
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
8 import array
2007
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
9
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
10 if sys.version_info >= (2, 7):
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
11 unittest = __import__('unittest')
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
12 else:
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
13 unittest = __import__('unittest2')
3797
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
14
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
15
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
16 if sys.version_info >= (2, 6):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
17 _bytes = bytes
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
18 else:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
19 # in py2.6 bytes is a synonim for str
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
20 _bytes = str
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
21
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
22 if sys.version_info >= (2, 6):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
23 _bytearray = bytearray
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
24 else:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
25 # no idea if this is correct but all integration tests are passing
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
26 # i think we never use bytearray anyway
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
27 _bytearray = array
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
28
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
29 if sys.version_info >= (2, 6):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
30 from collections import deque
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
31 else:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
32 #need to implement our own deque with maxlen
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
33 class deque(object):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
34
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
35 def __init__(self, iterable=(), maxlen= -1):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
36 if not hasattr(self, 'data'):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
37 self.left = self.right = 0
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
38 self.data = {}
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
39 self.maxlen = maxlen or -1
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
40 self.extend(iterable)
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
41
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
42 def append(self, x):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
43 self.data[self.right] = x
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
44 self.right += 1
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
45 if self.maxlen != -1 and len(self) > self.maxlen:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
46 self.popleft()
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
47
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
48 def appendleft(self, x):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
49 self.left -= 1
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
50 self.data[self.left] = x
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
51 if self.maxlen != -1 and len(self) > self.maxlen:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
52 self.pop()
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
53
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
54 def pop(self):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
55 if self.left == self.right:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
56 raise IndexError('cannot pop from empty deque')
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
57 self.right -= 1
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
58 elem = self.data[self.right]
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
59 del self.data[self.right]
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
60 return elem
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
61
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
62 def popleft(self):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
63 if self.left == self.right:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
64 raise IndexError('cannot pop from empty deque')
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
65 elem = self.data[self.left]
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
66 del self.data[self.left]
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
67 self.left += 1
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
68 return elem
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
69
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
70 def clear(self):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
71 self.data.clear()
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
72 self.left = self.right = 0
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
73
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
74 def extend(self, iterable):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
75 for elem in iterable:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
76 self.append(elem)
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
77
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
78 def extendleft(self, iterable):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
79 for elem in iterable:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
80 self.appendleft(elem)
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
81
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
82 def rotate(self, n=1):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
83 if self:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
84 n %= len(self)
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
85 for i in xrange(n):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
86 self.appendleft(self.pop())
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
87
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
88 def __getitem__(self, i):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
89 if i < 0:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
90 i += len(self)
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
91 try:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
92 return self.data[i + self.left]
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
93 except KeyError:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
94 raise IndexError
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
95
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
96 def __setitem__(self, i, value):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
97 if i < 0:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
98 i += len(self)
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
99 try:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
100 self.data[i + self.left] = value
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
101 except KeyError:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
102 raise IndexError
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
103
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
104 def __delitem__(self, i):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
105 size = len(self)
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
106 if not (-size <= i < size):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
107 raise IndexError
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
108 data = self.data
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
109 if i < 0:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
110 i += size
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
111 for j in xrange(self.left + i, self.right - 1):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
112 data[j] = data[j + 1]
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
113 self.pop()
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
114
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
115 def __len__(self):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
116 return self.right - self.left
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
117
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
118 def __cmp__(self, other):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
119 if type(self) != type(other):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
120 return cmp(type(self), type(other))
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
121 return cmp(list(self), list(other))
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
122
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
123 def __repr__(self, _track=[]):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
124 if id(self) in _track:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
125 return '...'
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
126 _track.append(id(self))
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
127 r = 'deque(%r, maxlen=%s)' % (list(self), self.maxlen)
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
128 _track.remove(id(self))
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
129 return r
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
130
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
131 def __getstate__(self):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
132 return (tuple(self),)
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
133
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
134 def __setstate__(self, s):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
135 self.__init__(s[0])
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
136
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
137 def __hash__(self):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
138 raise TypeError
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
139
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
140 def __copy__(self):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
141 return self.__class__(self)
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
142
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
143 def __deepcopy__(self, memo={}):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
144 from copy import deepcopy
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
145 result = self.__class__()
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
146 memo[id(self)] = result
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
147 result.__init__(deepcopy(tuple(self), memo))
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
148 return result
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
149
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
150
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
151 #==============================================================================
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
152 # threading.Event
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
153 #==============================================================================
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
154
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
155 if sys.version_info >= (2, 6):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
156 from threading import Event, Thread
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
157 else:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
158 from threading import _Verbose, Lock, Thread, _time, \
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
159 _allocate_lock, RLock, _sleep
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
160
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
161 def Condition(*args, **kwargs):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
162 return _Condition(*args, **kwargs)
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
163
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
164 class _Condition(_Verbose):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
165
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
166 def __init__(self, lock=None, verbose=None):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
167 _Verbose.__init__(self, verbose)
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
168 if lock is None:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
169 lock = RLock()
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
170 self.__lock = lock
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
171 # Export the lock's acquire() and release() methods
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
172 self.acquire = lock.acquire
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
173 self.release = lock.release
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
174 # If the lock defines _release_save() and/or _acquire_restore(),
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
175 # these override the default implementations (which just call
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
176 # release() and acquire() on the lock). Ditto for _is_owned().
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
177 try:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
178 self._release_save = lock._release_save
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
179 except AttributeError:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
180 pass
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
181 try:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
182 self._acquire_restore = lock._acquire_restore
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
183 except AttributeError:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
184 pass
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
185 try:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
186 self._is_owned = lock._is_owned
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
187 except AttributeError:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
188 pass
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
189 self.__waiters = []
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
190
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
191 def __enter__(self):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
192 return self.__lock.__enter__()
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
193
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
194 def __exit__(self, *args):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
195 return self.__lock.__exit__(*args)
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
196
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
197 def __repr__(self):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
198 return "<Condition(%s, %d)>" % (self.__lock, len(self.__waiters))
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
199
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
200 def _release_save(self):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
201 self.__lock.release() # No state to save
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
202
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
203 def _acquire_restore(self, x):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
204 self.__lock.acquire() # Ignore saved state
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
205
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
206 def _is_owned(self):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
207 # Return True if lock is owned by current_thread.
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
208 # This method is called only if __lock doesn't have _is_owned().
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
209 if self.__lock.acquire(0):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
210 self.__lock.release()
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
211 return False
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
212 else:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
213 return True
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
214
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
215 def wait(self, timeout=None):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
216 if not self._is_owned():
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
217 raise RuntimeError("cannot wait on un-acquired lock")
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
218 waiter = _allocate_lock()
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
219 waiter.acquire()
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
220 self.__waiters.append(waiter)
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
221 saved_state = self._release_save()
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
222 try: # restore state no matter what (e.g., KeyboardInterrupt)
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
223 if timeout is None:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
224 waiter.acquire()
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
225 if __debug__:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
226 self._note("%s.wait(): got it", self)
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
227 else:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
228 # Balancing act: We can't afford a pure busy loop, so we
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
229 # have to sleep; but if we sleep the whole timeout time,
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
230 # we'll be unresponsive. The scheme here sleeps very
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
231 # little at first, longer as time goes on, but never longer
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
232 # than 20 times per second (or the timeout time remaining).
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
233 endtime = _time() + timeout
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
234 delay = 0.0005 # 500 us -> initial delay of 1 ms
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
235 while True:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
236 gotit = waiter.acquire(0)
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
237 if gotit:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
238 break
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
239 remaining = endtime - _time()
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
240 if remaining <= 0:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
241 break
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
242 delay = min(delay * 2, remaining, .05)
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
243 _sleep(delay)
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
244 if not gotit:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
245 if __debug__:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
246 self._note("%s.wait(%s): timed out", self, timeout)
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
247 try:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
248 self.__waiters.remove(waiter)
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
249 except ValueError:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
250 pass
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
251 else:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
252 if __debug__:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
253 self._note("%s.wait(%s): got it", self, timeout)
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
254 finally:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
255 self._acquire_restore(saved_state)
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
256
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
257 def notify(self, n=1):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
258 if not self._is_owned():
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
259 raise RuntimeError("cannot notify on un-acquired lock")
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
260 __waiters = self.__waiters
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
261 waiters = __waiters[:n]
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
262 if not waiters:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
263 if __debug__:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
264 self._note("%s.notify(): no waiters", self)
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
265 return
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
266 self._note("%s.notify(): notifying %d waiter%s", self, n,
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
267 n != 1 and "s" or "")
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
268 for waiter in waiters:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
269 waiter.release()
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
270 try:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
271 __waiters.remove(waiter)
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
272 except ValueError:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
273 pass
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
274
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
275 def notifyAll(self):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
276 self.notify(len(self.__waiters))
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
277
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
278 notify_all = notifyAll
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
279
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
280 def Event(*args, **kwargs):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
281 return _Event(*args, **kwargs)
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
282
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
283 class _Event(_Verbose):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
284
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
285 # After Tim Peters' event class (without is_posted())
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
286
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
287 def __init__(self, verbose=None):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
288 _Verbose.__init__(self, verbose)
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
289 self.__cond = Condition(Lock())
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
290 self.__flag = False
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
291
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
292 def isSet(self):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
293 return self.__flag
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
294
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
295 is_set = isSet
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
296
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
297 def set(self):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
298 self.__cond.acquire()
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
299 try:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
300 self.__flag = True
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
301 self.__cond.notify_all()
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
302 finally:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
303 self.__cond.release()
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
304
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
305 def clear(self):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
306 self.__cond.acquire()
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
307 try:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
308 self.__flag = False
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
309 finally:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
310 self.__cond.release()
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
311
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
312 def wait(self, timeout=None):
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
313 self.__cond.acquire()
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
314 try:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
315 if not self.__flag:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
316 self.__cond.wait(timeout)
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
317 finally:
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
318 self.__cond.release()