Skip to content

Commit 1e32a4b

Browse files
authored
Merge pull request #10935 from nondescryptid/10328
2 parents faa1f9d + f6b995e commit 1e32a4b

35 files changed

+441
-257
lines changed

changelog/7781.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix writing non-encodable text to log file when using ``--debug``.

scripts/towncrier-draft-to-file.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ def main():
77
Platform agnostic wrapper script for towncrier.
88
Fixes the issue (#7251) where windows users are unable to natively run tox -e docs to build pytest docs.
99
"""
10-
with open("doc/en/_changelog_towncrier_draft.rst", "w") as draft_file:
10+
with open(
11+
"doc/en/_changelog_towncrier_draft.rst", "w", encoding="utf-8"
12+
) as draft_file:
1113
return call(("towncrier", "--draft"), stdout=draft_file)
1214

1315

src/_pytest/helpconfig.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def pytest_cmdline_parse():
105105
if config.option.debug:
106106
# --debug | --debug <file.log> was provided.
107107
path = config.option.debug
108-
debugfile = open(path, "w")
108+
debugfile = open(path, "w", encoding="utf-8")
109109
debugfile.write(
110110
"versions pytest-%s, "
111111
"python-%s\ncwd=%s\nargs=%s\n\n"

src/_pytest/pytester.py

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import contextlib
77
import gc
88
import importlib
9+
import locale
910
import os
1011
import platform
1112
import re
@@ -129,6 +130,7 @@ def get_open_files(self) -> List[Tuple[str, str]]:
129130
stderr=subprocess.DEVNULL,
130131
check=True,
131132
text=True,
133+
encoding=locale.getpreferredencoding(False),
132134
).stdout
133135

134136
def isopen(line: str) -> bool:

testing/_py/test_local.py

+59-39
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1+
import contextlib
12
import multiprocessing
23
import os
34
import sys
45
import time
6+
import warnings
57
from unittest import mock
68

79
import pytest
810
from py import error
911
from py.path import local
1012

1113

14+
@contextlib.contextmanager
15+
def ignore_encoding_warning():
16+
with warnings.catch_warnings():
17+
with contextlib.suppress(NameError): # new in 3.10
18+
warnings.simplefilter("ignore", EncodingWarning)
19+
yield
20+
21+
1222
class CommonFSTests:
1323
def test_constructor_equality(self, path1):
1424
p = path1.__class__(path1)
@@ -223,7 +233,8 @@ def test_cmp(self, path1):
223233
assert not (path1 < path1)
224234

225235
def test_simple_read(self, path1):
226-
x = path1.join("samplefile").read("r")
236+
with ignore_encoding_warning():
237+
x = path1.join("samplefile").read("r")
227238
assert x == "samplefile\n"
228239

229240
def test_join_div_operator(self, path1):
@@ -265,12 +276,14 @@ def test_newext(self, path1):
265276

266277
def test_readlines(self, path1):
267278
fn = path1.join("samplefile")
268-
contents = fn.readlines()
279+
with ignore_encoding_warning():
280+
contents = fn.readlines()
269281
assert contents == ["samplefile\n"]
270282

271283
def test_readlines_nocr(self, path1):
272284
fn = path1.join("samplefile")
273-
contents = fn.readlines(cr=0)
285+
with ignore_encoding_warning():
286+
contents = fn.readlines(cr=0)
274287
assert contents == ["samplefile", ""]
275288

276289
def test_file(self, path1):
@@ -362,8 +375,8 @@ def test_copy_file(self, path1):
362375
initpy.copy(copied)
363376
try:
364377
assert copied.check()
365-
s1 = initpy.read()
366-
s2 = copied.read()
378+
s1 = initpy.read_text(encoding="utf-8")
379+
s2 = copied.read_text(encoding="utf-8")
367380
assert s1 == s2
368381
finally:
369382
if copied.check():
@@ -376,8 +389,8 @@ def test_copy_dir(self, path1):
376389
otherdir.copy(copied)
377390
assert copied.check(dir=1)
378391
assert copied.join("__init__.py").check(file=1)
379-
s1 = otherdir.join("__init__.py").read()
380-
s2 = copied.join("__init__.py").read()
392+
s1 = otherdir.join("__init__.py").read_text(encoding="utf-8")
393+
s2 = copied.join("__init__.py").read_text(encoding="utf-8")
381394
assert s1 == s2
382395
finally:
383396
if copied.check(dir=1):
@@ -463,13 +476,13 @@ def setuptestfs(path):
463476
return
464477
# print "setting up test fs for", repr(path)
465478
samplefile = path.ensure("samplefile")
466-
samplefile.write("samplefile\n")
479+
samplefile.write_text("samplefile\n", encoding="utf-8")
467480

468481
execfile = path.ensure("execfile")
469-
execfile.write("x=42")
482+
execfile.write_text("x=42", encoding="utf-8")
470483

471484
execfilepy = path.ensure("execfile.py")
472-
execfilepy.write("x=42")
485+
execfilepy.write_text("x=42", encoding="utf-8")
473486

474487
d = {1: 2, "hello": "world", "answer": 42}
475488
path.ensure("samplepickle").dump(d)
@@ -481,22 +494,24 @@ def setuptestfs(path):
481494
otherdir.ensure("__init__.py")
482495

483496
module_a = otherdir.ensure("a.py")
484-
module_a.write("from .b import stuff as result\n")
497+
module_a.write_text("from .b import stuff as result\n", encoding="utf-8")
485498
module_b = otherdir.ensure("b.py")
486-
module_b.write('stuff="got it"\n')
499+
module_b.write_text('stuff="got it"\n', encoding="utf-8")
487500
module_c = otherdir.ensure("c.py")
488-
module_c.write(
501+
module_c.write_text(
489502
"""import py;
490503
import otherdir.a
491504
value = otherdir.a.result
492-
"""
505+
""",
506+
encoding="utf-8",
493507
)
494508
module_d = otherdir.ensure("d.py")
495-
module_d.write(
509+
module_d.write_text(
496510
"""import py;
497511
from otherdir import a
498512
value2 = a.result
499-
"""
513+
""",
514+
encoding="utf-8",
500515
)
501516

502517

@@ -534,9 +549,11 @@ def batch_make_numbered_dirs(rootdir, repeats):
534549
for i in range(repeats):
535550
dir_ = local.make_numbered_dir(prefix="repro-", rootdir=rootdir)
536551
file_ = dir_.join("foo")
537-
file_.write("%s" % i)
538-
actual = int(file_.read())
539-
assert actual == i, f"int(file_.read()) is {actual} instead of {i}"
552+
file_.write_text("%s" % i, encoding="utf-8")
553+
actual = int(file_.read_text(encoding="utf-8"))
554+
assert (
555+
actual == i
556+
), f"int(file_.read_text(encoding='utf-8')) is {actual} instead of {i}"
540557
dir_.join(".lock").remove(ignore_errors=True)
541558
return True
542559

@@ -692,14 +709,14 @@ def test_gt_with_strings(self, path1):
692709

693710
def test_open_and_ensure(self, path1):
694711
p = path1.join("sub1", "sub2", "file")
695-
with p.open("w", ensure=1) as f:
712+
with p.open("w", ensure=1, encoding="utf-8") as f:
696713
f.write("hello")
697-
assert p.read() == "hello"
714+
assert p.read_text(encoding="utf-8") == "hello"
698715

699716
def test_write_and_ensure(self, path1):
700717
p = path1.join("sub1", "sub2", "file")
701-
p.write("hello", ensure=1)
702-
assert p.read() == "hello"
718+
p.write_text("hello", ensure=1, encoding="utf-8")
719+
assert p.read_text(encoding="utf-8") == "hello"
703720

704721
@pytest.mark.parametrize("bin", (False, True))
705722
def test_dump(self, tmpdir, bin):
@@ -770,9 +787,9 @@ def test_ensure_filepath_withdir(self, tmpdir):
770787
newfile = tmpdir.join("test1", "test")
771788
newfile.ensure()
772789
assert newfile.check(file=1)
773-
newfile.write("42")
790+
newfile.write_text("42", encoding="utf-8")
774791
newfile.ensure()
775-
s = newfile.read()
792+
s = newfile.read_text(encoding="utf-8")
776793
assert s == "42"
777794

778795
def test_ensure_filepath_withoutdir(self, tmpdir):
@@ -806,9 +823,9 @@ def test_long_filenames(self, tmpdir):
806823
newfilename = "/test" * 60 # type:ignore[unreachable]
807824
l1 = tmpdir.join(newfilename)
808825
l1.ensure(file=True)
809-
l1.write("foo")
826+
l1.write_text("foo", encoding="utf-8")
810827
l2 = tmpdir.join(newfilename)
811-
assert l2.read() == "foo"
828+
assert l2.read_text(encoding="utf-8") == "foo"
812829

813830
def test_visit_depth_first(self, tmpdir):
814831
tmpdir.ensure("a", "1")
@@ -1278,22 +1295,22 @@ class TestPOSIXLocalPath:
12781295
def test_hardlink(self, tmpdir):
12791296
linkpath = tmpdir.join("test")
12801297
filepath = tmpdir.join("file")
1281-
filepath.write("Hello")
1298+
filepath.write_text("Hello", encoding="utf-8")
12821299
nlink = filepath.stat().nlink
12831300
linkpath.mklinkto(filepath)
12841301
assert filepath.stat().nlink == nlink + 1
12851302

12861303
def test_symlink_are_identical(self, tmpdir):
12871304
filepath = tmpdir.join("file")
1288-
filepath.write("Hello")
1305+
filepath.write_text("Hello", encoding="utf-8")
12891306
linkpath = tmpdir.join("test")
12901307
linkpath.mksymlinkto(filepath)
12911308
assert linkpath.readlink() == str(filepath)
12921309

12931310
def test_symlink_isfile(self, tmpdir):
12941311
linkpath = tmpdir.join("test")
12951312
filepath = tmpdir.join("file")
1296-
filepath.write("")
1313+
filepath.write_text("", encoding="utf-8")
12971314
linkpath.mksymlinkto(filepath)
12981315
assert linkpath.check(file=1)
12991316
assert not linkpath.check(link=0, file=1)
@@ -1302,10 +1319,12 @@ def test_symlink_isfile(self, tmpdir):
13021319
def test_symlink_relative(self, tmpdir):
13031320
linkpath = tmpdir.join("test")
13041321
filepath = tmpdir.join("file")
1305-
filepath.write("Hello")
1322+
filepath.write_text("Hello", encoding="utf-8")
13061323
linkpath.mksymlinkto(filepath, absolute=False)
13071324
assert linkpath.readlink() == "file"
1308-
assert filepath.read() == linkpath.read()
1325+
assert filepath.read_text(encoding="utf-8") == linkpath.read_text(
1326+
encoding="utf-8"
1327+
)
13091328

13101329
def test_symlink_not_existing(self, tmpdir):
13111330
linkpath = tmpdir.join("testnotexisting")
@@ -1338,7 +1357,7 @@ def test_symlink_remove(self, tmpdir):
13381357
def test_realpath_file(self, tmpdir):
13391358
linkpath = tmpdir.join("test")
13401359
filepath = tmpdir.join("file")
1341-
filepath.write("")
1360+
filepath.write_text("", encoding="utf-8")
13421361
linkpath.mksymlinkto(filepath)
13431362
realpath = linkpath.realpath()
13441363
assert realpath.basename == "file"
@@ -1383,7 +1402,7 @@ def test_atime(self, tmpdir):
13831402
atime1 = path.atime()
13841403
# we could wait here but timer resolution is very
13851404
# system dependent
1386-
path.read()
1405+
path.read_binary()
13871406
time.sleep(ATIME_RESOLUTION)
13881407
atime2 = path.atime()
13891408
time.sleep(ATIME_RESOLUTION)
@@ -1467,7 +1486,7 @@ def test_copy_stat_dir(self, tmpdir):
14671486
test_files = ["a", "b", "c"]
14681487
src = tmpdir.join("src")
14691488
for f in test_files:
1470-
src.join(f).write(f, ensure=True)
1489+
src.join(f).write_text(f, ensure=True, encoding="utf-8")
14711490
dst = tmpdir.join("dst")
14721491
# a small delay before the copy
14731492
time.sleep(ATIME_RESOLUTION)
@@ -1521,10 +1540,11 @@ def test_listdir(self, tmpdir):
15211540
def test_read_write(self, tmpdir):
15221541
x = tmpdir.join("hello")
15231542
part = "hällo"
1524-
x.write(part)
1525-
assert x.read() == part
1526-
x.write(part.encode(sys.getdefaultencoding()))
1527-
assert x.read() == part.encode(sys.getdefaultencoding())
1543+
with ignore_encoding_warning():
1544+
x.write(part)
1545+
assert x.read() == part
1546+
x.write(part.encode(sys.getdefaultencoding()))
1547+
assert x.read() == part.encode(sys.getdefaultencoding())
15281548

15291549

15301550
class TestBinaryAndTextMethods:

0 commit comments

Comments
 (0)