annotate compat.py @ 139:2d465a8a679c

Removed the redundant .hgtags file from the default branch Let each branch store its own tags.
author Oleg Oshmyan <chortos@inbox.lv>
date Tue, 24 May 2011 22:05:08 +0100
parents cd347cfca272
children e17ae4ccbc58 92f76baebcc6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
78
d46bd7ee3e69 Updated copyright notices
Oleg Oshmyan <chortos@inbox.lv>
parents: 70
diff changeset
1 # Copyright (c) 2010-2011 Chortos-2 <chortos@inbox.lv>
16
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
2
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
3 # A compatibility layer for Python 2.5+. This is what lets test.py
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
4 # run on all versions of Python starting with 2.5, including Python 3.
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
5
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
6 # A few notes regarding some compatibility-driven peculiarities
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
7 # in the use of the language that can be seen in all modules:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
8 #
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
9 # * Except statements never specify target; instead, when needed,
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
10 # the exception is taken from sys.exc_info(). Blame the incompatible
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
11 # syntaxes of the except clause in Python 2.5 and Python 3 and the lack
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
12 # of preprocessor macros in Python of any version ;P.
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
13 #
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
14 # * Keyword-only parameters are never used, even for parameters
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
15 # that should never be given in as arguments. The reason is
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
16 # the laziness of some Python developers who have failed to finish
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
17 # implementing them in Python 2 even though they had several years
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
18 # of time and multiple version releases to sneak them in.
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
19 #
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
20 # * Abstract classes are only implemented for Python 2.6 and 2.7.
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
21 # ABC's require the abc module and the specification of metaclasses,
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
22 # but in Python 2.5, the abc module does not exist, while in Python 3,
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
23 # metaclasses are specified using a syntax totally incompatible
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
24 # with Python 2 and not usable conditionally via exec() and such
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
25 # because it is a detail of the syntax of the class statement itself.
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
dc4be35d17e0 Bug fixes
Oleg Oshmyan <chortos@inbox.lv>
parents: 25
diff changeset
40 try:
dc4be35d17e0 Bug fixes
Oleg Oshmyan <chortos@inbox.lv>
parents: 25
diff changeset
41 import builtins
dc4be35d17e0 Bug fixes
Oleg Oshmyan <chortos@inbox.lv>
parents: 25
diff changeset
42 except ImportError:
dc4be35d17e0 Bug fixes
Oleg Oshmyan <chortos@inbox.lv>
parents: 25
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
48
16
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
49 try:
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
50 # Python 3
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
51 exec('say = print')
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
52 except SyntaxError:
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
53 try:
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
54 # Python 2.6/2.7
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
55 # An alternative is exec('from __future__ import print_function; say = print');
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
56 # if problems arise with the current line, one should try replacing it
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
57 # with this one with the future import before abandoning the idea altogether
27
dc4be35d17e0 Bug fixes
Oleg Oshmyan <chortos@inbox.lv>
parents: 25
diff changeset
58 say = getattr(builtins, 'print')
16
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
59 except Exception:
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
60 # Python 2.5
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
61 import sys
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
62 # This should fully emulate the print function of Python 2.6 in Python 2.3+
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
63 # The error messages are taken from Python 2.6
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
64 # The name bindings at the bottom of this file are in effect
16
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
65 def saytypeerror(value, name):
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
66 return TypeError(' '.join((name, 'must be None, str or unicode, not', type(value).__name__)))
16
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
67 def say(*values, **kwargs):
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
68 sep = kwargs.pop('sep' , None)
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
69 end = kwargs.pop('end' , None)
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
70 file = kwargs.pop('file', None)
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
71 if kwargs: raise TypeError("'%s' is an invalid keyword argument for this function" % kwargs.popitem()[0])
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
72 if sep is None: sep = ' '
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
73 if end is None: end = '\n'
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
74 if file is None: file = sys.stdout
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
75 if not isinstance(sep, basestring): raise saytypeerror(sep, 'sep')
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
76 if not isinstance(end, basestring): raise saytypeerror(end, 'end')
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
77 file.write(sep.join(map(str, values)) + end)
16
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
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
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
133 def import_urllib():
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
134 try:
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
135 # Python 3
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
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
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
138 except ImportError:
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
139 # Python 2
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
140 import urllib
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
141 return urllib, lambda url: urllib.urlopen(url).read()
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
142
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
143 try:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
144 from abc import ABCMeta, abstractmethod
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
145 except ImportError:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
146 ABCMeta, abstractmethod = None, lambda x: x
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
147
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
148 try:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
149 basestring = basestring
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
150 except NameError:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
151 basestring = str
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
158
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
171 from itertools import imap as map
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
172 except ImportError:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
173 map = map
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
174
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
175 try:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
176 from itertools import izip as zip
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
177 except ImportError:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
178 zip = zip
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
179
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
180 try:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
181 from itertools import ifilter as filter
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
182 except ImportError:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
183 filter = filter
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
211 try:
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
212 # Python 2.6/2.7
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
213 from itertools import izip_longest as zip_longest
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
214 except ImportError:
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
215 # Python 2.5
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
216 from itertools import chain, repeat
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
217 # Adapted from the documentation of itertools.izip_longest
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
218 def zip_longest(*args, **kwargs):
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
219 fillvalue = kwargs.get('fillvalue')
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
220 def sentinel(counter=([fillvalue]*(len(args)-1)).pop):
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
221 yield counter()
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
222 fillers = repeat(fillvalue)
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
223 iters = [chain(it, sentinel(), fillers) for it in args]
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
224 try:
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
225 for tup in zip(*iters):
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
226 yield tup
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
227 except IndexError:
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
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
dc4be35d17e0 Bug fixes
Oleg Oshmyan <chortos@inbox.lv>
parents: 25
diff changeset
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])