Mercurial > ~astiob > upreckon > hgweb
comparison 2.00/compat.py @ 27:dc4be35d17e0
Bug fixes
Bug fix: compat.py now works in Python 3 (module __builtin__ was renamed builtins).
Bug fix: random spaces in the indentation of testcases.py have been removed.
Bug fix: false dummy{in,out}name's are now again replaced with the values of testcase{in,out}name.
| author | Oleg Oshmyan <chortos@inbox.lv> |
|---|---|
| date | Fri, 15 Oct 2010 22:17:31 +0000 |
| parents | b500e117080e |
| children | fe1463e7e24d |
comparison
equal
deleted
inserted
replaced
| 26:5bbb68833868 | 27:dc4be35d17e0 |
|---|---|
| 23 # but in Python 2.5, the abc module does not exist, while in Python 3, | 23 # but in Python 2.5, the abc module does not exist, while in Python 3, |
| 24 # metaclasses are specified using a syntax totally incompatible | 24 # metaclasses are specified using a syntax totally incompatible |
| 25 # with Python 2 and not usable conditionally via exec() and such | 25 # with Python 2 and not usable conditionally via exec() and such |
| 26 # because it is a detail of the syntax of the class statement itself. | 26 # because it is a detail of the syntax of the class statement itself. |
| 27 | 27 |
| 28 import __builtin__ | 28 try: |
| 29 import builtins | |
| 30 except ImportError: | |
| 31 import __builtin__ as builtins | |
| 29 | 32 |
| 30 __all__ = ('say', 'basestring', 'range', 'map', 'zip', 'filter', 'items', | 33 __all__ = ('say', 'basestring', 'range', 'map', 'zip', 'filter', 'items', |
| 31 'keys', 'values', 'zip_longest', 'callable', | 34 'keys', 'values', 'zip_longest', 'callable', |
| 32 'ABCMeta', 'abstractmethod', 'CompatBuiltins') | 35 'ABCMeta', 'abstractmethod', 'CompatBuiltins') |
| 33 | 36 |
| 38 try: | 41 try: |
| 39 # Python 2.6/2.7 | 42 # Python 2.6/2.7 |
| 40 # An alternative is exec('from __future__ import print_function; say = print'); | 43 # An alternative is exec('from __future__ import print_function; say = print'); |
| 41 # if problems arise with the current line, one should try replacing it | 44 # if problems arise with the current line, one should try replacing it |
| 42 # with this one with the future import before abandoning the idea altogether | 45 # with this one with the future import before abandoning the idea altogether |
| 43 say = __builtins__['print'] | 46 say = getattr(builtins, 'print') |
| 44 except Exception: | 47 except Exception: |
| 45 # Python 2.5 | 48 # Python 2.5 |
| 46 import sys | 49 import sys |
| 47 # This should fully emulate the print function of Python 2.6 in Python 2.3+ | 50 # This should fully emulate the print function of Python 2.6 in Python 2.3+ |
| 48 # The error messages are taken from Python 2.6 | 51 # The error messages are taken from Python 2.6 |
| 142 def __init__(self): | 145 def __init__(self): |
| 143 self.originals = {} | 146 self.originals = {} |
| 144 def __enter__(self): | 147 def __enter__(self): |
| 145 g = globals() | 148 g = globals() |
| 146 for name in __all__: | 149 for name in __all__: |
| 147 if hasattr(__builtin__, name): | 150 if hasattr(builtins, name): |
| 148 self.originals[name] = getattr(__builtin__, name) | 151 self.originals[name] = getattr(builtins, name) |
| 149 setattr(__builtin__, name, g[name]) | 152 setattr(builtins, name, g[name]) |
| 150 def __exit__(self, exc_type, exc_val, exc_tb): | 153 def __exit__(self, exc_type, exc_val, exc_tb): |
| 151 for name in self.originals: | 154 for name in self.originals: |
| 152 setattr(__builtin__, name, self.originals[name]) | 155 setattr(builtins, name, self.originals[name]) |
| 153 | 156 |
| 154 # Support simple testconf.py's written for test.py 1.x | 157 # Support simple testconf.py's written for test.py 1.x |
| 155 __builtin__.xrange = range | 158 builtins.xrange = range |
