chore(eval): drop Python 2 coding cookie prepended to evaluated source#6089
Open
henryiii wants to merge 1 commit into
Open
chore(eval): drop Python 2 coding cookie prepended to evaluated source#6089henryiii wants to merge 1 commit into
henryiii wants to merge 1 commit into
Conversation
The `# -*- coding: utf-8 -*-` line prepended to every string passed to PyRun_String was a Python 2 workaround. Python 3's PyRun_String already assumes UTF-8 source encoding, so the cookie is dead weight — and harmful: it shifts all line numbers up by one in tracebacks and SyntaxErrors from evaluated code (a `raise` on line 2 would incorrectly appear as line 3). Remove the cookie and replace the stale comment with one that explains the actual reason for the `std::string` conversion (need a C string for PyRun_String). No other location in eval.h uses this pattern (eval_file reads directly from a FILE*). Assisted-by: ClaudeCode:claude-fable-5
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 AI text below 🤖
Summary
# -*- coding: utf-8 -*-line that was prepended to every string passed toPyRun_Stringinpy::eval/py::exec.PyRun_Stringalready assumes UTF-8 source — the cookie is a Python 2 artifact that serves no purpose.SyntaxErrors from evaluated code. Code thatraises on line 2 of the evaluated string now correctly appears as line 2 in tracebacks (previously it appeared as line 3).std::stringconversion (obtaining aconst char*forPyRun_String).eval_filereads directly from aFILE*viaPyRun_FileExand was never affected.Verification
Tested on macOS with Python 3.14 by compiling a scratch extension and confirming:
py::eval("1+1")returns 2.héllo) round-trips correctly.py::exec("x = 1\nraise ValueError('line2error')")now correctly reports the traceback at line 2 (previously line 3 due to the prepended cookie).No tests assert on line numbers in evaluated code, so no test updates were needed.
Part of #6084