# To fix or bypass Bug1701389 # ( http://sourceforge.net/tracker/index.php?func=detail&aid=1701389&group_id=5470&atid=105470 ) # @author Iceberg@21cn.com import __builtin__, codecs __all__ = ["open"] def open(filename, mode='rb', encoding=None, errors='strict', buffering=1): # @note utf32 is not yet tested #print 'before:', mode, encoding if encoding in ('utf16', 'utf_16', 'U16') and 'a' in mode: try: bom = __builtin__.open( filename, 'rb' ).read(2) if bom == '\xff\xfe': encoding = 'utf-16-le' elif bom == '\xfe\xff': encoding = 'utf-16-be' else: pass # no bom, no magic except IOError: pass # The file does not exist yet. No magic is needed. #print 'after:', mode, encoding return codecs.open( filename, mode, encoding, errors, buffering )