Skip to content

Commit cb53b8c

Browse files
[3.8] bpo-41138: Fix trace CLI for non-UTF-8 files. (GH-21177) (GH-21200)
Fix also a resource warning when store counts and module info. (cherry picked from commit 04cdeb7)
1 parent 86ef6fe commit cb53b8c

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

Lib/test/test_trace.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
import sys
3-
from test.support import TESTFN, rmtree, unlink, captured_stdout
3+
from test.support import TESTFN, TESTFN_UNICODE, FS_NONASCII, rmtree, unlink, captured_stdout
44
from test.support.script_helper import assert_python_ok, assert_python_failure
55
import textwrap
66
import unittest
@@ -429,9 +429,10 @@ class TestCoverageCommandLineOutput(unittest.TestCase):
429429
coverfile = 'tmp.cover'
430430

431431
def setUp(self):
432-
with open(self.codefile, 'w') as f:
432+
with open(self.codefile, 'w', encoding='iso-8859-15') as f:
433433
f.write(textwrap.dedent('''\
434-
x = 42
434+
# coding: iso-8859-15
435+
x = 'spœm'
435436
if []:
436437
print('unreachable')
437438
'''))
@@ -452,9 +453,10 @@ def test_cover_files_written_no_highlight(self):
452453
self.assertEqual(stderr, b'')
453454
self.assertFalse(os.path.exists(tracecoverpath))
454455
self.assertTrue(os.path.exists(self.coverfile))
455-
with open(self.coverfile) as f:
456+
with open(self.coverfile, encoding='iso-8859-15') as f:
456457
self.assertEqual(f.read(),
457-
" 1: x = 42\n"
458+
" # coding: iso-8859-15\n"
459+
" 1: x = 'spœm'\n"
458460
" 1: if []:\n"
459461
" print('unreachable')\n"
460462
)
@@ -463,9 +465,10 @@ def test_cover_files_written_with_highlight(self):
463465
argv = '-m trace --count --missing'.split() + [self.codefile]
464466
status, stdout, stderr = assert_python_ok(*argv)
465467
self.assertTrue(os.path.exists(self.coverfile))
466-
with open(self.coverfile) as f:
468+
with open(self.coverfile, encoding='iso-8859-15') as f:
467469
self.assertEqual(f.read(), textwrap.dedent('''\
468-
1: x = 42
470+
# coding: iso-8859-15
471+
1: x = 'spœm'
469472
1: if []:
470473
>>>>>> print('unreachable')
471474
'''))
@@ -486,14 +489,19 @@ def test_failures(self):
486489
self.assertIn(message, stderr)
487490

488491
def test_listfuncs_flag_success(self):
489-
with open(TESTFN, 'w') as fd:
490-
self.addCleanup(unlink, TESTFN)
492+
filename = TESTFN + '.py'
493+
modulename = os.path.basename(TESTFN)
494+
with open(filename, 'w', encoding='utf-8') as fd:
495+
self.addCleanup(unlink, filename)
491496
fd.write("a = 1\n")
492-
status, stdout, stderr = assert_python_ok('-m', 'trace', '-l', TESTFN)
497+
status, stdout, stderr = assert_python_ok('-m', 'trace', '-l', filename,
498+
PYTHONIOENCODING='utf-8')
493499
self.assertIn(b'functions called:', stdout)
500+
expected = f'filename: {filename}, modulename: {modulename}, funcname: <module>'
501+
self.assertIn(expected.encode(), stdout)
494502

495503
def test_sys_argv_list(self):
496-
with open(TESTFN, 'w') as fd:
504+
with open(TESTFN, 'w', encoding='utf-8') as fd:
497505
self.addCleanup(unlink, TESTFN)
498506
fd.write("import sys\n")
499507
fd.write("print(type(sys.argv))\n")
@@ -505,7 +513,8 @@ def test_sys_argv_list(self):
505513
def test_count_and_summary(self):
506514
filename = f'{TESTFN}.py'
507515
coverfilename = f'{TESTFN}.cover'
508-
with open(filename, 'w') as fd:
516+
modulename = os.path.basename(TESTFN)
517+
with open(filename, 'w', encoding='utf-8') as fd:
509518
self.addCleanup(unlink, filename)
510519
self.addCleanup(unlink, coverfilename)
511520
fd.write(textwrap.dedent("""\
@@ -522,7 +531,7 @@ def f():
522531
stdout = stdout.decode()
523532
self.assertEqual(status, 0)
524533
self.assertIn('lines cov% module (path)', stdout)
525-
self.assertIn(f'6 100% {TESTFN} ({filename})', stdout)
534+
self.assertIn(f'6 100% {modulename} ({filename})', stdout)
526535

527536
def test_run_as_module(self):
528537
assert_python_ok('-m', 'trace', '-l', '--module', 'timeit', '-n', '1')

Lib/trace.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,9 @@ def write_results(self, show_missing=True, summary=False, coverdir=None):
287287
if self.outfile:
288288
# try and store counts and module info into self.outfile
289289
try:
290-
pickle.dump((self.counts, self.calledfuncs, self.callers),
291-
open(self.outfile, 'wb'), 1)
290+
with open(self.outfile, 'wb') as f:
291+
pickle.dump((self.counts, self.calledfuncs, self.callers),
292+
f, 1)
292293
except OSError as err:
293294
print("Can't save counts files because %s" % err, file=sys.stderr)
294295

@@ -731,7 +732,7 @@ def parse_ignore_dir(s):
731732
sys.argv = [opts.progname, *opts.arguments]
732733
sys.path[0] = os.path.dirname(opts.progname)
733734

734-
with open(opts.progname) as fp:
735+
with open(opts.progname, 'rb') as fp:
735736
code = compile(fp.read(), opts.progname, 'exec')
736737
# try to emulate __main__ namespace as much as possible
737738
globs = {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed the :mod:`trace` module CLI for Python source files with non-UTF-8
2+
encoding.

0 commit comments

Comments
 (0)