Behdad Esfahbod | 503179f | 2013-11-27 14:36:57 -0500 | [diff] [blame] | 1 | """Python 2/3 compat layer.""" |
| 2 | |
Behdad Esfahbod | 32c10ee | 2013-11-27 17:46:17 -0500 | [diff] [blame] | 3 | from __future__ import print_function, division |
Behdad Esfahbod | 30e691e | 2013-11-27 17:27:45 -0500 | [diff] [blame] | 4 | |
Behdad Esfahbod | 503179f | 2013-11-27 14:36:57 -0500 | [diff] [blame] | 5 | try: |
| 6 | basestring |
| 7 | except NameError: |
| 8 | basestring = str |
| 9 | |
| 10 | try: |
| 11 | unicode |
| 12 | except NameError: |
| 13 | unicode = str |
| 14 | |
| 15 | try: |
| 16 | unichr |
| 17 | bytechr = chr |
Behdad Esfahbod | 319c5fd | 2013-11-27 18:13:48 -0500 | [diff] [blame] | 18 | byteord = ord |
Behdad Esfahbod | 503179f | 2013-11-27 14:36:57 -0500 | [diff] [blame] | 19 | except: |
| 20 | unichr = chr |
| 21 | def bytechr(n): |
| 22 | return bytes([n]) |
Behdad Esfahbod | 319c5fd | 2013-11-27 18:13:48 -0500 | [diff] [blame] | 23 | def byteord(c): |
Behdad Esfahbod | d368939 | 2013-11-27 21:13:05 -0500 | [diff] [blame] | 24 | return c if isinstance(c, int) else ord(c) |
Behdad Esfahbod | 503179f | 2013-11-27 14:36:57 -0500 | [diff] [blame] | 25 | |
| 26 | try: |
| 27 | from cStringIO import StringIO |
| 28 | except ImportError: |
Behdad Esfahbod | 3e8795d | 2013-11-27 21:10:27 -0500 | [diff] [blame] | 29 | try: |
| 30 | from StringIO import StringIO |
| 31 | except ImportError: |
| 32 | from io import BytesIO as StringIO |
Behdad Esfahbod | ac4672e | 2013-11-27 16:44:53 -0500 | [diff] [blame] | 33 | |
Behdad Esfahbod | 18316aa | 2013-11-27 21:17:35 -0500 | [diff] [blame] | 34 | def strjoin(iterable): |
| 35 | return ''.join(iterable) |
Behdad Esfahbod | ac4672e | 2013-11-27 16:44:53 -0500 | [diff] [blame] | 36 | if str == bytes: |
| 37 | class Tag(str): |
| 38 | def tobytes(self): |
| 39 | if isinstance(self, bytes): |
| 40 | return self |
| 41 | else: |
Behdad Esfahbod | c076261 | 2013-11-28 06:46:59 -0500 | [diff] [blame] | 42 | return self.encode('latin1') |
Behdad Esfahbod | 5cf4008 | 2013-11-27 19:51:59 -0500 | [diff] [blame] | 43 | |
Behdad Esfahbod | 18316aa | 2013-11-27 21:17:35 -0500 | [diff] [blame] | 44 | def tostr(s, encoding='ascii'): |
Behdad Esfahbod | 5cf4008 | 2013-11-27 19:51:59 -0500 | [diff] [blame] | 45 | if not isinstance(s, str): |
Behdad Esfahbod | 18316aa | 2013-11-27 21:17:35 -0500 | [diff] [blame] | 46 | return s.encode(encoding) |
Behdad Esfahbod | 5cf4008 | 2013-11-27 19:51:59 -0500 | [diff] [blame] | 47 | else: |
| 48 | return s |
| 49 | tobytes = tostr |
Behdad Esfahbod | 821572c | 2013-11-27 21:09:03 -0500 | [diff] [blame] | 50 | |
Behdad Esfahbod | 18316aa | 2013-11-27 21:17:35 -0500 | [diff] [blame] | 51 | bytesjoin = strjoin |
Behdad Esfahbod | ac4672e | 2013-11-27 16:44:53 -0500 | [diff] [blame] | 52 | else: |
| 53 | class Tag(str): |
| 54 | |
| 55 | @staticmethod |
| 56 | def transcode(blob): |
| 57 | if not isinstance(blob, str): |
| 58 | blob = blob.decode('latin-1') |
| 59 | return blob |
| 60 | |
| 61 | def __new__(self, content): |
| 62 | return str.__new__(self, self.transcode(content)) |
| 63 | def __eq__(self, other): |
| 64 | return str.__eq__(self, self.transcode(other)) |
| 65 | |
| 66 | def __hash__(self): |
| 67 | return str.__hash__(self) |
| 68 | |
| 69 | def tobytes(self): |
| 70 | return self.encode('latin-1') |
Behdad Esfahbod | 5cf4008 | 2013-11-27 19:51:59 -0500 | [diff] [blame] | 71 | |
Behdad Esfahbod | 18316aa | 2013-11-27 21:17:35 -0500 | [diff] [blame] | 72 | def tostr(s, encoding='ascii'): |
Behdad Esfahbod | 5cf4008 | 2013-11-27 19:51:59 -0500 | [diff] [blame] | 73 | if not isinstance(s, str): |
Behdad Esfahbod | 18316aa | 2013-11-27 21:17:35 -0500 | [diff] [blame] | 74 | return s.decode(encoding) |
Behdad Esfahbod | 5cf4008 | 2013-11-27 19:51:59 -0500 | [diff] [blame] | 75 | else: |
| 76 | return s |
Behdad Esfahbod | 18316aa | 2013-11-27 21:17:35 -0500 | [diff] [blame] | 77 | def tobytes(s, encoding='ascii'): |
Behdad Esfahbod | 5cf4008 | 2013-11-27 19:51:59 -0500 | [diff] [blame] | 78 | if not isinstance(s, bytes): |
Behdad Esfahbod | 18316aa | 2013-11-27 21:17:35 -0500 | [diff] [blame] | 79 | return s.encode(encoding) |
Behdad Esfahbod | 5cf4008 | 2013-11-27 19:51:59 -0500 | [diff] [blame] | 80 | else: |
| 81 | return s |
Behdad Esfahbod | 821572c | 2013-11-27 21:09:03 -0500 | [diff] [blame] | 82 | |
| 83 | def bytesjoin(iterable): |
Denis Jacquerye | db08ee2 | 2013-11-29 14:11:19 +0100 | [diff] [blame^] | 84 | return b''.join(tobytes(item) for item in iterable) |