diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2010-11-09 01:08:59 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2010-11-09 01:08:59 (GMT) |
commit | 58c0752a33253641c1423fac2d4ef3f623fbcb46 (patch) | |
tree | 2e2ada02342f78d3cc58a4fe23082818c4025b1b /Lib/tokenize.py | |
parent | ae4836df6d0ea92d778ef30bd37417d048fc37fc (diff) | |
download | cpython-58c0752a33253641c1423fac2d4ef3f623fbcb46.zip cpython-58c0752a33253641c1423fac2d4ef3f623fbcb46.tar.gz cpython-58c0752a33253641c1423fac2d4ef3f623fbcb46.tar.bz2 |
Issue #10335: Add tokenize.open(), detect the file encoding using
tokenize.detect_encoding() and open it in read only mode.
Diffstat (limited to 'Lib/tokenize.py')
-rw-r--r-- | Lib/tokenize.py | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/Lib/tokenize.py b/Lib/tokenize.py index eb58831..7745412 100644 --- a/Lib/tokenize.py +++ b/Lib/tokenize.py @@ -29,6 +29,7 @@ import sys from token import * from codecs import lookup, BOM_UTF8 import collections +from io import TextIOWrapper cookie_re = re.compile("coding[:=]\s*([-\w.]+)") import token @@ -335,6 +336,20 @@ def detect_encoding(readline): return default, [first, second] +_builtin_open = open + +def open(filename): + """Open a file in read only mode using the encoding detected by + detect_encoding(). + """ + buffer = _builtin_open(filename, 'rb') + encoding, lines = detect_encoding(buffer.readline) + buffer.seek(0) + text = TextIOWrapper(buffer, encoding, line_buffering=True) + text.mode = 'r' + return text + + def tokenize(readline): """ The tokenize() generator requires one argment, readline, which |