Skip to content

[3.10] gh-91924: Fix __ltrace__ for non-UTF-8 stdout encoding #93214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 25, 2022
Merged
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
71 changes: 69 additions & 2 deletions Lib/test/test_lltrace.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import os
import opcode
import re
import sys
import textwrap
import unittest

from test.support import os_helper
from test.support import os_helper, verbose
from test.support.script_helper import assert_python_ok


Py_DEBUG = hasattr(sys, 'gettotalrefcount')

@unittest.skipUnless(Py_DEBUG, "lltrace requires Py_DEBUG")
class TestLLTrace(unittest.TestCase):

def test_lltrace_does_not_crash_on_subscript_operator(self):
Expand All @@ -27,5 +32,67 @@ def test_lltrace_does_not_crash_on_subscript_operator(self):

assert_python_ok(os_helper.TESTFN)

def run_code(self, code):
code = textwrap.dedent(code).strip()
with open(os_helper.TESTFN, 'w', encoding='utf-8') as fd:
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
fd.write(code)
status, stdout, stderr = assert_python_ok(os_helper.TESTFN)
self.assertEqual(stderr, b"")
self.assertEqual(status, 0)
result = stdout.decode('utf-8')
if verbose:
print("\n\n--- code ---")
print(code)
print("\n--- stdout ---")
print(result)
print()
return result

def check_op(self, op, stdout, present):
op = opcode.opmap[op]
regex = re.compile(f': {op}($|, )', re.MULTILINE)
if present:
self.assertTrue(regex.search(stdout),
f'": {op}" not found in: {stdout}')
else:
self.assertFalse(regex.search(stdout),
f'": {op}" found in: {stdout}')

def check_op_in(self, op, stdout):
self.check_op(op, stdout, True)

def check_op_not_in(self, op, stdout):
self.check_op(op, stdout, False)

def test_lltrace(self):
stdout = self.run_code("""
def dont_trace_1():
a = "a"
a = 10 * a
def trace_me():
for i in range(3):
+i
def dont_trace_2():
x = 42
y = -x
dont_trace_1()
__ltrace__ = 1
trace_me()
del __ltrace__
dont_trace_2()
""")
self.check_op_in("GET_ITER", stdout)
self.check_op_in("FOR_ITER", stdout)
self.check_op_in("UNARY_POSITIVE", stdout)
self.check_op_in("POP_TOP", stdout)

# before: dont_trace_1() is not traced
self.check_op_not_in("BINARY_MULTIPLY", stdout)

# after: dont_trace_2() is not traced
self.check_op_not_in("UNARY_NEGATIVE", stdout)


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix ``__ltrace__`` debug feature if the stdout encoding is not UTF-8. Patch
by Victor Stinner.
2 changes: 2 additions & 0 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -5377,6 +5377,8 @@ prtrace(PyThreadState *tstate, PyObject *v, const char *str)
}
printf("\n");
PyErr_Restore(type, value, traceback);
// gh-91924: PyObject_Print() can indirectly set lltrace to 0
lltrace = 1;
return 1;
}
#endif
Expand Down