diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2014-12-26 10:48:29 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2014-12-26 10:48:29 -0500 |
commit | ff0f1ae98dad66594cd34b5aa7c24e909554bad3 (patch) | |
tree | fdcac4f02baa46101c4ab0cfe6904823b3b600cc /coverage/phystokens.py | |
parent | 0dd9c8fb84dab988f1e34421bb91d53d45da807b (diff) | |
download | python-coveragepy-ff0f1ae98dad66594cd34b5aa7c24e909554bad3.tar.gz |
Unify and clarify reading Python source. Probably broke .pyw files
Diffstat (limited to 'coverage/phystokens.py')
-rw-r--r-- | coverage/phystokens.py | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/coverage/phystokens.py b/coverage/phystokens.py index 4faa3c3..70deb80 100644 --- a/coverage/phystokens.py +++ b/coverage/phystokens.py @@ -153,17 +153,17 @@ def source_encoding(source): Returns a string, the name of the encoding. """ - # Note: this function should never be called on Python 3, since py3 has - # built-in tools to do this. - assert sys.version_info < (3, 0) + if sys.version_info >= (3, 0): + readline = iter(source.splitlines(True)).__next__ + return tokenize.detect_encoding(readline)[0] + + # Do this so the detect_encode code we copied will work. + readline = iter(source.splitlines(True)).next # This is mostly code adapted from Py3.2's tokenize module. cookie_re = re.compile(r"^\s*#.*coding[:=]\s*([-\w.]+)") - # Do this so the detect_encode code we copied will work. - readline = iter(source.splitlines(True)).next - def _get_normal_name(orig_enc): """Imitates get_normal_name in tokenizer.c.""" # Only care about the first 12 characters. @@ -246,9 +246,12 @@ def source_encoding(source): # Reading Python source and interpreting the coding comment is a big deal. if sys.version_info >= (3, 0): # Python 3.2 provides `tokenize.open`, the best way to open source files. - import tokenize - open_python_source = tokenize.open + def read_python_source(filename): + # Returns unicode on Py3, bytes on Py2 + with tokenize.open(filename) as f: + return f.read() else: - def open_python_source(fname): - """Open a source file the best way.""" - return open(fname, "rU") + def read_python_source(filename): + # Returns unicode on Py3, bytes on Py2 + with open(filename, "rU") as f: + return f.read() |