Skip to content

refactor: remove needless load_src alias in string_caster#6093

Merged
rwgk merged 1 commit into
masterfrom
cleanup-load-src-alias
Jun 24, 2026
Merged

refactor: remove needless load_src alias in string_caster#6093
rwgk merged 1 commit into
masterfrom
cleanup-load-src-alias

Conversation

@henryiii

Copy link
Copy Markdown
Collaborator

🤖 AI text below 🤖

Description

string_caster::load declared handle load_src = src; and then only ever read from it. This is a Python 2-era leftover: a now-deleted branch used to reassign load_src to a temporary unicode object created from a Python 2 str/bytes, so the downstream PyUnicode_* calls operated on the temp rather than the original src. That reassignment was removed along with Python 2 support, leaving load_src an exact, never-reassigned alias of src.

This drops the alias and uses src directly. handle is a non-owning, trivially-copyable PyObject* wrapper, so there is no runtime behavior change.

Spotted in review of #6092.

Suggested changelog entry

(none — internal cleanup, no user-facing change)

`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 rwgk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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
    original std::wstring caster. That code created object temp, initialized
    handle load_src = src, and reassigned load_src = temp after calling
    PyUnicode_FromObject(...) for non-Unicode inputs.

  • 11a337f (Unicode fixes and docs (#624), 2017-02-14) unified the string
    handling for std::basic_string<CharT, ...>. The same load_src pattern 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 call PyUnicode_FromObject on 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, both object temp and the
    load_src = temp reassignment were deleted, but handle load_src = src
    remained.

  • 4a00e11 (refactor: remove needless load_src alias in string_caster,
    2026-06-23, branch upstream/cleanup-load-src-alias) removes the now-unused
    alias and switches direct uses to src.

@rwgk rwgk merged commit 84da0e2 into master Jun 24, 2026
87 checks passed
@rwgk rwgk deleted the cleanup-load-src-alias branch June 24, 2026 04:16
@github-actions github-actions Bot added the needs changelog Possibly needs a changelog entry label Jun 24, 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