Skip to content

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

Merged
henryiii merged 1 commit into
pybind:masterfrom
henryiii:chore/eval-drop-coding-cookie
Jun 17, 2026
Merged

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

Conversation

@henryiii

@henryiii henryiii commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

Not only is this a Python 2 holdover, but it makes traceback line numbers be off by one!

🤖 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

@virtuald virtuald left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Per https://docs.python.org/3/reference/lexical_analysis.html#encoding-declarations:

If no encoding declaration is found, the default encoding is UTF-8.

@henryiii henryiii merged commit 99117da into pybind:master Jun 17, 2026
106 checks passed
@henryiii henryiii deleted the chore/eval-drop-coding-cookie branch June 17, 2026 12:16
@github-actions github-actions Bot added the needs changelog Possibly needs a changelog entry label Jun 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs changelog Possibly needs a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants