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 | 135467172407a7f40907cc752f282bf0e3cce306 (patch) | |
tree | 48cc1c881f8cda7c2a5180e19b61e257e7c90d74 /coverage/files.py | |
parent | a6e77b38f2341ae2ff4713119fdcad0bb3515e11 (diff) | |
download | python-coveragepy-git-135467172407a7f40907cc752f282bf0e3cce306.tar.gz |
Unify and clarify reading Python source. Probably broke .pyw files
Diffstat (limited to 'coverage/files.py')
-rw-r--r-- | coverage/files.py | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/coverage/files.py b/coverage/files.py index 6ba7983b..29450542 100644 --- a/coverage/files.py +++ b/coverage/files.py @@ -8,8 +8,8 @@ import sys import ntpath import posixpath -from coverage.misc import CoverageException, join_regex -from coverage.phystokens import open_python_source +from coverage.misc import CoverageException, NoSource, join_regex +from coverage.phystokens import read_python_source, source_encoding class FileLocator(object): @@ -56,19 +56,20 @@ class FileLocator(object): def get_python_source(filename): - """Return the source code, as a string.""" + """Return the source code, as a str.""" if os.path.exists(filename): # A regular text file: open it. - with open_python_source(filename) as f: - return f.read() + return read_python_source(filename) # Maybe it's in a zip file? source = get_zip_bytes(filename) if source is not None: + if sys.version_info >= (3, 0): + source = source.decode(source_encoding(source)) return source # Couldn't find source. - raise CoverageException( + raise NoSource( "No source for code: '%s'." % filename ) |