From 33320bdb07f34927efe74a150d452600157f292b Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 11 Jun 2026 17:11:32 -0400 Subject: [PATCH] chore(eval): drop Python 2 coding cookie prepended to evaluated source MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- include/pybind11/eval.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/include/pybind11/eval.h b/include/pybind11/eval.h index a588729182..ee24fd3584 100644 --- a/include/pybind11/eval.h +++ b/include/pybind11/eval.h @@ -52,9 +52,8 @@ object eval(const str &expr, object global = globals(), object local = object()) 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) {