refactor: remove needless load_src alias in string_caster#6093
Conversation
`load_src` was a Python 2-era leftover. A removed branch used to reassign it to a temporary unicode object; since that was deleted with Python 2 support, it is now an exact, never-reassigned alias of `src`. Use `src` directly. Spotted in review of #6092. Assisted-by: ClaudeCode:claude-opus-4.8
rwgk
left a comment
There was a problem hiding this comment.
A little bit of forensics (gpt-5.5 with small manual edits):
Apparently this is a Python 2-era leftover. It originally allowed the string caster to replace the input handle with a temporary Unicode object, then let the rest of the function decode that temporary object. After Python 2 support was removed, the reassignment path disappeared but the alias remained.
Evidence From History
The relevant line currently appears in include/pybind11/cast.h inside the UTF string caster. git blame attributes the exact line to an old mechanical rewrite, so the more useful history comes from git log -L and pickaxe searches for load_src.
Important waypoints:
-
c8f68b3 (
add wstring caster, 2016-03-02) introduced the pattern in the
originalstd::wstringcaster. That code createdobject temp, initialized
handle load_src = src, and reassignedload_src = tempafter calling
PyUnicode_FromObject(...)for non-Unicode inputs. -
11a337f (
Unicode fixes and docs (#624), 2017-02-14) unified the string
handling forstd::basic_string<CharT, ...>. The sameload_srcpattern was
carried forward. At that point, the reassignment path was guarded by Python 2
conditionals; Python 3 either failed or used a separate bytes path. -
74b501c (
Fix passing in utf8 encoded strings with python 2,
2017-06-06) confirms the Python 2-specific purpose. The commit message says
the previous code tried to callPyUnicode_FromObjecton Python 2 string
data, and the fix treated Python 2 strings more like Python 3 bytes for
single-byte character types. The wider-character Python 2 path still used
load_src = temp. -
6493f49 (
Python 2 removal part 1: tests (C++ code is intentionally ~untouched) (#3688), 2022-02-10) removed the Python 2 branch from
string_caster::load(). In that diff, bothobject tempand the
load_src = tempreassignment were deleted, buthandle load_src = src
remained. -
4a00e11 (
refactor: remove needless load_src alias in string_caster,
2026-06-23, branchupstream/cleanup-load-src-alias) removes the now-unused
alias and switches direct uses tosrc.
🤖 AI text below 🤖
Description
string_caster::loaddeclaredhandle load_src = src;and then only ever read from it. This is a Python 2-era leftover: a now-deleted branch used to reassignload_srcto a temporary unicode object created from a Python 2str/bytes, so the downstreamPyUnicode_*calls operated on the temp rather than the originalsrc. That reassignment was removed along with Python 2 support, leavingload_srcan exact, never-reassigned alias ofsrc.This drops the alias and uses
srcdirectly.handleis a non-owning, trivially-copyablePyObject*wrapper, so there is no runtime behavior change.Spotted in review of #6092.
Suggested changelog entry
(none — internal cleanup, no user-facing change)