bpo-41138: Fix trace CLI for non-UTF-8 files. (GH-21177)

Fix also a resource warning when store counts and module info.
(cherry picked from commit 04cdeb7a56)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Miss Islington (bot) 2020-06-28 03:52:26 -07:00 committed by GitHub
parent 1497bf66f9
commit d1e05e78a3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 16 deletions

View file

@ -287,8 +287,9 @@ class CoverageResults:
if self.outfile:
# try and store counts and module info into self.outfile
try:
pickle.dump((self.counts, self.calledfuncs, self.callers),
open(self.outfile, 'wb'), 1)
with open(self.outfile, 'wb') as f:
pickle.dump((self.counts, self.calledfuncs, self.callers),
f, 1)
except OSError as err:
print("Can't save counts files because %s" % err, file=sys.stderr)
@ -715,7 +716,7 @@ def main():
sys.argv = [opts.progname, *opts.arguments]
sys.path[0] = os.path.dirname(opts.progname)
with open(opts.progname) as fp:
with open(opts.progname, 'rb') as fp:
code = compile(fp.read(), opts.progname, 'exec')
# try to emulate __main__ namespace as much as possible
globs = {