Mercurial > ~astiob > upreckon > hgweb
annotate compat.py @ 114:c4dba6d44194 2.00 2.00.1
Fixed the version number reported in --version
author | Oleg Oshmyan <chortos@inbox.lv> |
---|---|
date | Fri, 08 Apr 2011 20:05:33 +0300 |
parents | cd347cfca272 |
children | e17ae4ccbc58 92f76baebcc6 |
rev | line source |
---|---|
78 | 1 # Copyright (c) 2010-2011 Chortos-2 <chortos@inbox.lv> |
16 | 2 |
21 | 3 # A compatibility layer for Python 2.5+. This is what lets test.py |
4 # run on all versions of Python starting with 2.5, including Python 3. | |
5 | |
6 # A few notes regarding some compatibility-driven peculiarities | |
7 # in the use of the language that can be seen in all modules: | |
8 # | |
9 # * Except statements never specify target; instead, when needed, | |
10 # the exception is taken from sys.exc_info(). Blame the incompatible | |
11 # syntaxes of the except clause in Python 2.5 and Python 3 and the lack | |
12 # of preprocessor macros in Python of any version ;P. | |
13 # | |
14 # * Keyword-only parameters are never used, even for parameters | |
15 # that should never be given in as arguments. The reason is | |
16 # the laziness of some Python developers who have failed to finish | |
17 # implementing them in Python 2 even though they had several years | |
18 # of time and multiple version releases to sneak them in. | |
19 # | |
20 # * Abstract classes are only implemented for Python 2.6 and 2.7. | |
21 # ABC's require the abc module and the specification of metaclasses, | |
22 # but in Python 2.5, the abc module does not exist, while in Python 3, | |
23 # metaclasses are specified using a syntax totally incompatible | |
24 # with Python 2 and not usable conditionally via exec() and such | |
25 # because it is a detail of the syntax of the class statement itself. | |
26 | |
67
e0f8b28e15b5
Added a copyright & licensing notice about code borrowed from Python itself
Oleg Oshmyan <chortos@inbox.lv>
parents:
50
diff
changeset
|
27 # Some code was adapted from Python 2.7.1 and its documentation. |
e0f8b28e15b5
Added a copyright & licensing notice about code borrowed from Python itself
Oleg Oshmyan <chortos@inbox.lv>
parents:
50
diff
changeset
|
28 # This code is clearly marked as such in preceding comments and is |
e0f8b28e15b5
Added a copyright & licensing notice about code borrowed from Python itself
Oleg Oshmyan <chortos@inbox.lv>
parents:
50
diff
changeset
|
29 # covered by copyright as follows: |
e0f8b28e15b5
Added a copyright & licensing notice about code borrowed from Python itself
Oleg Oshmyan <chortos@inbox.lv>
parents:
50
diff
changeset
|
30 # |
e0f8b28e15b5
Added a copyright & licensing notice about code borrowed from Python itself
Oleg Oshmyan <chortos@inbox.lv>
parents:
50
diff
changeset
|
31 # Copyright (c) 2001-2010 Python Software Foundation; all rights reserved. |
e0f8b28e15b5
Added a copyright & licensing notice about code borrowed from Python itself
Oleg Oshmyan <chortos@inbox.lv>
parents:
50
diff
changeset
|
32 # |
e0f8b28e15b5
Added a copyright & licensing notice about code borrowed from Python itself
Oleg Oshmyan <chortos@inbox.lv>
parents:
50
diff
changeset
|
33 # The code is used according to the PSF License Agreement |
e0f8b28e15b5
Added a copyright & licensing notice about code borrowed from Python itself
Oleg Oshmyan <chortos@inbox.lv>
parents:
50
diff
changeset
|
34 # for Python 2.7.1, whose full text is available from your local |
e0f8b28e15b5
Added a copyright & licensing notice about code borrowed from Python itself
Oleg Oshmyan <chortos@inbox.lv>
parents:
50
diff
changeset
|
35 # installation of Python (enter 'license()' in the interactive |
e0f8b28e15b5
Added a copyright & licensing notice about code borrowed from Python itself
Oleg Oshmyan <chortos@inbox.lv>
parents:
50
diff
changeset
|
36 # interpreter) or from the Web at the following URL: |
e0f8b28e15b5
Added a copyright & licensing notice about code borrowed from Python itself
Oleg Oshmyan <chortos@inbox.lv>
parents:
50
diff
changeset
|
37 # |
e0f8b28e15b5
Added a copyright & licensing notice about code borrowed from Python itself
Oleg Oshmyan <chortos@inbox.lv>
parents:
50
diff
changeset
|
38 # http://docs.python.org/2.7.1/license.html#terms-and-conditions-for-accessing-or-otherwise-using-python |
e0f8b28e15b5
Added a copyright & licensing notice about code borrowed from Python itself
Oleg Oshmyan <chortos@inbox.lv>
parents:
50
diff
changeset
|
39 |
27 | 40 try: |
41 import builtins | |
42 except ImportError: | |
43 import __builtin__ as builtins | |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
44 |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
45 pseudobuiltins = ('say', 'basestring', 'range', 'map', 'zip', 'filter', 'next', |
80
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
78
diff
changeset
|
46 'items', 'keys', 'values', 'zip_longest', 'callable', 'ceil') |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
47 __all__ = pseudobuiltins + ('ABCMeta', 'abstractmethod', 'CompatBuiltins') |
21 | 48 |
16 | 49 try: |
50 # Python 3 | |
51 exec('say = print') | |
52 except SyntaxError: | |
53 try: | |
54 # Python 2.6/2.7 | |
21 | 55 # An alternative is exec('from __future__ import print_function; say = print'); |
56 # if problems arise with the current line, one should try replacing it | |
57 # with this one with the future import before abandoning the idea altogether | |
27 | 58 say = getattr(builtins, 'print') |
16 | 59 except Exception: |
60 # Python 2.5 | |
61 import sys | |
62 # This should fully emulate the print function of Python 2.6 in Python 2.3+ | |
21 | 63 # The error messages are taken from Python 2.6 |
64 # The name bindings at the bottom of this file are in effect | |
16 | 65 def saytypeerror(value, name): |
21 | 66 return TypeError(' '.join((name, 'must be None, str or unicode, not', type(value).__name__))) |
16 | 67 def say(*values, **kwargs): |
68 sep = kwargs.pop('sep' , None) | |
69 end = kwargs.pop('end' , None) | |
70 file = kwargs.pop('file', None) | |
71 if kwargs: raise TypeError("'%s' is an invalid keyword argument for this function" % kwargs.popitem()[0]) | |
72 if sep is None: sep = ' ' | |
73 if end is None: end = '\n' | |
74 if file is None: file = sys.stdout | |
75 if not isinstance(sep, basestring): raise saytypeerror(sep, 'sep') | |
76 if not isinstance(end, basestring): raise saytypeerror(end, 'end') | |
21 | 77 file.write(sep.join(map(str, values)) + end) |
16 | 78 |
34
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
79 try: |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
80 from os.path import relpath |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
81 except ImportError: |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
82 # Python 2.5 |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
83 import os.path as _path |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
84 |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
85 # Adapted from Python 2.7.1 |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
86 |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
87 if hasattr(_path, 'splitunc'): |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
88 def _abspath_split(path): |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
89 abs = _path.abspath(_path.normpath(path)) |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
90 prefix, rest = _path.splitunc(abs) |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
91 is_unc = bool(prefix) |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
92 if not is_unc: |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
93 prefix, rest = _path.splitdrive(abs) |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
94 return is_unc, prefix, [x for x in rest.split(_path.sep) if x] |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
95 else: |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
96 def _abspath_split(path): |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
97 prefix, rest = _path.splitdrive(_path.abspath(_path.normpath(path))) |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
98 return False, prefix, [x for x in rest.split(_path.sep) if x] |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
99 |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
100 def relpath(path, start=_path.curdir): |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
101 """Return a relative version of a path""" |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
102 |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
103 if not path: |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
104 raise ValueError("no path specified") |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
105 |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
106 start_is_unc, start_prefix, start_list = _abspath_split(start) |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
107 path_is_unc, path_prefix, path_list = _abspath_split(path) |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
108 |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
109 if path_is_unc ^ start_is_unc: |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
110 raise ValueError("Cannot mix UNC and non-UNC paths (%s and %s)" |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
111 % (path, start)) |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
112 if path_prefix.lower() != start_prefix.lower(): |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
113 if path_is_unc: |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
114 raise ValueError("path is on UNC root %s, start on UNC root %s" |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
115 % (path_prefix, start_prefix)) |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
116 else: |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
117 raise ValueError("path is on drive %s, start on drive %s" |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
118 % (path_prefix, start_prefix)) |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
119 # Work out how much of the filepath is shared by start and path. |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
120 i = 0 |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
121 for e1, e2 in zip(start_list, path_list): |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
122 if e1.lower() != e2.lower(): |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
123 break |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
124 i += 1 |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
125 |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
126 rel_list = [_path.pardir] * (len(start_list)-i) + path_list[i:] |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
127 if not rel_list: |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
128 return _path.curdir |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
129 return _path.join(*rel_list) |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
130 |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
131 _path.relpath = relpath |
8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
31
diff
changeset
|
132 |
16 | 133 def import_urllib(): |
134 try: | |
135 # Python 3 | |
136 import urllib.request | |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
137 return urllib.request, lambda url: urllib.request.urlopen(url).read().decode('ascii') |
16 | 138 except ImportError: |
139 # Python 2 | |
140 import urllib | |
21 | 141 return urllib, lambda url: urllib.urlopen(url).read() |
142 | |
143 try: | |
144 from abc import ABCMeta, abstractmethod | |
145 except ImportError: | |
146 ABCMeta, abstractmethod = None, lambda x: x | |
147 | |
148 try: | |
149 basestring = basestring | |
150 except NameError: | |
151 basestring = str | |
152 | |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
153 # xrange is set to support simple testconf.py's written for test.py 1.x |
21 | 154 try: |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
155 xrange = range = xrange |
21 | 156 except NameError: |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
157 xrange = range = range |
21 | 158 |
159 try: | |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
160 callable = callable |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
161 except NameError: |
68
e00ab6d1f0ba
Corrected the implementation of callable() for Python 3.0 and 3.1
Oleg Oshmyan <chortos@inbox.lv>
parents:
67
diff
changeset
|
162 from collections import Callable |
e00ab6d1f0ba
Corrected the implementation of callable() for Python 3.0 and 3.1
Oleg Oshmyan <chortos@inbox.lv>
parents:
67
diff
changeset
|
163 callable = lambda obj: isinstance(obj, Callable) |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
164 |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
165 try: |
35
23aa8da5be5f
compat.py now emulates the next() built-in in Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
34
diff
changeset
|
166 next = next |
23aa8da5be5f
compat.py now emulates the next() built-in in Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
34
diff
changeset
|
167 except NameError: |
23aa8da5be5f
compat.py now emulates the next() built-in in Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
34
diff
changeset
|
168 next = lambda obj: obj.next() |
23aa8da5be5f
compat.py now emulates the next() built-in in Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
34
diff
changeset
|
169 |
23aa8da5be5f
compat.py now emulates the next() built-in in Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
34
diff
changeset
|
170 try: |
21 | 171 from itertools import imap as map |
172 except ImportError: | |
173 map = map | |
174 | |
175 try: | |
176 from itertools import izip as zip | |
177 except ImportError: | |
178 zip = zip | |
179 | |
180 try: | |
181 from itertools import ifilter as filter | |
182 except ImportError: | |
183 filter = filter | |
184 | |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
185 try: |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
186 items = dict.iteritems |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
187 except AttributeError: |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
188 items = dict.items |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
189 |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
190 try: |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
191 keys = dict.iterkeys |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
192 except AttributeError: |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
193 keys = dict.keys |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
194 |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
195 try: |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
196 values = dict.itervalues |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
197 except AttributeError: |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
198 values = dict.values |
21 | 199 |
80
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
78
diff
changeset
|
200 from math import ceil |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
78
diff
changeset
|
201 if not isinstance(ceil(0), int): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
78
diff
changeset
|
202 def ceil(x): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
78
diff
changeset
|
203 y = int(x) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
78
diff
changeset
|
204 if y < x: y += 1 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
78
diff
changeset
|
205 return y |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
78
diff
changeset
|
206 |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
207 try: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
208 # Python 3 |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
209 from itertools import zip_longest |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
210 except ImportError: |
31 | 211 try: |
212 # Python 2.6/2.7 | |
213 from itertools import izip_longest as zip_longest | |
214 except ImportError: | |
215 # Python 2.5 | |
216 from itertools import chain, repeat | |
217 # Adapted from the documentation of itertools.izip_longest | |
218 def zip_longest(*args, **kwargs): | |
219 fillvalue = kwargs.get('fillvalue') | |
220 def sentinel(counter=([fillvalue]*(len(args)-1)).pop): | |
221 yield counter() | |
222 fillers = repeat(fillvalue) | |
223 iters = [chain(it, sentinel(), fillers) for it in args] | |
224 try: | |
225 for tup in zip(*iters): | |
226 yield tup | |
227 except IndexError: | |
228 pass | |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
229 |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
230 # Automatically import * from this module into testconf.py's |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
231 class CompatBuiltins(object): |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
232 __slots__ = 'originals' |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
233 globals = globals() |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
234 def __enter__(self): |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
235 self.originals = {} |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
236 for name in pseudobuiltins: |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
237 try: |
27 | 238 self.originals[name] = getattr(builtins, name) |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
239 except AttributeError: |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
240 pass |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
241 setattr(builtins, name, self.globals[name]) |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
242 return self |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
243 def __exit__(self, exc_type, exc_val, exc_tb): |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
244 for name in self.originals: |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
245 setattr(builtins, name, self.originals[name]) |