Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 64bc3b2

Browse files
author
Victor Stinner
committed
Issue #10329: The trace module writes reports using the input Python script
encoding, instead of the locale encoding. Patch written by Alexander Belopolsky.
1 parent bb4f218 commit 64bc3b2

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

Lib/trace.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,10 @@ def write_results(self, show_missing=True, summary=False, coverdir=None):
330330

331331
source = linecache.getlines(filename)
332332
coverpath = os.path.join(dir, modulename + ".cover")
333+
with open(filename, 'rb') as fp:
334+
encoding, _ = tokenize.detect_encoding(fp.readline)
333335
n_hits, n_lines = self.write_results_file(coverpath, source,
334-
lnotab, count)
335-
336+
lnotab, count, encoding)
336337
if summary and n_lines:
337338
percent = int(100 * n_hits / n_lines)
338339
sums[modulename] = n_lines, percent, modulename, filename
@@ -351,11 +352,11 @@ def write_results(self, show_missing=True, summary=False, coverdir=None):
351352
except IOError as err:
352353
print("Can't save counts files because %s" % err, file=sys.stderr)
353354

354-
def write_results_file(self, path, lines, lnotab, lines_hit):
355+
def write_results_file(self, path, lines, lnotab, lines_hit, encoding=None):
355356
"""Return a coverage results file in path."""
356357

357358
try:
358-
outfile = open(path, "w")
359+
outfile = open(path, "w", encoding=encoding)
359360
except IOError as err:
360361
print(("trace: Could not open %r for writing: %s"
361362
"- skipping" % (path, err)), file=sys.stderr)

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ Core and Builtins
6565
Library
6666
-------
6767

68+
- Issue #10329: The trace module writes reports using the input Python script
69+
encoding, instead of the locale encoding. Patch written by Alexander
70+
Belopolsky.
71+
6872
- Issue #10126: Fix distutils' test_build when Python was built with
6973
--enable-shared.
7074

0 commit comments

Comments
 (0)