diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2013-09-28 11:52:11 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2013-09-28 11:52:11 -0400 |
commit | a5af8925195b8ca6b37365f26210e98425cac864 (patch) | |
tree | 6cdc1e2f20f6e5b2050803de0f48e1d154915398 /coverage/execfile.py | |
parent | aafd82cc752bb16fe217656a2cae4e531cfc611f (diff) | |
download | python-coveragepy-git-a5af8925195b8ca6b37365f26210e98425cac864.tar.gz |
Thanks, Windows, for reminding me to close my files!
Diffstat (limited to 'coverage/execfile.py')
-rw-r--r-- | coverage/execfile.py | 32 |
1 files changed, 18 insertions, 14 deletions
diff --git a/coverage/execfile.py b/coverage/execfile.py index 2e892903..942d6953 100644 --- a/coverage/execfile.py +++ b/coverage/execfile.py @@ -150,18 +150,22 @@ def make_code_from_pyc(filename): except IOError: raise NoCode("No file to run: %r" % filename) - # First four bytes are a version-specific magic number. It has to match - # or we won't run the file. - magic = fpyc.read(4) - if magic != imp.get_magic(): - raise NoCode("Bad magic number in .pyc file") - - # Skip the junk in the header that we don't need. - fpyc.read(4) # Skip the moddate. - if sys.version_info >= (3, 3): - # 3.3 added another long to the header (size), skip it. - fpyc.read(4) - - # The rest of the file is the code object we want. - code = marshal.load(fpyc) + try: + # First four bytes are a version-specific magic number. It has to match + # or we won't run the file. + magic = fpyc.read(4) + if magic != imp.get_magic(): + raise NoCode("Bad magic number in .pyc file") + + # Skip the junk in the header that we don't need. + fpyc.read(4) # Skip the moddate. + if sys.version_info >= (3, 3): + # 3.3 added another long to the header (size), skip it. + fpyc.read(4) + + # The rest of the file is the code object we want. + code = marshal.load(fpyc) + finally: + fpyc.close() + return code |