Skip to content

gh-91924: Fix __lltrace__ for non-UTF-8 stdout encoding #93199

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

gh-91924: Fix __lltrace__ for non-UTF-8 stdout encoding #93199

merged 2 commits into from
May 25, 2022

Conversation

vstinner
Copy link
Member

Fix lltrace debug feature if the stdout encoding is not UTF-8.

If the stdout encoding is not UTF-8, the first call to
lltrace_resume_frame() indirectly sets lltrace to 0 when calling
unicode_check_encoding_errors() which calls
encodings.search_function().

@vstinner
Copy link
Member Author

Python 3.10 is also affected: lltrace must be set to 0 in prtrace(). In Python 3.10, the feature is enabled by setting __ltrace__ with a single L, whereas Python 3.11 and 3.12 uses __lltrace__ with two L!

@sweeneyde
Copy link
Member

sweeneyde commented May 25, 2022

Making lltrace a local variable would fix 3.10 as well, right?

@vstinner
Copy link
Member Author

The __lltrace__ feature is only mentioned in the doc of Python debug build: https://docs.python.org/dev/using/configure.html#python-debug-build

@sweeneyde
Copy link
Member

Spelling was originally changed by 3c1e481, but changed back by 37965d2

Fix __lltrace__ debug feature if the stdout encoding is not UTF-8.

If the stdout encoding is not UTF-8, the first call to
lltrace_resume_frame() indirectly sets lltrace to 0 when calling
unicode_check_encoding_errors() which calls
encodings.search_function().
@vstinner
Copy link
Member Author

My first implementation set lltrace to 1 at the end of lltrace_instruction() and lltrace_resume_frame() to workaround side effects of recusive calls to _PyEval_EvalFrame().

diff --git a/Python/ceval.c b/Python/ceval.c
index 230198b41f6a5..4d4681099e0df 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -148,6 +148,9 @@ lltrace_instruction(_PyInterpreterFrame *frame,
         printf("%d: %s\n", offset * 2, opname);
     }
     fflush(stdout);
+
+    // gh-91924: PyObject_Print() can indirectly set lltrace to 0
+    lltrace = 1;
 }
 static void
 lltrace_resume_frame(_PyInterpreterFrame *frame)
@@ -179,6 +182,9 @@ lltrace_resume_frame(_PyInterpreterFrame *frame)
     printf("\n");
     fflush(stdout);
     PyErr_Restore(type, value, traceback);
+
+    // gh-91924: PyObject_Print() can indirectly set lltrace to 0
+    lltrace = 1;
 }
 #endif
 static int call_trace(Py_tracefunc, PyObject *,

But @sweeneyde is right, making the variable local is enough. I updated my PR.

@vstinner
Copy link
Member Author

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@vstinner vstinner merged commit 5695c0e into python:main May 25, 2022
@miss-islington
Copy link
Contributor

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.11.
🐍🍒⛏🤖

@vstinner vstinner deleted the fix_lltrace branch May 25, 2022 09:45
@bedevere-bot
Copy link

GH-93212 is a backport of this pull request to the 3.11 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.11 only security fixes label May 25, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 25, 2022
…GH-93199)

Fix __lltrace__ debug feature if the stdout encoding is not UTF-8.

If the stdout encoding is not UTF-8, the first call to
lltrace_resume_frame() indirectly sets lltrace to 0 when calling
unicode_check_encoding_errors() which calls
encodings.search_function().
(cherry picked from commit 5695c0e)

Co-authored-by: Victor Stinner <[email protected]>
miss-islington added a commit that referenced this pull request May 25, 2022
Fix __lltrace__ debug feature if the stdout encoding is not UTF-8.

If the stdout encoding is not UTF-8, the first call to
lltrace_resume_frame() indirectly sets lltrace to 0 when calling
unicode_check_encoding_errors() which calls
encodings.search_function().
(cherry picked from commit 5695c0e)

Co-authored-by: Victor Stinner <[email protected]>
@vstinner
Copy link
Member Author

In the 3.10 branch, making lltrace variable local would require to change call_function(). I prefer to minimize changes and so I wrote a simpler change: PR #93214.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants