Mercurial > ~astiob > upreckon > hgweb
annotate compat.py @ 236:359c79a54fd4 2.00
Closed branch 2.00
author | Oleg Oshmyan <chortos@inbox.lv> |
---|---|
date | Tue, 11 Dec 2012 01:01:42 +0200 |
parents | e17ae4ccbc58 |
children |
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 |
21 | 133 try: |
134 from abc import ABCMeta, abstractmethod | |
135 except ImportError: | |
136 ABCMeta, abstractmethod = None, lambda x: x | |
137 | |
138 try: | |
139 basestring = basestring | |
140 except NameError: | |
141 basestring = str | |
142 | |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
143 # xrange is set to support simple testconf.py's written for test.py 1.x |
21 | 144 try: |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
145 xrange = range = xrange |
21 | 146 except NameError: |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
147 xrange = range = range |
21 | 148 |
149 try: | |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
150 callable = callable |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
151 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
|
152 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
|
153 callable = lambda obj: isinstance(obj, Callable) |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
154 |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
155 try: |
35
23aa8da5be5f
compat.py now emulates the next() built-in in Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
34
diff
changeset
|
156 next = next |
23aa8da5be5f
compat.py now emulates the next() built-in in Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
34
diff
changeset
|
157 except NameError: |
23aa8da5be5f
compat.py now emulates the next() built-in in Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
34
diff
changeset
|
158 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
|
159 |
23aa8da5be5f
compat.py now emulates the next() built-in in Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents:
34
diff
changeset
|
160 try: |
21 | 161 from itertools import imap as map |
162 except ImportError: | |
163 map = map | |
164 | |
165 try: | |
166 from itertools import izip as zip | |
167 except ImportError: | |
168 zip = zip | |
169 | |
170 try: | |
171 from itertools import ifilter as filter | |
172 except ImportError: | |
173 filter = filter | |
174 | |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
175 try: |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
176 items = dict.iteritems |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
177 except AttributeError: |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
178 items = dict.items |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
179 |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
180 try: |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
181 keys = dict.iterkeys |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
182 except AttributeError: |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
183 keys = dict.keys |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
184 |
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 values = dict.itervalues |
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 values = dict.values |
21 | 189 |
80
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
78
diff
changeset
|
190 from math import ceil |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
78
diff
changeset
|
191 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
|
192 def ceil(x): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
78
diff
changeset
|
193 y = int(x) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
78
diff
changeset
|
194 if y < x: y += 1 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
78
diff
changeset
|
195 return y |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
78
diff
changeset
|
196 |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
197 try: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
198 # Python 3 |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
199 from itertools import zip_longest |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
200 except ImportError: |
31 | 201 try: |
202 # Python 2.6/2.7 | |
203 from itertools import izip_longest as zip_longest | |
204 except ImportError: | |
205 # Python 2.5 | |
206 from itertools import chain, repeat | |
207 # Adapted from the documentation of itertools.izip_longest | |
208 def zip_longest(*args, **kwargs): | |
209 fillvalue = kwargs.get('fillvalue') | |
210 def sentinel(counter=([fillvalue]*(len(args)-1)).pop): | |
211 yield counter() | |
212 fillers = repeat(fillvalue) | |
213 iters = [chain(it, sentinel(), fillers) for it in args] | |
214 try: | |
215 for tup in zip(*iters): | |
216 yield tup | |
217 except IndexError: | |
218 pass | |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
219 |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
220 # Automatically import * from this module into testconf.py's |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
221 class CompatBuiltins(object): |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
222 __slots__ = 'originals' |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
223 globals = globals() |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
224 def __enter__(self): |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
225 self.originals = {} |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
226 for name in pseudobuiltins: |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
227 try: |
27 | 228 self.originals[name] = getattr(builtins, name) |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
229 except AttributeError: |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
230 pass |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
231 setattr(builtins, name, self.globals[name]) |
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
232 return self |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
233 def __exit__(self, exc_type, exc_val, exc_tb): |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
22
diff
changeset
|
234 for name in self.originals: |
70
b9d5857f7b9a
Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents:
68
diff
changeset
|
235 setattr(builtins, name, self.originals[name]) |