Skip to content

Commit 04cdeb7

Browse files
bpo-41138: Fix trace CLI for non-UTF-8 files. (GH-21177)
Fix also a resource warning when store counts and module info.
1 parent cd3c2bd commit 04cdeb7

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

Lib/test/test_trace.py

Lines changed: 21 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
@@ -428,9 +428,10 @@ class TestCoverageCommandLineOutput(unittest.TestCase):
428428
coverfile = 'tmp.cover'
429429

430430
def setUp(self):
431-
with open(self.codefile, 'w') as f:
431+
with open(self.codefile, 'w', encoding='iso-8859-15') as f:
432432
f.write(textwrap.dedent('''\
433-
x = 42
433+
# coding: iso-8859-15
434+
x = 'spœm'
434435
if []:
435436
print('unreachable')
436437
'''))
@@ -451,9 +452,10 @@ def test_cover_files_written_no_highlight(self):
451452
self.assertEqual(stderr, b'')
452453
self.assertFalse(os.path.exists(tracecoverpath))
453454
self.assertTrue(os.path.exists(self.coverfile))
454-
with open(self.coverfile) as f:
455+
with open(self.coverfile, encoding='iso-8859-15') as f:
455456
self.assertEqual(f.read(),
456-
" 1: x = 42\n"
457+
" # coding: iso-8859-15\n"
458+
" 1: x = 'spœm'\n"
457459
" 1: if []:\n"
458460
" print('unreachable')\n"
459461
)
@@ -462,9 +464,10 @@ def test_cover_files_written_with_highlight(self):
462464
argv = '-m trace --count --missing'.split() + [self.codefile]
463465
status, stdout, stderr = assert_python_ok(*argv)
464466
self.assertTrue(os.path.exists(self.coverfile))
465-
with open(self.coverfile) as f:
467+
with open(self.coverfile, encoding='iso-8859-15') as f:
466468
self.assertEqual(f.read(), textwrap.dedent('''\
467-
1: x = 42
469+
# coding: iso-8859-15
470+
1: x = 'spœm'
468471
1: if []:
469472
>>>>>> print('unreachable')
470473
'''))
@@ -485,15 +488,19 @@ def test_failures(self):
485488
self.assertIn(message, stderr)
486489

487490
def test_listfuncs_flag_success(self):
488-
with open(TESTFN, 'w') as fd:
489-
self.addCleanup(unlink, TESTFN)
491+
filename = TESTFN + '.py'
492+
modulename = os.path.basename(TESTFN)
493+
with open(filename, 'w', encoding='utf-8') as fd:
494+
self.addCleanup(unlink, filename)
490495
fd.write("a = 1\n")
491-
status, stdout, stderr = assert_python_ok('-m', 'trace', '-l', TESTFN,
496+
status, stdout, stderr = assert_python_ok('-m', 'trace', '-l', filename,
492497
PYTHONIOENCODING='utf-8')
493498
self.assertIn(b'functions called:', stdout)
499+
expected = f'filename: {filename}, modulename: {modulename}, funcname: <module>'
500+
self.assertIn(expected.encode(), stdout)
494501

495502
def test_sys_argv_list(self):
496-
with open(TESTFN, 'w') as fd:
503+
with open(TESTFN, 'w', encoding='utf-8') as fd:
497504
self.addCleanup(unlink, TESTFN)
498505
fd.write("import sys\n")
499506
fd.write("print(type(sys.argv))\n")
@@ -506,7 +513,8 @@ def test_sys_argv_list(self):
506513
def test_count_and_summary(self):
507514
filename = f'{TESTFN}.py'
508515
coverfilename = f'{TESTFN}.cover'
509-
with open(filename, 'w') as fd:
516+
modulename = os.path.basename(TESTFN)
517+
with open(filename, 'w', encoding='utf-8') as fd:
510518
self.addCleanup(unlink, filename)
511519
self.addCleanup(unlink, coverfilename)
512520
fd.write(textwrap.dedent("""\
@@ -524,7 +532,7 @@ def f():
524532
stdout = stdout.decode()
525533
self.assertEqual(status, 0)
526534
self.assertIn('lines cov% module (path)', stdout)
527-
self.assertIn(f'6 100% {TESTFN} ({filename})', stdout)
535+
self.assertIn(f'6 100% {modulename} ({filename})', stdout)
528536

529537
def test_run_as_module(self):
530538
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

@@ -715,7 +716,7 @@ def parse_ignore_dir(s):
715716
sys.argv = [opts.progname, *opts.arguments]
716717
sys.path[0] = os.path.dirname(opts.progname)
717718

718-
with open(opts.progname) as fp:
719+
with open(opts.progname, 'rb') as fp:
719720
code = compile(fp.read(), opts.progname, 'exec')
720721
# try to emulate __main__ namespace as much as possible
721722
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)