py23.py revision c076261598611984c44ba3346c8aa5bf140dfce9
1503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbod"""Python 2/3 compat layer.""" 2503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbod 332c10eecffb4923e0721c395e4b80fb732543f18Behdad Esfahbodfrom __future__ import print_function, division 430e691edd056ba22fa8970280e986747817bec3dBehdad Esfahbod 5503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbodtry: 6503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbod basestring 7503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbodexcept NameError: 8503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbod basestring = str 9503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbod 10503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbodtry: 11503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbod unicode 12503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbodexcept NameError: 13503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbod unicode = str 14503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbod 15503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbodtry: 16503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbod unichr 17503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbod bytechr = chr 18319c5fd10e2ea84304bd299b7483e05b5b0d5480Behdad Esfahbod byteord = ord 19503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbodexcept: 20503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbod unichr = chr 21503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbod def bytechr(n): 22503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbod return bytes([n]) 23319c5fd10e2ea84304bd299b7483e05b5b0d5480Behdad Esfahbod def byteord(c): 24d3689390a6ec0c6b0fb158284f29a87a6e45601bBehdad Esfahbod return c if isinstance(c, int) else ord(c) 25503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbod 26503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbodtry: 27503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbod from cStringIO import StringIO 28503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbodexcept ImportError: 293e8795d99ae7f03710233e5ff4e32b2f5cd07c86Behdad Esfahbod try: 303e8795d99ae7f03710233e5ff4e32b2f5cd07c86Behdad Esfahbod from StringIO import StringIO 313e8795d99ae7f03710233e5ff4e32b2f5cd07c86Behdad Esfahbod except ImportError: 323e8795d99ae7f03710233e5ff4e32b2f5cd07c86Behdad Esfahbod from io import BytesIO as StringIO 33ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod 3418316aa769566eeb6f3f4a6ed2685fa8f8e861c2Behdad Esfahboddef strjoin(iterable): 3518316aa769566eeb6f3f4a6ed2685fa8f8e861c2Behdad Esfahbod return ''.join(iterable) 36ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbodif str == bytes: 37ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod class Tag(str): 38ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod def tobytes(self): 39ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod if isinstance(self, bytes): 40ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod return self 41ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod else: 42c076261598611984c44ba3346c8aa5bf140dfce9Behdad Esfahbod return self.encode('latin1') 435cf40083364e1d2dce119de25cb42ce69d2fb53cBehdad Esfahbod 4418316aa769566eeb6f3f4a6ed2685fa8f8e861c2Behdad Esfahbod def tostr(s, encoding='ascii'): 455cf40083364e1d2dce119de25cb42ce69d2fb53cBehdad Esfahbod if not isinstance(s, str): 4618316aa769566eeb6f3f4a6ed2685fa8f8e861c2Behdad Esfahbod return s.encode(encoding) 475cf40083364e1d2dce119de25cb42ce69d2fb53cBehdad Esfahbod else: 485cf40083364e1d2dce119de25cb42ce69d2fb53cBehdad Esfahbod return s 495cf40083364e1d2dce119de25cb42ce69d2fb53cBehdad Esfahbod tobytes = tostr 50821572c9a92d338a7ecbb4261c08ce378eb5434dBehdad Esfahbod 5118316aa769566eeb6f3f4a6ed2685fa8f8e861c2Behdad Esfahbod bytesjoin = strjoin 52ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbodelse: 53ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod class Tag(str): 54ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod 55ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod @staticmethod 56ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod def transcode(blob): 57ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod if not isinstance(blob, str): 58ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod blob = blob.decode('latin-1') 59ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod return blob 60ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod 61ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod def __new__(self, content): 62ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod return str.__new__(self, self.transcode(content)) 63ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod def __eq__(self, other): 64ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod return str.__eq__(self, self.transcode(other)) 65ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod 66ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod def __hash__(self): 67ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod return str.__hash__(self) 68ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod 69ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod def tobytes(self): 70ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod return self.encode('latin-1') 715cf40083364e1d2dce119de25cb42ce69d2fb53cBehdad Esfahbod 7218316aa769566eeb6f3f4a6ed2685fa8f8e861c2Behdad Esfahbod def tostr(s, encoding='ascii'): 735cf40083364e1d2dce119de25cb42ce69d2fb53cBehdad Esfahbod if not isinstance(s, str): 7418316aa769566eeb6f3f4a6ed2685fa8f8e861c2Behdad Esfahbod return s.decode(encoding) 755cf40083364e1d2dce119de25cb42ce69d2fb53cBehdad Esfahbod else: 765cf40083364e1d2dce119de25cb42ce69d2fb53cBehdad Esfahbod return s 7718316aa769566eeb6f3f4a6ed2685fa8f8e861c2Behdad Esfahbod def tobytes(s, encoding='ascii'): 785cf40083364e1d2dce119de25cb42ce69d2fb53cBehdad Esfahbod if not isinstance(s, bytes): 7918316aa769566eeb6f3f4a6ed2685fa8f8e861c2Behdad Esfahbod return s.encode(encoding) 805cf40083364e1d2dce119de25cb42ce69d2fb53cBehdad Esfahbod else: 815cf40083364e1d2dce119de25cb42ce69d2fb53cBehdad Esfahbod return s 82821572c9a92d338a7ecbb4261c08ce378eb5434dBehdad Esfahbod 83821572c9a92d338a7ecbb4261c08ce378eb5434dBehdad Esfahbod def bytesjoin(iterable): 84821572c9a92d338a7ecbb4261c08ce378eb5434dBehdad Esfahbod return sum((tobytes(item) for item in iterable), b'') 85