annotate compat.py @ 59:93978361c67f

Added tag 2.00.0 with test group support for changeset c95addbe0851
author Oleg Oshmyan <chortos@inbox.lv>
date Tue, 21 Dec 2010 03:05:04 +0200
parents 4ea7133ac25c
children e0f8b28e15b5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
1 #! /usr/bin/env python
16
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
2 # Copyright (c) 2010 Chortos-2 <chortos@inbox.lv>
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
3
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
4 # 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
5 # 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
6
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
7 # A few notes regarding some compatibility-driven peculiarities
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
8 # 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
9 #
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
10 # * Except statements never specify target; instead, when needed,
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
11 # 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
12 # 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
13 # of preprocessor macros in Python of any version ;P.
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
14 #
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
15 # * Keyword-only parameters are never used, even for parameters
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
16 # 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
17 # 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
18 # 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
19 # of time and multiple version releases to sneak them in.
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
20 #
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
21 # * 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
22 # 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
23 # 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
24 # metaclasses are specified using a syntax totally incompatible
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
25 # 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
26 # 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
27
27
dc4be35d17e0 Bug fixes
Oleg Oshmyan <chortos@inbox.lv>
parents: 25
diff changeset
28 try:
dc4be35d17e0 Bug fixes
Oleg Oshmyan <chortos@inbox.lv>
parents: 25
diff changeset
29 import builtins
dc4be35d17e0 Bug fixes
Oleg Oshmyan <chortos@inbox.lv>
parents: 25
diff changeset
30 except ImportError:
dc4be35d17e0 Bug fixes
Oleg Oshmyan <chortos@inbox.lv>
parents: 25
diff changeset
31 import __builtin__ as builtins
25
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
32
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
33 __all__ = ('say', 'basestring', 'range', 'map', 'zip', 'filter', 'items',
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
34 'keys', 'values', 'zip_longest', 'callable',
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
35 'ABCMeta', 'abstractmethod', 'CompatBuiltins')
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
36
16
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
37 try:
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
38 # Python 3
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
39 exec('say = print')
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
40 except SyntaxError:
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
41 try:
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
42 # Python 2.6/2.7
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
43 # 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
44 # 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
45 # 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
46 say = getattr(builtins, 'print')
16
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
47 except Exception:
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
48 # Python 2.5
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
49 import sys
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
50 # 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
51 # The error messages are taken from Python 2.6
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
52 # 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
53 def saytypeerror(value, name):
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
54 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
55 def say(*values, **kwargs):
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
56 sep = kwargs.pop('sep' , None)
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
57 end = kwargs.pop('end' , None)
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
58 file = kwargs.pop('file', None)
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
59 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
60 if sep is None: sep = ' '
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
61 if end is None: end = '\n'
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
62 if file is None: file = sys.stdout
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
63 if not isinstance(sep, basestring): raise saytypeerror(sep, 'sep')
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
64 if not isinstance(end, basestring): raise saytypeerror(end, 'end')
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
65 file.write(sep.join(map(str, values)) + end)
16
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
66
34
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
67 try:
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
68 from os.path import relpath
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
69 except ImportError:
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
70 # Python 2.5
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
71 import os.path as _path
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
72
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
73 # 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
74
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
75 if hasattr(_path, 'splitunc'):
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
76 def _abspath_split(path):
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
77 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
78 prefix, rest = _path.splitunc(abs)
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
79 is_unc = bool(prefix)
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
80 if not is_unc:
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
81 prefix, rest = _path.splitdrive(abs)
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
82 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
83 else:
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
84 def _abspath_split(path):
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
85 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
86 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
87
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
88 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
89 """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
90
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
91 if not path:
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
92 raise ValueError("no path specified")
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
93
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
94 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
95 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
96
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
97 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
98 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
99 % (path, start))
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
100 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
101 if path_is_unc:
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
102 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
103 % (path_prefix, start_prefix))
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
104 else:
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
105 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
106 % (path_prefix, start_prefix))
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
107 # 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
108 i = 0
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
109 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
110 if e1.lower() != e2.lower():
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
111 break
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
112 i += 1
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
113
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
114 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
115 if not rel_list:
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
116 return _path.curdir
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
117 return _path.join(*rel_list)
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
118
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
119 _path.relpath = relpath
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
120
16
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
121 def import_urllib():
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
122 try:
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
123 # Python 3
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
124 import urllib.request
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
125 return urllib.request, lambda url: urllib.request.urlopen(url).read().decode()
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
126 except ImportError:
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
127 # Python 2
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
128 import urllib
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
129 return urllib, lambda url: urllib.urlopen(url).read()
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
130
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
131 try:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
132 from abc import ABCMeta, abstractmethod
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
133 except ImportError:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
134 ABCMeta, abstractmethod = None, lambda x: x
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
135
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
136 # In all of the following, the try clause is for Python 2 and the except
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
137 # clause is for Python 3. More checks are performed than needed
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
138 # for standard builds of Python to ensure as much as possible works
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
139 # on custom builds.
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
140 try:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
141 basestring = basestring
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
142 except NameError:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
143 basestring = str
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
144
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
145 try:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
146 range = xrange
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
147 except NameError:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
148 range = range
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
149
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
150 try:
25
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
151 callable = callable
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
152 except NameError:
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
153 callable = lambda obj: hasattr(obj, '__call__')
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
161 from itertools import imap as map
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
162 except ImportError:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
163 map = map
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
164
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
165 try:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
166 from itertools import izip as zip
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
167 except ImportError:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
168 zip = zip
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
169
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
170 try:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
171 from itertools import ifilter as filter
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 filter = filter
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 items = dict.iteritems if hasattr(dict, 'iteritems') else dict.items
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
176 keys = dict.iterkeys if hasattr(dict, 'iterkeys') else dict.keys
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
177 values = dict.itervalues if hasattr(dict, 'itervalues') else dict.values
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
178
25
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
179 try:
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
180 # Python 3
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
181 from itertools import zip_longest
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
182 except ImportError:
31
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
183 try:
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
184 # Python 2.6/2.7
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
185 from itertools import izip_longest as zip_longest
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
186 except ImportError:
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
187 # Python 2.5
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
188 from itertools import chain, repeat
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
189 # Adapted from the documentation of itertools.izip_longest
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
190 def zip_longest(*args, **kwargs):
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
191 fillvalue = kwargs.get('fillvalue')
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
192 def sentinel(counter=([fillvalue]*(len(args)-1)).pop):
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
193 yield counter()
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
194 fillers = repeat(fillvalue)
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
195 iters = [chain(it, sentinel(), fillers) for it in args]
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
196 try:
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
197 for tup in zip(*iters):
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
198 yield tup
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
199 except IndexError:
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
200 pass
25
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
201
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
202 # Automatically import * from this module into testconf.py's
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
203 class CompatBuiltins(object):
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
204 __slots__ = 'originals'
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
205 def __init__(self):
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
206 self.originals = {}
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
207 def __enter__(self):
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
208 g = globals()
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
209 for name in __all__:
27
dc4be35d17e0 Bug fixes
Oleg Oshmyan <chortos@inbox.lv>
parents: 25
diff changeset
210 if hasattr(builtins, name):
dc4be35d17e0 Bug fixes
Oleg Oshmyan <chortos@inbox.lv>
parents: 25
diff changeset
211 self.originals[name] = getattr(builtins, name)
dc4be35d17e0 Bug fixes
Oleg Oshmyan <chortos@inbox.lv>
parents: 25
diff changeset
212 setattr(builtins, name, g[name])
25
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
213 def __exit__(self, exc_type, exc_val, exc_tb):
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
214 for name in self.originals:
27
dc4be35d17e0 Bug fixes
Oleg Oshmyan <chortos@inbox.lv>
parents: 25
diff changeset
215 setattr(builtins, name, self.originals[name])
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
216
22
f07b7a431ea6 Further 2.00 work
Oleg Oshmyan <chortos@inbox.lv>
parents: 21
diff changeset
217 # Support simple testconf.py's written for test.py 1.x
27
dc4be35d17e0 Bug fixes
Oleg Oshmyan <chortos@inbox.lv>
parents: 25
diff changeset
218 builtins.xrange = range