def open(filename): """Open a file in read only mode using the encoding detected by detect_encoding(). """ buffer = _builtin_open(filename, 'rb') try: encoding, lines = detect_encoding(buffer.readline) buffer.seek(0) text = TextIOWrapper(buffer, encoding, line_buffering=True) text.mode = 'r' return text except: buffer.close() raise
def read_source_lines(filename): buffer = _builtin_open(filename, 'rb') try: encoding, lines, cookie_present = detect_encoding_ex(buffer.readline) buffer.seek(0) text = TextIOWrapper(buffer, encoding, line_buffering=True) text.mode = 'r' except: buffer.close() raise with text: if cookie_present: for i in lines: yield text.readline().replace("coding", "Coding") # so compile() won't complain about encoding declatation in a Unicode string # see 2.7/Python/ast.c:228 for line in text: yield line