Skip to content

chore(eval): drop Python 2 coding cookie prepended to evaluated source#6089

Open
henryiii wants to merge 1 commit into
pybind:masterfrom
henryiii:chore/eval-drop-coding-cookie
Open

chore(eval): drop Python 2 coding cookie prepended to evaluated source#6089
henryiii wants to merge 1 commit into
pybind:masterfrom
henryiii:chore/eval-drop-coding-cookie

Conversation

@henryiii

Copy link
Copy Markdown
Collaborator

🤖 AI text below 🤖

Summary

  • Remove the # -*- coding: utf-8 -*- line that was prepended to every string passed to PyRun_String in py::eval / py::exec.
  • Python 3's PyRun_String already assumes UTF-8 source — the cookie is a Python 2 artifact that serves no purpose.
  • User-visible fix: the cookie shifted all source line numbers up by one in tracebacks and SyntaxErrors from evaluated code. Code that raises on line 2 of the evaluated string now correctly appears as line 2 in tracebacks (previously it appeared as line 3).
  • Update the adjacent comment to reflect the actual reason for the std::string conversion (obtaining a const char* for PyRun_String).
  • eval_file reads directly from a FILE* via PyRun_FileEx and was never affected.

Verification

Tested on macOS with Python 3.14 by compiling a scratch extension and confirming:

  1. py::eval("1+1") returns 2.
  2. A UTF-8 string literal in evaluated source (héllo) round-trips correctly.
  3. 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

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
@henryiii henryiii marked this pull request as ready for review June 11, 2026 22:45
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.

1 participant