| OLD | NEW |
| 1 """Add things to old Pythons so I can pretend they are newer.""" | 1 """Add things to old Pythons so I can pretend they are newer.""" |
| 2 | 2 |
| 3 # This file does lots of tricky stuff, so disable a bunch of lintisms. | 3 # This file does lots of tricky stuff, so disable a bunch of lintisms. |
| 4 # pylint: disable=F0401,W0611,W0622 | 4 # pylint: disable=F0401,W0611,W0622 |
| 5 # F0401: Unable to import blah | 5 # F0401: Unable to import blah |
| 6 # W0611: Unused import blah | 6 # W0611: Unused import blah |
| 7 # W0622: Redefining built-in blah | 7 # W0622: Redefining built-in blah |
| 8 | 8 |
| 9 import os, re, sys | 9 import os, re, sys |
| 10 | 10 |
| 11 # Python 2.3 doesn't have `set` | 11 # Python 2.3 doesn't have `set` |
| 12 try: | 12 try: |
| 13 set = set # new in 2.4 | 13 set = set # new in 2.4 |
| 14 except NameError: | 14 except NameError: |
| 15 from sets import Set as set | 15 from sets import Set as set |
| 16 | 16 |
| 17 # Python 2.3 doesn't have `sorted`. | 17 # Python 2.3 doesn't have `sorted`. |
| 18 try: | 18 try: |
| 19 sorted = sorted | 19 sorted = sorted |
| 20 except NameError: | 20 except NameError: |
| 21 def sorted(iterable): | 21 def sorted(iterable): |
| 22 """A 2.3-compatible implementation of `sorted`.""" | 22 """A 2.3-compatible implementation of `sorted`.""" |
| 23 lst = list(iterable) | 23 lst = list(iterable) |
| 24 lst.sort() | 24 lst.sort() |
| 25 return lst | 25 return lst |
| 26 | 26 |
| 27 # Python 2.3 doesn't have `reversed`. |
| 28 try: |
| 29 reversed = reversed |
| 30 except NameError: |
| 31 def reversed(iterable): |
| 32 """A 2.3-compatible implementation of `reversed`.""" |
| 33 lst = list(iterable) |
| 34 return lst[::-1] |
| 35 |
| 27 # rpartition is new in 2.5 | 36 # rpartition is new in 2.5 |
| 28 try: | 37 try: |
| 29 "".rpartition | 38 "".rpartition |
| 30 except AttributeError: | 39 except AttributeError: |
| 31 def rpartition(s, sep): | 40 def rpartition(s, sep): |
| 32 """Implement s.rpartition(sep) for old Pythons.""" | 41 """Implement s.rpartition(sep) for old Pythons.""" |
| 33 i = s.rfind(sep) | 42 i = s.rfind(sep) |
| 34 if i == -1: | 43 if i == -1: |
| 35 return ('', '', s) | 44 return ('', '', s) |
| 36 else: | 45 else: |
| (...skipping 22 matching lines...) Expand all Loading... |
| 59 except ImportError: | 68 except ImportError: |
| 60 import pickle | 69 import pickle |
| 61 | 70 |
| 62 # range or xrange? | 71 # range or xrange? |
| 63 try: | 72 try: |
| 64 range = xrange | 73 range = xrange |
| 65 except NameError: | 74 except NameError: |
| 66 range = range | 75 range = range |
| 67 | 76 |
| 68 # A function to iterate listlessly over a dict's items. | 77 # A function to iterate listlessly over a dict's items. |
| 69 if "iteritems" in dir({}): | 78 try: |
| 79 {}.iteritems |
| 80 except AttributeError: |
| 81 def iitems(d): |
| 82 """Produce the items from dict `d`.""" |
| 83 return d.items() |
| 84 else: |
| 70 def iitems(d): | 85 def iitems(d): |
| 71 """Produce the items from dict `d`.""" | 86 """Produce the items from dict `d`.""" |
| 72 return d.iteritems() | 87 return d.iteritems() |
| 73 else: | |
| 74 def iitems(d): | |
| 75 """Produce the items from dict `d`.""" | |
| 76 return d.items() | |
| 77 | 88 |
| 78 # Exec is a statement in Py2, a function in Py3 | 89 # Exec is a statement in Py2, a function in Py3 |
| 79 if sys.version_info >= (3, 0): | 90 if sys.version_info >= (3, 0): |
| 80 def exec_code_object(code, global_map): | 91 def exec_code_object(code, global_map): |
| 81 """A wrapper around exec().""" | 92 """A wrapper around exec().""" |
| 82 exec(code, global_map) | 93 exec(code, global_map) |
| 83 else: | 94 else: |
| 84 # OK, this is pretty gross. In Py2, exec was a statement, but that will | 95 # OK, this is pretty gross. In Py2, exec was a statement, but that will |
| 85 # be a syntax error if we try to put it in a Py3 file, even if it is never | 96 # be a syntax error if we try to put it in a Py3 file, even if it is never |
| 86 # executed. So hide it inside an evaluated string literal instead. | 97 # executed. So hide it inside an evaluated string literal instead. |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 # get them right, and make them no-ops in 2.x | 133 # get them right, and make them no-ops in 2.x |
| 123 if sys.version_info >= (3, 0): | 134 if sys.version_info >= (3, 0): |
| 124 def to_bytes(s): | 135 def to_bytes(s): |
| 125 """Convert string `s` to bytes.""" | 136 """Convert string `s` to bytes.""" |
| 126 return s.encode('utf8') | 137 return s.encode('utf8') |
| 127 | 138 |
| 128 def to_string(b): | 139 def to_string(b): |
| 129 """Convert bytes `b` to a string.""" | 140 """Convert bytes `b` to a string.""" |
| 130 return b.decode('utf8') | 141 return b.decode('utf8') |
| 131 | 142 |
| 143 def binary_bytes(byte_values): |
| 144 """Produce a byte string with the ints from `byte_values`.""" |
| 145 return bytes(byte_values) |
| 146 |
| 147 def byte_to_int(byte_value): |
| 148 """Turn an element of a bytes object into an int.""" |
| 149 return byte_value |
| 150 |
| 151 def bytes_to_ints(bytes_value): |
| 152 """Turn a bytes object into a sequence of ints.""" |
| 153 # In Py3, iterating bytes gives ints. |
| 154 return bytes_value |
| 155 |
| 132 else: | 156 else: |
| 133 def to_bytes(s): | 157 def to_bytes(s): |
| 134 """Convert string `s` to bytes (no-op in 2.x).""" | 158 """Convert string `s` to bytes (no-op in 2.x).""" |
| 135 return s | 159 return s |
| 136 | 160 |
| 137 def to_string(b): | 161 def to_string(b): |
| 138 """Convert bytes `b` to a string (no-op in 2.x).""" | 162 """Convert bytes `b` to a string (no-op in 2.x).""" |
| 139 return b | 163 return b |
| 140 | 164 |
| 165 def binary_bytes(byte_values): |
| 166 """Produce a byte string with the ints from `byte_values`.""" |
| 167 return "".join([chr(b) for b in byte_values]) |
| 168 |
| 169 def byte_to_int(byte_value): |
| 170 """Turn an element of a bytes object into an int.""" |
| 171 return ord(byte_value) |
| 172 |
| 173 def bytes_to_ints(bytes_value): |
| 174 """Turn a bytes object into a sequence of ints.""" |
| 175 for byte in bytes_value: |
| 176 yield ord(byte) |
| 177 |
| 141 # Md5 is available in different places. | 178 # Md5 is available in different places. |
| 142 try: | 179 try: |
| 143 import hashlib | 180 import hashlib |
| 144 md5 = hashlib.md5 | 181 md5 = hashlib.md5 |
| 145 except ImportError: | 182 except ImportError: |
| 146 import md5 | 183 import md5 |
| 147 md5 = md5.new | 184 md5 = md5.new |
| OLD | NEW |