bpo-41138: Fix trace CLI for non-UTF-8 files. (GH-21177)
Fix also a resource warning when store counts and module info.
This commit is contained in:
parent
cd3c2bdd5d
commit
04cdeb7a56
|
@ -1,6 +1,6 @@
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
from test.support import TESTFN, rmtree, unlink, captured_stdout
|
from test.support import TESTFN, TESTFN_UNICODE, FS_NONASCII, rmtree, unlink, captured_stdout
|
||||||
from test.support.script_helper import assert_python_ok, assert_python_failure
|
from test.support.script_helper import assert_python_ok, assert_python_failure
|
||||||
import textwrap
|
import textwrap
|
||||||
import unittest
|
import unittest
|
||||||
|
@ -428,9 +428,10 @@ class TestCoverageCommandLineOutput(unittest.TestCase):
|
||||||
coverfile = 'tmp.cover'
|
coverfile = 'tmp.cover'
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
with open(self.codefile, 'w') as f:
|
with open(self.codefile, 'w', encoding='iso-8859-15') as f:
|
||||||
f.write(textwrap.dedent('''\
|
f.write(textwrap.dedent('''\
|
||||||
x = 42
|
# coding: iso-8859-15
|
||||||
|
x = 'spœm'
|
||||||
if []:
|
if []:
|
||||||
print('unreachable')
|
print('unreachable')
|
||||||
'''))
|
'''))
|
||||||
|
@ -451,9 +452,10 @@ class TestCoverageCommandLineOutput(unittest.TestCase):
|
||||||
self.assertEqual(stderr, b'')
|
self.assertEqual(stderr, b'')
|
||||||
self.assertFalse(os.path.exists(tracecoverpath))
|
self.assertFalse(os.path.exists(tracecoverpath))
|
||||||
self.assertTrue(os.path.exists(self.coverfile))
|
self.assertTrue(os.path.exists(self.coverfile))
|
||||||
with open(self.coverfile) as f:
|
with open(self.coverfile, encoding='iso-8859-15') as f:
|
||||||
self.assertEqual(f.read(),
|
self.assertEqual(f.read(),
|
||||||
" 1: x = 42\n"
|
" # coding: iso-8859-15\n"
|
||||||
|
" 1: x = 'spœm'\n"
|
||||||
" 1: if []:\n"
|
" 1: if []:\n"
|
||||||
" print('unreachable')\n"
|
" print('unreachable')\n"
|
||||||
)
|
)
|
||||||
|
@ -462,9 +464,10 @@ class TestCoverageCommandLineOutput(unittest.TestCase):
|
||||||
argv = '-m trace --count --missing'.split() + [self.codefile]
|
argv = '-m trace --count --missing'.split() + [self.codefile]
|
||||||
status, stdout, stderr = assert_python_ok(*argv)
|
status, stdout, stderr = assert_python_ok(*argv)
|
||||||
self.assertTrue(os.path.exists(self.coverfile))
|
self.assertTrue(os.path.exists(self.coverfile))
|
||||||
with open(self.coverfile) as f:
|
with open(self.coverfile, encoding='iso-8859-15') as f:
|
||||||
self.assertEqual(f.read(), textwrap.dedent('''\
|
self.assertEqual(f.read(), textwrap.dedent('''\
|
||||||
1: x = 42
|
# coding: iso-8859-15
|
||||||
|
1: x = 'spœm'
|
||||||
1: if []:
|
1: if []:
|
||||||
>>>>>> print('unreachable')
|
>>>>>> print('unreachable')
|
||||||
'''))
|
'''))
|
||||||
|
@ -485,15 +488,19 @@ class TestCommandLine(unittest.TestCase):
|
||||||
self.assertIn(message, stderr)
|
self.assertIn(message, stderr)
|
||||||
|
|
||||||
def test_listfuncs_flag_success(self):
|
def test_listfuncs_flag_success(self):
|
||||||
with open(TESTFN, 'w') as fd:
|
filename = TESTFN + '.py'
|
||||||
self.addCleanup(unlink, TESTFN)
|
modulename = os.path.basename(TESTFN)
|
||||||
|
with open(filename, 'w', encoding='utf-8') as fd:
|
||||||
|
self.addCleanup(unlink, filename)
|
||||||
fd.write("a = 1\n")
|
fd.write("a = 1\n")
|
||||||
status, stdout, stderr = assert_python_ok('-m', 'trace', '-l', TESTFN,
|
status, stdout, stderr = assert_python_ok('-m', 'trace', '-l', filename,
|
||||||
PYTHONIOENCODING='utf-8')
|
PYTHONIOENCODING='utf-8')
|
||||||
self.assertIn(b'functions called:', stdout)
|
self.assertIn(b'functions called:', stdout)
|
||||||
|
expected = f'filename: {filename}, modulename: {modulename}, funcname: <module>'
|
||||||
|
self.assertIn(expected.encode(), stdout)
|
||||||
|
|
||||||
def test_sys_argv_list(self):
|
def test_sys_argv_list(self):
|
||||||
with open(TESTFN, 'w') as fd:
|
with open(TESTFN, 'w', encoding='utf-8') as fd:
|
||||||
self.addCleanup(unlink, TESTFN)
|
self.addCleanup(unlink, TESTFN)
|
||||||
fd.write("import sys\n")
|
fd.write("import sys\n")
|
||||||
fd.write("print(type(sys.argv))\n")
|
fd.write("print(type(sys.argv))\n")
|
||||||
|
@ -506,7 +513,8 @@ class TestCommandLine(unittest.TestCase):
|
||||||
def test_count_and_summary(self):
|
def test_count_and_summary(self):
|
||||||
filename = f'{TESTFN}.py'
|
filename = f'{TESTFN}.py'
|
||||||
coverfilename = f'{TESTFN}.cover'
|
coverfilename = f'{TESTFN}.cover'
|
||||||
with open(filename, 'w') as fd:
|
modulename = os.path.basename(TESTFN)
|
||||||
|
with open(filename, 'w', encoding='utf-8') as fd:
|
||||||
self.addCleanup(unlink, filename)
|
self.addCleanup(unlink, filename)
|
||||||
self.addCleanup(unlink, coverfilename)
|
self.addCleanup(unlink, coverfilename)
|
||||||
fd.write(textwrap.dedent("""\
|
fd.write(textwrap.dedent("""\
|
||||||
|
@ -524,7 +532,7 @@ class TestCommandLine(unittest.TestCase):
|
||||||
stdout = stdout.decode()
|
stdout = stdout.decode()
|
||||||
self.assertEqual(status, 0)
|
self.assertEqual(status, 0)
|
||||||
self.assertIn('lines cov% module (path)', stdout)
|
self.assertIn('lines cov% module (path)', stdout)
|
||||||
self.assertIn(f'6 100% {TESTFN} ({filename})', stdout)
|
self.assertIn(f'6 100% {modulename} ({filename})', stdout)
|
||||||
|
|
||||||
def test_run_as_module(self):
|
def test_run_as_module(self):
|
||||||
assert_python_ok('-m', 'trace', '-l', '--module', 'timeit', '-n', '1')
|
assert_python_ok('-m', 'trace', '-l', '--module', 'timeit', '-n', '1')
|
||||||
|
|
|
@ -287,8 +287,9 @@ class CoverageResults:
|
||||||
if self.outfile:
|
if self.outfile:
|
||||||
# try and store counts and module info into self.outfile
|
# try and store counts and module info into self.outfile
|
||||||
try:
|
try:
|
||||||
|
with open(self.outfile, 'wb') as f:
|
||||||
pickle.dump((self.counts, self.calledfuncs, self.callers),
|
pickle.dump((self.counts, self.calledfuncs, self.callers),
|
||||||
open(self.outfile, 'wb'), 1)
|
f, 1)
|
||||||
except OSError as err:
|
except OSError as err:
|
||||||
print("Can't save counts files because %s" % err, file=sys.stderr)
|
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.argv = [opts.progname, *opts.arguments]
|
||||||
sys.path[0] = os.path.dirname(opts.progname)
|
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')
|
code = compile(fp.read(), opts.progname, 'exec')
|
||||||
# try to emulate __main__ namespace as much as possible
|
# try to emulate __main__ namespace as much as possible
|
||||||
globs = {
|
globs = {
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fixed the :mod:`trace` module CLI for Python source files with non-UTF-8
|
||||||
|
encoding.
|
Loading…
Reference in New Issue