Back to index

python-biopython  1.60
_py3k.py
Go to the documentation of this file.
00001 # Copyright 2010 by Peter Cock.  All rights reserved.
00002 # This code is part of the Biopython distribution and governed by its
00003 # license.  Please see the LICENSE file that should have been included
00004 # as part of this package.
00005 """Python 3 compatibility tools (PRIVATE)."""
00006 
00007 import sys
00008 
00009 if sys.version_info[0] >= 3:
00010     #Python 3 code (which will be converted using 2to3 script)
00011     import codecs
00012 
00013     _bytes_to_string = lambda b: b.decode() # bytes to unicode string
00014     _string_to_bytes = lambda s: s.encode() # unicode string to bytes
00015 
00016     def _as_unicode(s):
00017         """Turn byte string or unicode string into a unicode string."""
00018         if isinstance(s, str):
00019             return s
00020         #Assume it is a bytes string
00021         #Note ISO-8859-1 aka Latin-1 preserves first 256 chars
00022         return codecs.latin_1_decode(s)[0]
00023 
00024 
00025     def _as_bytes(s):
00026         """Turn byte string or unicode string into a bytes string.
00027         
00028         The Python 2 version returns a (byte) string.
00029         """
00030         if isinstance(s, bytes):
00031             return s
00032         #Assume it is a unicode string
00033         #Note ISO-8859-1 aka Latin-1 preserves first 256 chars
00034         return codecs.latin_1_encode(s)[0]
00035 
00036     
00037     _as_string = _as_unicode
00038 
00039     def _is_int_or_long(i):
00040         """Check if the value is an integer.
00041 
00042         Note there are no longs on Python 3.
00043         """
00044         return isinstance(i, int)
00045 
00046     import io
00047     def _binary_to_string_handle(handle):
00048         """Treat a binary (bytes) handle like a text (unicode) handle."""
00049         #See also http://bugs.python.org/issue5628
00050         #and http://bugs.python.org/issue13541
00051         #and http://bugs.python.org/issue13464 which should be fixed in Python 3.3
00052         #return io.TextIOWrapper(io.BufferedReader(handle))
00053         #TODO - Re-evaluate this workaround under Python 3.3
00054         #(perhaps we will only need it on Python 3.1 and 3.2?)
00055         class EvilHandleHack(object):
00056             def __init__(self, handle):
00057                 self._handle = handle
00058             def read(self, length=None):
00059                 return _as_string(self._handle.read(length))
00060             def readline(self):
00061                 return _as_string(self._handle.readline())
00062             def __iter__(self):
00063                 for line in self._handle:
00064                     yield _as_string(line)
00065             def close(self):
00066                 return self._handle.close()
00067             def seek(self, pos):
00068                 return self._handle.seek(pos)
00069             def tell(self):
00070                 return self._handle.tell(pos)
00071         return EvilHandleHack(handle)
00072 
00073 else:
00074     #Python 2 code
00075 
00076     _bytes_to_string = lambda b: b # bytes to string, i.e. do nothing
00077     _string_to_bytes = lambda s: str(s) # str (or unicode) to bytes string
00078 
00079     def _as_unicode(s):
00080         """Turn a (byte) string or a unicode string into a (byte) string."""
00081         #Will be changed by 2to3 to "isinstance(s, str)" but doesn't matter:
00082         if isinstance(s, unicode):
00083             return s
00084         return s.decode()
00085     
00086     def _as_bytes(s):
00087         """Turn a (byte) string or a unicode string into a (byte) string."""
00088         return str(s)
00089     
00090     _as_string = _as_bytes
00091 
00092     def _is_int_or_long(i):
00093         """Check if the value is an integer or long."""
00094         #If the 2to3 long fixer is enabled (which it is by default), this
00095         #will be changed to "isinstance(i, int) or isinstance(i, int)"
00096         #but that doesn't matter.
00097         return isinstance(i, int) or isinstance(i, long)
00098 
00099     def _binary_to_string_handle(handle):
00100         """Treat a binary handle like a text handle."""
00101         return handle