py23.py revision 32c10eecffb4923e0721c395e4b80fb732543f18
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 18503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbodexcept: 19503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbod unichr = chr 20503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbod def bytechr(n): 21503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbod return bytes([n]) 22503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbod 23503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbodtry: 24503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbod from cStringIO import StringIO 25503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbodexcept ImportError: 26503179f2ed6416a719c1caea90a7882519bedfa9Behdad Esfahbod from io import StringIO 27ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod 28ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbodif str == bytes: 29ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod class Tag(str): 30ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod def tobytes(self): 31ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod if isinstance(self, bytes): 32ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod return self 33ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod else: 34ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod return self.encode('latin-1') 35ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbodelse: 36ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod class Tag(str): 37ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod 38ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod @staticmethod 39ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod def transcode(blob): 40ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod if not isinstance(blob, str): 41ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod blob = blob.decode('latin-1') 42ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod return blob 43ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod 44ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod def __new__(self, content): 45ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod return str.__new__(self, self.transcode(content)) 46ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod def __eq__(self, other): 47ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod return str.__eq__(self, self.transcode(other)) 48ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod 49ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod def __hash__(self): 50ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod return str.__hash__(self) 51ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod 52ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod def tobytes(self): 53ac4672e4510a79a56c4983ad28b42724c30ea9d1Behdad Esfahbod return self.encode('latin-1') 54