1503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbod"""Python 2/3 compat layer.""" 2503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbod 31ae29591efbb29492ce05378909ccf4028d7c1eeBehdad Esfahbodfrom __future__ import print_function, division, absolute_import 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: 2794968f31518249bff775201d3b3b47bac8ea6271Behdad Esfahbod from StringIO import StringIO 28503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbodexcept ImportError: 29cc13b7889da3997a1c6c802366be8b43df95e124Behdad Esfahbod from io import BytesIO as StringIO 30ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod 3118316aa769566eeb6f3f4a6ed2685fa8f8e861c2Behdad Esfahboddef strjoin(iterable): 3218316aa769566eeb6f3f4a6ed2685fa8f8e861c2Behdad Esfahbod return ''.join(iterable) 33ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbodif str == bytes: 34ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod class Tag(str): 35ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod def tobytes(self): 36ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod if isinstance(self, bytes): 37ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod return self 38ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod else: 39c076261598611984c44ba3346c8aa5bf140dfce9Behdad Esfahbod return self.encode('latin1') 405cf40083364e1d2dce119de25cb42ce69d2fb53cBehdad Esfahbod 4118316aa769566eeb6f3f4a6ed2685fa8f8e861c2Behdad Esfahbod def tostr(s, encoding='ascii'): 425cf40083364e1d2dce119de25cb42ce69d2fb53cBehdad Esfahbod if not isinstance(s, str): 4318316aa769566eeb6f3f4a6ed2685fa8f8e861c2Behdad Esfahbod return s.encode(encoding) 445cf40083364e1d2dce119de25cb42ce69d2fb53cBehdad Esfahbod else: 455cf40083364e1d2dce119de25cb42ce69d2fb53cBehdad Esfahbod return s 465cf40083364e1d2dce119de25cb42ce69d2fb53cBehdad Esfahbod tobytes = tostr 47821572c9a92d338a7ecbb4261c08ce378eb5434dBehdad Esfahbod 4818316aa769566eeb6f3f4a6ed2685fa8f8e861c2Behdad Esfahbod bytesjoin = strjoin 49ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbodelse: 50ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod class Tag(str): 51ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod 52ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod @staticmethod 53ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod def transcode(blob): 54ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod if not isinstance(blob, str): 55ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod blob = blob.decode('latin-1') 56ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod return blob 57ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod 58ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod def __new__(self, content): 59ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod return str.__new__(self, self.transcode(content)) 608ea6439d3b66c5acc246261d761d4375bcb7cfabBehdad Esfahbod def __ne__(self, other): 618ea6439d3b66c5acc246261d761d4375bcb7cfabBehdad Esfahbod return not self.__eq__(other) 62ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod def __eq__(self, other): 63ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod return str.__eq__(self, self.transcode(other)) 64ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod 65ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod def __hash__(self): 66ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod return str.__hash__(self) 67ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod 68ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod def tobytes(self): 69ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod return self.encode('latin-1') 705cf40083364e1d2dce119de25cb42ce69d2fb53cBehdad Esfahbod 7118316aa769566eeb6f3f4a6ed2685fa8f8e861c2Behdad Esfahbod def tostr(s, encoding='ascii'): 725cf40083364e1d2dce119de25cb42ce69d2fb53cBehdad Esfahbod if not isinstance(s, str): 7318316aa769566eeb6f3f4a6ed2685fa8f8e861c2Behdad Esfahbod return s.decode(encoding) 745cf40083364e1d2dce119de25cb42ce69d2fb53cBehdad Esfahbod else: 755cf40083364e1d2dce119de25cb42ce69d2fb53cBehdad Esfahbod return s 7618316aa769566eeb6f3f4a6ed2685fa8f8e861c2Behdad Esfahbod def tobytes(s, encoding='ascii'): 775cf40083364e1d2dce119de25cb42ce69d2fb53cBehdad Esfahbod if not isinstance(s, bytes): 7818316aa769566eeb6f3f4a6ed2685fa8f8e861c2Behdad Esfahbod return s.encode(encoding) 795cf40083364e1d2dce119de25cb42ce69d2fb53cBehdad Esfahbod else: 805cf40083364e1d2dce119de25cb42ce69d2fb53cBehdad Esfahbod return s 81821572c9a92d338a7ecbb4261c08ce378eb5434dBehdad Esfahbod 82821572c9a92d338a7ecbb4261c08ce378eb5434dBehdad Esfahbod def bytesjoin(iterable): 83db08ee2c19ba148b440e90ccef73c634b4577163Denis Jacquerye return b''.join(tobytes(item) for item in iterable) 84