annotate compat.py @ 80:809b77302b21

Win32-specific module with memory and CPU time limits The Win32-specific implementation of call() and friends now lives in module win32, looks clean and in addition is able to enforce memory and CPU time limits on NT kernels, in particular on Windows 2000 and up asking the system to terminate the process as soon as or (in the case of CPU time) almost as soon as the limits are broken. According to my observations, malloc() in the limited process does not return NULL when memory usage is close to the limit and instead crashes the process (which Upreckon happily translates into 'memory limit exceeded'). The catch is that the module is not actually used yet; coming soon.
author Oleg Oshmyan <chortos@inbox.lv>
date Wed, 16 Feb 2011 00:01:33 +0000
parents d46bd7ee3e69
children cd347cfca272
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
78
d46bd7ee3e69 Updated copyright notices
Oleg Oshmyan <chortos@inbox.lv>
parents: 70
diff changeset
2 # Copyright (c) 2010-2011 Chortos-2 <chortos@inbox.lv>
16
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
67
e0f8b28e15b5 Added a copyright & licensing notice about code borrowed from Python itself
Oleg Oshmyan <chortos@inbox.lv>
parents: 50
diff changeset
28 # 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
29 # 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
30 # 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
31 #
e0f8b28e15b5 Added a copyright & licensing notice about code borrowed from Python itself
Oleg Oshmyan <chortos@inbox.lv>
parents: 50
diff changeset
32 # 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
33 #
e0f8b28e15b5 Added a copyright & licensing notice about code borrowed from Python itself
Oleg Oshmyan <chortos@inbox.lv>
parents: 50
diff changeset
34 # 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
35 # 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
36 # 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
37 # 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
38 #
e0f8b28e15b5 Added a copyright & licensing notice about code borrowed from Python itself
Oleg Oshmyan <chortos@inbox.lv>
parents: 50
diff changeset
39 # 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
40
27
dc4be35d17e0 Bug fixes
Oleg Oshmyan <chortos@inbox.lv>
parents: 25
diff changeset
41 try:
dc4be35d17e0 Bug fixes
Oleg Oshmyan <chortos@inbox.lv>
parents: 25
diff changeset
42 import builtins
dc4be35d17e0 Bug fixes
Oleg Oshmyan <chortos@inbox.lv>
parents: 25
diff changeset
43 except ImportError:
dc4be35d17e0 Bug fixes
Oleg Oshmyan <chortos@inbox.lv>
parents: 25
diff changeset
44 import __builtin__ as builtins
25
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
45
70
b9d5857f7b9a Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents: 68
diff changeset
46 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
47 '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
48 __all__ = pseudobuiltins + ('ABCMeta', 'abstractmethod', 'CompatBuiltins')
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
49
16
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
50 try:
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
51 # Python 3
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
52 exec('say = print')
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
53 except SyntaxError:
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
54 try:
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
55 # Python 2.6/2.7
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
56 # 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
57 # 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
58 # 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
59 say = getattr(builtins, 'print')
16
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
60 except Exception:
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
61 # Python 2.5
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
62 import sys
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
63 # 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
64 # The error messages are taken from Python 2.6
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
65 # 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
66 def saytypeerror(value, name):
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
67 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
68 def say(*values, **kwargs):
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
69 sep = kwargs.pop('sep' , None)
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
70 end = kwargs.pop('end' , None)
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
71 file = kwargs.pop('file', None)
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
72 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
73 if sep is None: sep = ' '
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
74 if end is None: end = '\n'
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
75 if file is None: file = sys.stdout
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
76 if not isinstance(sep, basestring): raise saytypeerror(sep, 'sep')
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
77 if not isinstance(end, basestring): raise saytypeerror(end, 'end')
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
78 file.write(sep.join(map(str, values)) + end)
16
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
79
34
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
80 try:
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
81 from os.path import relpath
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
82 except ImportError:
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
83 # Python 2.5
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
84 import os.path as _path
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
85
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
86 # 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
87
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
88 if hasattr(_path, 'splitunc'):
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
89 def _abspath_split(path):
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
90 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
91 prefix, rest = _path.splitunc(abs)
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
92 is_unc = bool(prefix)
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
93 if not is_unc:
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
94 prefix, rest = _path.splitdrive(abs)
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
95 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
96 else:
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
97 def _abspath_split(path):
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
98 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
99 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
100
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
101 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
102 """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
103
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
104 if not path:
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
105 raise ValueError("no path specified")
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
106
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
107 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
108 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
109
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
110 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
111 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
112 % (path, start))
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
113 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
114 if path_is_unc:
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
115 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
116 % (path_prefix, start_prefix))
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
117 else:
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
118 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
119 % (path_prefix, start_prefix))
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
120 # 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
121 i = 0
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
122 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
123 if e1.lower() != e2.lower():
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
124 break
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
125 i += 1
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
126
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
127 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
128 if not rel_list:
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
129 return _path.curdir
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
130 return _path.join(*rel_list)
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
131
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
132 _path.relpath = relpath
8fec38b0dd6e A os.path.relpath implementation for Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 31
diff changeset
133
16
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
134 def import_urllib():
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
135 try:
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
136 # Python 3
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
137 import urllib.request
70
b9d5857f7b9a Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents: 68
diff changeset
138 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
139 except ImportError:
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
140 # Python 2
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
141 import urllib
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
142 return urllib, lambda url: urllib.urlopen(url).read()
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
143
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
144 try:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
145 from abc import ABCMeta, abstractmethod
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
146 except ImportError:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
147 ABCMeta, abstractmethod = None, lambda x: x
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
148
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
149 try:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
150 basestring = basestring
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
151 except NameError:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
152 basestring = str
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
153
70
b9d5857f7b9a Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents: 68
diff changeset
154 # 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
155 try:
70
b9d5857f7b9a Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents: 68
diff changeset
156 xrange = range = xrange
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
157 except NameError:
70
b9d5857f7b9a Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents: 68
diff changeset
158 xrange = range = range
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
159
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
160 try:
25
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
161 callable = callable
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
162 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
163 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
164 callable = lambda obj: isinstance(obj, Callable)
25
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
165
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
166 try:
35
23aa8da5be5f compat.py now emulates the next() built-in in Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 34
diff changeset
167 next = next
23aa8da5be5f compat.py now emulates the next() built-in in Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 34
diff changeset
168 except NameError:
23aa8da5be5f compat.py now emulates the next() built-in in Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 34
diff changeset
169 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
170
23aa8da5be5f compat.py now emulates the next() built-in in Python 2.5
Oleg Oshmyan <chortos@inbox.lv>
parents: 34
diff changeset
171 try:
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
172 from itertools import imap as map
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
173 except ImportError:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
174 map = map
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
175
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
176 try:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
177 from itertools import izip as zip
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
178 except ImportError:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
179 zip = zip
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
180
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
181 try:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
182 from itertools import ifilter as filter
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
183 except ImportError:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
184 filter = filter
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
185
70
b9d5857f7b9a Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents: 68
diff changeset
186 try:
b9d5857f7b9a Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents: 68
diff changeset
187 items = dict.iteritems
b9d5857f7b9a Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents: 68
diff changeset
188 except AttributeError:
b9d5857f7b9a Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents: 68
diff changeset
189 items = dict.items
b9d5857f7b9a Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents: 68
diff changeset
190
b9d5857f7b9a Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents: 68
diff changeset
191 try:
b9d5857f7b9a Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents: 68
diff changeset
192 keys = dict.iterkeys
b9d5857f7b9a Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents: 68
diff changeset
193 except AttributeError:
b9d5857f7b9a Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents: 68
diff changeset
194 keys = dict.keys
b9d5857f7b9a Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents: 68
diff changeset
195
b9d5857f7b9a Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents: 68
diff changeset
196 try:
b9d5857f7b9a Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents: 68
diff changeset
197 values = dict.itervalues
b9d5857f7b9a Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents: 68
diff changeset
198 except AttributeError:
b9d5857f7b9a Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents: 68
diff changeset
199 values = dict.values
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
200
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents: 78
diff changeset
201 from math import ceil
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents: 78
diff changeset
202 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
203 def ceil(x):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents: 78
diff changeset
204 y = int(x)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents: 78
diff changeset
205 if y < x: y += 1
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents: 78
diff changeset
206 return y
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents: 78
diff changeset
207
25
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
208 try:
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
209 # Python 3
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
210 from itertools import zip_longest
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
211 except ImportError:
31
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
212 try:
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
213 # Python 2.6/2.7
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
214 from itertools import izip_longest as zip_longest
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
215 except ImportError:
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
216 # Python 2.5
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
217 from itertools import chain, repeat
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
218 # Adapted from the documentation of itertools.izip_longest
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
219 def zip_longest(*args, **kwargs):
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
220 fillvalue = kwargs.get('fillvalue')
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
221 def sentinel(counter=([fillvalue]*(len(args)-1)).pop):
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
222 yield counter()
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
223 fillers = repeat(fillvalue)
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
224 iters = [chain(it, sentinel(), fillers) for it in args]
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
225 try:
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
226 for tup in zip(*iters):
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
227 yield tup
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
228 except IndexError:
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 27
diff changeset
229 pass
25
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
230
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
231 # Automatically import * from this module into testconf.py's
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
232 class CompatBuiltins(object):
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
233 __slots__ = 'originals'
70
b9d5857f7b9a Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents: 68
diff changeset
234 globals = globals()
25
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
235 def __enter__(self):
70
b9d5857f7b9a Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents: 68
diff changeset
236 self.originals = {}
b9d5857f7b9a Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents: 68
diff changeset
237 for name in pseudobuiltins:
b9d5857f7b9a Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents: 68
diff changeset
238 try:
27
dc4be35d17e0 Bug fixes
Oleg Oshmyan <chortos@inbox.lv>
parents: 25
diff changeset
239 self.originals[name] = getattr(builtins, name)
70
b9d5857f7b9a Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents: 68
diff changeset
240 except AttributeError:
b9d5857f7b9a Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents: 68
diff changeset
241 pass
b9d5857f7b9a Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents: 68
diff changeset
242 setattr(builtins, name, self.globals[name])
b9d5857f7b9a Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents: 68
diff changeset
243 return self
25
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
244 def __exit__(self, exc_type, exc_val, exc_tb):
b500e117080e Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents: 22
diff changeset
245 for name in self.originals:
70
b9d5857f7b9a Better emulation of built-ins for testconf
Oleg Oshmyan <chortos@inbox.lv>
parents: 68
diff changeset
246 setattr(builtins, name, self.originals[name])