Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/7781.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix writing non-encodable text to log file when using ``--debug``.
1 change: 1 addition & 0 deletions changelog/7786.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix setting of ``PYTEST_CURRENT_TEST`` when the current locale cannot encode the test name.
2 changes: 1 addition & 1 deletion src/_pytest/helpconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def pytest_cmdline_parse():
config = outcome.get_result() # type: Config
if config.option.debug:
path = os.path.abspath("pytestdebug.log")
debugfile = open(path, "w")
debugfile = open(path, "w", encoding="UTF-8")
debugfile.write(
"versions pytest-%s, py-%s, "
"python-%s\ncwd=%s\nargs=%s\n\n"
Expand Down
8 changes: 7 additions & 1 deletion src/_pytest/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,13 @@ def _update_current_test_var(
value = "{} ({})".format(item.nodeid, when)
# don't allow null bytes on environment variables (see #2644, #2957)
value = value.replace("\x00", "(null)")
os.environ[var_name] = value
# if we are running with a locale that cannot encode the test name
# and we're on a system where os.environb is supported, explicitly
# encode using UTF-8 (otherwise we get an EncodeError)
if os.supports_bytes_environ:
os.environb[var_name.encode()] = value.encode()
else:
os.environ[var_name] = value
else:
os.environ.pop(var_name)

Expand Down
15 changes: 15 additions & 0 deletions testing/test_helpconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,21 @@ def test_debug(testdir):
assert "pytest_sessionstart" in p.read()


def test_debug_with_non_ascii_test_name(testdir, monkeypatch):
testdir.makepyfile(
"""\
def test4_чћшђ_čćšđ():
pass
"""
)
# force ASCII default io encoding (PYTHONIOENCODING didn't seem to work?)
monkeypatch.setenv("LANG", "C")
result = testdir.runpytest_subprocess("--debug")
assert result.ret == ExitCode.OK
log_contents = testdir.tmpdir.join("pytestdebug.log").read_binary()
assert b"pytest_sessionstart" in log_contents


def test_PYTEST_DEBUG(testdir, monkeypatch):
monkeypatch.setenv("PYTEST_DEBUG", "1")
result = testdir.runpytest_subprocess()
Expand Down