chore(eval): drop Python 2 coding cookie prepended to evaluated source (#6089)
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
diff --git a/include/pybind11/eval.h b/include/pybind11/eval.h
index a588729..ee24fd3 100644
--- a/include/pybind11/eval.h
+++ b/include/pybind11/eval.h
@@ -52,9 +52,8 @@
detail::ensure_builtins_in_globals(global);
- /* PyRun_String does not accept a PyObject / encoding specifier,
- this seems to be the only alternative */
- std::string buffer = "# -*- coding: utf-8 -*-\n" + (std::string) expr;
+ // PyRun_String requires a plain C string; Python 3 always assumes UTF-8 source.
+ std::string buffer = (std::string) expr;
int start = 0;
switch (mode) {