annotate rhodecode/lib/compat.py @ 3181:efe23d6c178c rhodecode-0.0.1.5.2

merged with beta
author Marcin Kuzminski <marcin@python-works.com>
date Mon, 21 Jan 2013 00:49:59 +0100
parents 4b7ad342e53b
children d7488551578e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
2 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
3 rhodecode.lib.compat
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
4 ~~~~~~~~~~~~~~~~~~~~
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
5
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
6 Python backward compatibility functions and common libs
1818
cf51bbfb120e auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents: 1549
diff changeset
7
cf51bbfb120e auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents: 1549
diff changeset
8
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
9 :created_on: Oct 7, 2011
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
10 :author: marcink
1824
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1818
diff changeset
11 :copyright: (C) 2010-2010 Marcin Kuzminski <marcin@python-works.com>
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
12 :license: GPLv3, see COPYING for more details.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
13 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
14 # This program is free software: you can redistribute it and/or modify
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
15 # it under the terms of the GNU General Public License as published by
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
16 # the Free Software Foundation, either version 3 of the License, or
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
17 # (at your option) any later version.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
18 #
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
19 # This program is distributed in the hope that it will be useful,
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
22 # GNU General Public License for more details.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
23 #
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
24 # You should have received a copy of the GNU General Public License
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
25 # along with this program. If not, see <http://www.gnu.org/licenses/>.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
26
1549
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
27 import os
2729
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
28 from rhodecode import __platform__, PLATFORM_WIN, __py_version__
1549
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
29
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
30 #==============================================================================
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
31 # json
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
32 #==============================================================================
2173
775a7672d363 add ext_json module
Marcin Kuzminski <marcin@python-works.com>
parents: 2160
diff changeset
33 from rhodecode.lib.ext_json import json
2826
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
34 import array
2151
12ceeda33339 #404 API extensions for showing permission for users
Marcin Kuzminski <marcin@python-works.com>
parents: 2068
diff changeset
35
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
36
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
37 #==============================================================================
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
38 # izip_longest
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
39 #==============================================================================
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
40 try:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
41 from itertools import izip_longest
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
42 except ImportError:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
43 import itertools
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
44
2151
12ceeda33339 #404 API extensions for showing permission for users
Marcin Kuzminski <marcin@python-works.com>
parents: 2068
diff changeset
45 def izip_longest(*args, **kwds):
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
46 fillvalue = kwds.get("fillvalue")
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
47
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
48 def sentinel(counter=([fillvalue] * (len(args) - 1)).pop):
2151
12ceeda33339 #404 API extensions for showing permission for users
Marcin Kuzminski <marcin@python-works.com>
parents: 2068
diff changeset
49 yield counter() # yields the fillvalue, or raises IndexError
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
50
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
51 fillers = itertools.repeat(fillvalue)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
52 iters = [itertools.chain(it, sentinel(), fillers)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
53 for it in args]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
54 try:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
55 for tup in itertools.izip(*iters):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
56 yield tup
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
57 except IndexError:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
58 pass
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
59
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
60
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
61 #==============================================================================
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
62 # OrderedDict
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
63 #==============================================================================
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
64
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
65 # Python Software Foundation License
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
66
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
67 # XXX: it feels like using the class with "is" and "is not" instead of "==" and
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
68 # "!=" should be faster.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
69 class _Nil(object):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
70
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
71 def __repr__(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
72 return "nil"
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
73
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
74 def __eq__(self, other):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
75 if (isinstance(other, _Nil)):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
76 return True
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
77 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
78 return NotImplemented
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
79
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
80 def __ne__(self, other):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
81 if (isinstance(other, _Nil)):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
82 return False
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
83 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
84 return NotImplemented
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
85
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
86 _nil = _Nil()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
87
1890
a3efaaa6ed4f fixes issue #271
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
88
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
89 class _odict(object):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
90 """Ordered dict data structure, with O(1) complexity for dict operations
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
91 that modify one element.
1818
cf51bbfb120e auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents: 1549
diff changeset
92
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
93 Overwriting values doesn't change their original sequential order.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
94 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
95
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
96 def _dict_impl(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
97 return None
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
98
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
99 def __init__(self, data=(), **kwds):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
100 """This doesn't accept keyword initialization as normal dicts to avoid
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
101 a trap - inside a function or method the keyword args are accessible
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
102 only as a dict, without a defined order, so their original order is
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
103 lost.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
104 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
105 if kwds:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
106 raise TypeError("__init__() of ordered dict takes no keyword "
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
107 "arguments to avoid an ordering trap.")
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
108 self._dict_impl().__init__(self)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
109 # If you give a normal dict, then the order of elements is undefined
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
110 if hasattr(data, "iteritems"):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
111 for key, val in data.iteritems():
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
112 self[key] = val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
113 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
114 for key, val in data:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
115 self[key] = val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
116
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
117 # Double-linked list header
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
118 def _get_lh(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
119 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
120 if not hasattr(self, '_lh'):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
121 dict_impl.__setattr__(self, '_lh', _nil)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
122 return dict_impl.__getattribute__(self, '_lh')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
123
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
124 def _set_lh(self, val):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
125 self._dict_impl().__setattr__(self, '_lh', val)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
126
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
127 lh = property(_get_lh, _set_lh)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
128
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
129 # Double-linked list tail
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
130 def _get_lt(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
131 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
132 if not hasattr(self, '_lt'):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
133 dict_impl.__setattr__(self, '_lt', _nil)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
134 return dict_impl.__getattribute__(self, '_lt')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
135
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
136 def _set_lt(self, val):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
137 self._dict_impl().__setattr__(self, '_lt', val)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
138
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
139 lt = property(_get_lt, _set_lt)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
140
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
141 def __getitem__(self, key):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
142 return self._dict_impl().__getitem__(self, key)[1]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
143
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
144 def __setitem__(self, key, val):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
145 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
146 try:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
147 dict_impl.__getitem__(self, key)[1] = val
1890
a3efaaa6ed4f fixes issue #271
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
148 except KeyError:
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
149 new = [dict_impl.__getattribute__(self, 'lt'), val, _nil]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
150 dict_impl.__setitem__(self, key, new)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
151 if dict_impl.__getattribute__(self, 'lt') == _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
152 dict_impl.__setattr__(self, 'lh', key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
153 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
154 dict_impl.__getitem__(
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
155 self, dict_impl.__getattribute__(self, 'lt'))[2] = key
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
156 dict_impl.__setattr__(self, 'lt', key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
157
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
158 def __delitem__(self, key):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
159 dict_impl = self._dict_impl()
1890
a3efaaa6ed4f fixes issue #271
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
160 pred, _, succ = self._dict_impl().__getitem__(self, key)
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
161 if pred == _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
162 dict_impl.__setattr__(self, 'lh', succ)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
163 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
164 dict_impl.__getitem__(self, pred)[2] = succ
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
165 if succ == _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
166 dict_impl.__setattr__(self, 'lt', pred)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
167 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
168 dict_impl.__getitem__(self, succ)[0] = pred
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
169 dict_impl.__delitem__(self, key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
170
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
171 def __contains__(self, key):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
172 return key in self.keys()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
173
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
174 def __len__(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
175 return len(self.keys())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
176
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
177 def __str__(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
178 pairs = ("%r: %r" % (k, v) for k, v in self.iteritems())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
179 return "{%s}" % ", ".join(pairs)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
180
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
181 def __repr__(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
182 if self:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
183 pairs = ("(%r, %r)" % (k, v) for k, v in self.iteritems())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
184 return "odict([%s])" % ", ".join(pairs)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
185 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
186 return "odict()"
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
187
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
188 def get(self, k, x=None):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
189 if k in self:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
190 return self._dict_impl().__getitem__(self, k)[1]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
191 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
192 return x
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
193
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
194 def __iter__(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
195 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
196 curr_key = dict_impl.__getattribute__(self, 'lh')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
197 while curr_key != _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
198 yield curr_key
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
199 curr_key = dict_impl.__getitem__(self, curr_key)[2]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
200
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
201 iterkeys = __iter__
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
202
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
203 def keys(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
204 return list(self.iterkeys())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
205
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
206 def itervalues(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
207 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
208 curr_key = dict_impl.__getattribute__(self, 'lh')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
209 while curr_key != _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
210 _, val, curr_key = dict_impl.__getitem__(self, curr_key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
211 yield val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
212
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
213 def values(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
214 return list(self.itervalues())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
215
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
216 def iteritems(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
217 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
218 curr_key = dict_impl.__getattribute__(self, 'lh')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
219 while curr_key != _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
220 _, val, next_key = dict_impl.__getitem__(self, curr_key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
221 yield curr_key, val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
222 curr_key = next_key
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
223
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
224 def items(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
225 return list(self.iteritems())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
226
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
227 def sort(self, cmp=None, key=None, reverse=False):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
228 items = [(k, v) for k, v in self.items()]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
229 if cmp is not None:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
230 items = sorted(items, cmp=cmp)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
231 elif key is not None:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
232 items = sorted(items, key=key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
233 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
234 items = sorted(items, key=lambda x: x[1])
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
235 if reverse:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
236 items.reverse()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
237 self.clear()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
238 self.__init__(items)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
239
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
240 def clear(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
241 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
242 dict_impl.clear(self)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
243 dict_impl.__setattr__(self, 'lh', _nil)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
244 dict_impl.__setattr__(self, 'lt', _nil)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
245
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
246 def copy(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
247 return self.__class__(self)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
248
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
249 def update(self, data=(), **kwds):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
250 if kwds:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
251 raise TypeError("update() of ordered dict takes no keyword "
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
252 "arguments to avoid an ordering trap.")
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
253 if hasattr(data, "iteritems"):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
254 data = data.iteritems()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
255 for key, val in data:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
256 self[key] = val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
257
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
258 def setdefault(self, k, x=None):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
259 try:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
260 return self[k]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
261 except KeyError:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
262 self[k] = x
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
263 return x
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
264
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
265 def pop(self, k, x=_nil):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
266 try:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
267 val = self[k]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
268 del self[k]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
269 return val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
270 except KeyError:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
271 if x == _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
272 raise
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
273 return x
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
274
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
275 def popitem(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
276 try:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
277 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
278 key = dict_impl.__getattribute__(self, 'lt')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
279 return key, self.pop(key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
280 except KeyError:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
281 raise KeyError("'popitem(): ordered dictionary is empty'")
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
282
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
283 def riterkeys(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
284 """To iterate on keys in reversed order.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
285 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
286 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
287 curr_key = dict_impl.__getattribute__(self, 'lt')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
288 while curr_key != _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
289 yield curr_key
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
290 curr_key = dict_impl.__getitem__(self, curr_key)[0]
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
291
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
292 __reversed__ = riterkeys
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
293
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
294 def rkeys(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
295 """List of the keys in reversed order.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
296 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
297 return list(self.riterkeys())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
298
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
299 def ritervalues(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
300 """To iterate on values in reversed order.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
301 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
302 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
303 curr_key = dict_impl.__getattribute__(self, 'lt')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
304 while curr_key != _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
305 curr_key, val, _ = dict_impl.__getitem__(self, curr_key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
306 yield val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
307
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
308 def rvalues(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
309 """List of the values in reversed order.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
310 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
311 return list(self.ritervalues())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
312
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
313 def riteritems(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
314 """To iterate on (key, value) in reversed order.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
315 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
316 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
317 curr_key = dict_impl.__getattribute__(self, 'lt')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
318 while curr_key != _nil:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
319 pred_key, val, _ = dict_impl.__getitem__(self, curr_key)
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
320 yield curr_key, val
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
321 curr_key = pred_key
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
322
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
323 def ritems(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
324 """List of the (key, value) in reversed order.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
325 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
326 return list(self.riteritems())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
327
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
328 def firstkey(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
329 if self:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
330 return self._dict_impl().__getattribute__(self, 'lh')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
331 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
332 raise KeyError("'firstkey(): ordered dictionary is empty'")
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
333
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
334 def lastkey(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
335 if self:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
336 return self._dict_impl().__getattribute__(self, 'lt')
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
337 else:
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
338 raise KeyError("'lastkey(): ordered dictionary is empty'")
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
339
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
340 def as_dict(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
341 return self._dict_impl()(self.items())
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
342
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
343 def _repr(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
344 """_repr(): low level repr of the whole data contained in the odict.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
345 Useful for debugging.
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
346 """
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
347 dict_impl = self._dict_impl()
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
348 form = "odict low level repr lh,lt,data: %r, %r, %s"
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
349 return form % (dict_impl.__getattribute__(self, 'lh'),
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
350 dict_impl.__getattribute__(self, 'lt'),
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
351 dict_impl.__repr__(self))
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
352
1890
a3efaaa6ed4f fixes issue #271
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
353
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
354 class OrderedDict(_odict, dict):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
355
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
356 def _dict_impl(self):
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
357 return dict
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
358
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
359
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
360 #==============================================================================
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
361 # OrderedSet
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
362 #==============================================================================
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
363 from sqlalchemy.util import OrderedSet
1549
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
364
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
365
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
366 #==============================================================================
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
367 # kill FUNCTIONS
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
368 #==============================================================================
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
369 if __platform__ in PLATFORM_WIN:
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
370 import ctypes
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
371
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
372 def kill(pid, sig):
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
373 """kill function for Win32"""
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
374 kernel32 = ctypes.windll.kernel32
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
375 handle = kernel32.OpenProcess(1, 0, pid)
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
376 return (0 != kernel32.TerminateProcess(handle, 0))
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
377
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
378 else:
d6cb805c92fd moved kill function to compat
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
379 kill = os.kill
2068
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
380
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
381
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
382 #==============================================================================
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
383 # itertools.product
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
384 #==============================================================================
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
385
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
386 try:
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
387 from itertools import product
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
388 except ImportError:
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
389 def product(*args, **kwds):
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
390 # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
391 # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
392 pools = map(tuple, args) * kwds.get('repeat', 1)
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
393 result = [[]]
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
394 for pool in pools:
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
395 result = [x + [y] for x in result for y in pool]
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
396 for prod in result:
f664d3b57fa4 p2.5 fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 1890
diff changeset
397 yield tuple(prod)
2552
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
398
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
399
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
400 #==============================================================================
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
401 # BytesIO
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
402 #==============================================================================
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
403
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
404 try:
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
405 from io import BytesIO
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
406 except ImportError:
e8650fbc4d4e Moved BytesIO into compat because of py25 compatibility issue
Marcin Kuzminski <marcin@python-works.com>
parents: 2173
diff changeset
407 from cStringIO import StringIO as BytesIO
2729
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
408
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
409
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
410 #==============================================================================
2790
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
411 # bytes
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
412 #==============================================================================
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
413 if __py_version__ >= (2, 6):
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
414 _bytes = bytes
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
415 else:
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
416 # in py2.6 bytes is a synonim for str
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
417 _bytes = str
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
418
2826
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
419 if __py_version__ >= (2, 6):
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
420 _bytearray = bytearray
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
421 else:
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
422 # no idea if this is correct but all integration tests are passing
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
423 # i think we never use bytearray anyway
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
424 _bytearray = array
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
425
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
426
2790
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
427 #==============================================================================
2729
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
428 # deque
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
429 #==============================================================================
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
430
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
431 if __py_version__ >= (2, 6):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
432 from collections import deque
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
433 else:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
434 #need to implement our own deque with maxlen
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
435 class deque(object):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
436
2790
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
437 def __init__(self, iterable=(), maxlen= -1):
2729
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
438 if not hasattr(self, 'data'):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
439 self.left = self.right = 0
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
440 self.data = {}
2790
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
441 self.maxlen = maxlen or -1
2729
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
442 self.extend(iterable)
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
443
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
444 def append(self, x):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
445 self.data[self.right] = x
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
446 self.right += 1
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
447 if self.maxlen != -1 and len(self) > self.maxlen:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
448 self.popleft()
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
449
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
450 def appendleft(self, x):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
451 self.left -= 1
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
452 self.data[self.left] = x
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
453 if self.maxlen != -1 and len(self) > self.maxlen:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
454 self.pop()
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
455
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
456 def pop(self):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
457 if self.left == self.right:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
458 raise IndexError('cannot pop from empty deque')
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
459 self.right -= 1
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
460 elem = self.data[self.right]
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
461 del self.data[self.right]
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
462 return elem
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
463
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
464 def popleft(self):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
465 if self.left == self.right:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
466 raise IndexError('cannot pop from empty deque')
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
467 elem = self.data[self.left]
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
468 del self.data[self.left]
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
469 self.left += 1
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
470 return elem
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
471
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
472 def clear(self):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
473 self.data.clear()
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
474 self.left = self.right = 0
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
475
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
476 def extend(self, iterable):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
477 for elem in iterable:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
478 self.append(elem)
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
479
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
480 def extendleft(self, iterable):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
481 for elem in iterable:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
482 self.appendleft(elem)
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
483
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
484 def rotate(self, n=1):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
485 if self:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
486 n %= len(self)
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
487 for i in xrange(n):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
488 self.appendleft(self.pop())
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
489
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
490 def __getitem__(self, i):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
491 if i < 0:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
492 i += len(self)
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
493 try:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
494 return self.data[i + self.left]
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
495 except KeyError:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
496 raise IndexError
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
497
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
498 def __setitem__(self, i, value):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
499 if i < 0:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
500 i += len(self)
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
501 try:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
502 self.data[i + self.left] = value
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
503 except KeyError:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
504 raise IndexError
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
505
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
506 def __delitem__(self, i):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
507 size = len(self)
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
508 if not (-size <= i < size):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
509 raise IndexError
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
510 data = self.data
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
511 if i < 0:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
512 i += size
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
513 for j in xrange(self.left + i, self.right - 1):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
514 data[j] = data[j + 1]
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
515 self.pop()
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
516
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
517 def __len__(self):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
518 return self.right - self.left
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
519
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
520 def __cmp__(self, other):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
521 if type(self) != type(other):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
522 return cmp(type(self), type(other))
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
523 return cmp(list(self), list(other))
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
524
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
525 def __repr__(self, _track=[]):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
526 if id(self) in _track:
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
527 return '...'
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
528 _track.append(id(self))
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
529 r = 'deque(%r, maxlen=%s)' % (list(self), self.maxlen)
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
530 _track.remove(id(self))
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
531 return r
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
532
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
533 def __getstate__(self):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
534 return (tuple(self),)
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
535
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
536 def __setstate__(self, s):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
537 self.__init__(s[0])
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
538
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
539 def __hash__(self):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
540 raise TypeError
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
541
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
542 def __copy__(self):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
543 return self.__class__(self)
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
544
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
545 def __deepcopy__(self, memo={}):
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
546 from copy import deepcopy
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
547 result = self.__class__()
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
548 memo[id(self)] = result
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
549 result.__init__(deepcopy(tuple(self), memo))
e9e7c40b4f1a added deque with maxlen for py2.5 compat
Marcin Kuzminski <marcin@python-works.com>
parents: 2552
diff changeset
550 return result
2730
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
551
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
552
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
553 #==============================================================================
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
554 # threading.Event
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
555 #==============================================================================
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
556
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
557 if __py_version__ >= (2, 6):
2790
3c0ae44557c4 more py2.5 compatibility patches
Marcin Kuzminski <marcin@python-works.com>
parents: 2730
diff changeset
558 from threading import Event, Thread
2730
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
559 else:
2826
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
560 from threading import _Verbose, Condition, Lock, Thread, _time, \
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
561 _allocate_lock, RLock, _sleep
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
562
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
563 def Condition(*args, **kwargs):
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
564 return _Condition(*args, **kwargs)
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
565
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
566 class _Condition(_Verbose):
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
567
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
568 def __init__(self, lock=None, verbose=None):
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
569 _Verbose.__init__(self, verbose)
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
570 if lock is None:
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
571 lock = RLock()
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
572 self.__lock = lock
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
573 # Export the lock's acquire() and release() methods
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
574 self.acquire = lock.acquire
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
575 self.release = lock.release
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
576 # If the lock defines _release_save() and/or _acquire_restore(),
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
577 # these override the default implementations (which just call
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
578 # release() and acquire() on the lock). Ditto for _is_owned().
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
579 try:
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
580 self._release_save = lock._release_save
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
581 except AttributeError:
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
582 pass
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
583 try:
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
584 self._acquire_restore = lock._acquire_restore
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
585 except AttributeError:
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
586 pass
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
587 try:
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
588 self._is_owned = lock._is_owned
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
589 except AttributeError:
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
590 pass
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
591 self.__waiters = []
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
592
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
593 def __enter__(self):
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
594 return self.__lock.__enter__()
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
595
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
596 def __exit__(self, *args):
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
597 return self.__lock.__exit__(*args)
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
598
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
599 def __repr__(self):
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
600 return "<Condition(%s, %d)>" % (self.__lock, len(self.__waiters))
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
601
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
602 def _release_save(self):
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
603 self.__lock.release() # No state to save
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
604
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
605 def _acquire_restore(self, x):
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
606 self.__lock.acquire() # Ignore saved state
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
607
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
608 def _is_owned(self):
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
609 # Return True if lock is owned by current_thread.
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
610 # This method is called only if __lock doesn't have _is_owned().
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
611 if self.__lock.acquire(0):
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
612 self.__lock.release()
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
613 return False
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
614 else:
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
615 return True
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
616
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
617 def wait(self, timeout=None):
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
618 if not self._is_owned():
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
619 raise RuntimeError("cannot wait on un-acquired lock")
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
620 waiter = _allocate_lock()
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
621 waiter.acquire()
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
622 self.__waiters.append(waiter)
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
623 saved_state = self._release_save()
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
624 try: # restore state no matter what (e.g., KeyboardInterrupt)
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
625 if timeout is None:
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
626 waiter.acquire()
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
627 if __debug__:
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
628 self._note("%s.wait(): got it", self)
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
629 else:
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
630 # Balancing act: We can't afford a pure busy loop, so we
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
631 # have to sleep; but if we sleep the whole timeout time,
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
632 # we'll be unresponsive. The scheme here sleeps very
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
633 # little at first, longer as time goes on, but never longer
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
634 # than 20 times per second (or the timeout time remaining).
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
635 endtime = _time() + timeout
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
636 delay = 0.0005 # 500 us -> initial delay of 1 ms
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
637 while True:
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
638 gotit = waiter.acquire(0)
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
639 if gotit:
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
640 break
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
641 remaining = endtime - _time()
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
642 if remaining <= 0:
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
643 break
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
644 delay = min(delay * 2, remaining, .05)
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
645 _sleep(delay)
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
646 if not gotit:
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
647 if __debug__:
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
648 self._note("%s.wait(%s): timed out", self, timeout)
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
649 try:
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
650 self.__waiters.remove(waiter)
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
651 except ValueError:
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
652 pass
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
653 else:
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
654 if __debug__:
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
655 self._note("%s.wait(%s): got it", self, timeout)
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
656 finally:
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
657 self._acquire_restore(saved_state)
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
658
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
659 def notify(self, n=1):
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
660 if not self._is_owned():
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
661 raise RuntimeError("cannot notify on un-acquired lock")
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
662 __waiters = self.__waiters
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
663 waiters = __waiters[:n]
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
664 if not waiters:
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
665 if __debug__:
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
666 self._note("%s.notify(): no waiters", self)
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
667 return
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
668 self._note("%s.notify(): notifying %d waiter%s", self, n,
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
669 n != 1 and "s" or "")
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
670 for waiter in waiters:
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
671 waiter.release()
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
672 try:
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
673 __waiters.remove(waiter)
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
674 except ValueError:
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
675 pass
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
676
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
677 def notifyAll(self):
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
678 self.notify(len(self.__waiters))
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
679
4b7ad342e53b python2.5 fixes !
Marcin Kuzminski <marcin@python-works.com>
parents: 2815
diff changeset
680 notify_all = notifyAll
2730
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
681
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
682 def Event(*args, **kwargs):
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
683 return _Event(*args, **kwargs)
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
684
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
685 class _Event(_Verbose):
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
686
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
687 # After Tim Peters' event class (without is_posted())
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
688
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
689 def __init__(self, verbose=None):
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
690 _Verbose.__init__(self, verbose)
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
691 self.__cond = Condition(Lock())
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
692 self.__flag = False
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
693
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
694 def isSet(self):
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
695 return self.__flag
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
696
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
697 is_set = isSet
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
698
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
699 def set(self):
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
700 self.__cond.acquire()
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
701 try:
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
702 self.__flag = True
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
703 self.__cond.notify_all()
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
704 finally:
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
705 self.__cond.release()
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
706
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
707 def clear(self):
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
708 self.__cond.acquire()
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
709 try:
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
710 self.__flag = False
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
711 finally:
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
712 self.__cond.release()
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
713
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
714 def wait(self, timeout=None):
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
715 self.__cond.acquire()
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
716 try:
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
717 if not self.__flag:
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
718 self.__cond.wait(timeout)
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
719 finally:
7949bc80b3b1 more py25 compat fixes
Marcin Kuzminski <marcin@python-works.com>
parents: 2729
diff changeset
720 self.__cond.release()