Repository navigation
Never pass a non-string function wrapper binding to str only C API - #371
Merged
Merged
Conversation
The C extension compared the binding of a function wrapper using PyUnicode_CompareWithASCIIString(), which does not check the type of the object it is given, so a binding which was not a string crashed the interpreter. A wrapper can be constructed directly with any object as the binding, and a FunctionWrapper wrapping an existing wrapper, such as when a decorator is applied to it, inherits that binding. The crash occurred when calling a bound or unbound wrapper and when binding one via the descriptor protocol. All binding comparisons now use wrapt_name_equals() against the interned binding names in module state. A non-string binding never matches, so the wrapper takes the same fallback path as in the pure Python implementation. Closes #368. Closes #369.
This was referenced Nov 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When using the C extension, a
wrapt.BoundFunctionWrapperorwrapt.FunctionWrapperwhose binding was not a string could crash the interpreter, as reported in #368:The C extension compared the binding using
PyUnicode_CompareWithASCIIString(), which does not check the type of the object it is given. The binding is normally one of the interned strings wrapt chooses itself, but a wrapper can be constructed directly with any object as the binding, and awrapt.FunctionWrapperwrapping an existing wrapper, as happens when a decorator is applied to it, inherits that binding. The comparison was made in five places insrc/wrapt/_wrappers.c, in the following code paths:BoundFunctionWrapper.__call__(), the case in the report._FunctionWrapperBase.__call__()when there is no instance, so anyFunctionWrapperwhich inherited such a binding.__get__()on an unbound wrapper, checking for the"builtin"and"class"bindings.__get__()on a bound wrapper with no instance, which rebinds via the parent.All of these now use the existing
wrapt_name_equals()helper against the interned binding names held in module state. It returns false for anything which is not astr, so a non-string binding takes the same fallback path as in the pure Python implementation. Astrsubclass is still compared by value. The comment onwrapt_name_equals()has been updated to state that it accepts any object.Tests
tests/core/test_non_string_binding.pycovers each of the crash sites, with expected behaviour matching the pure Python implementation, plus string bindings, includingstrsubclasses, to check these are still recognised.just testpasses on all 24 runs: Python 3.10 to 3.15, plus 3.14t and 3.15t, each without C extensions, with C extensions, and with C extensions disabled at runtime.Acknowledgements
Thanks to @theosotr for the report in #368, and to @edsonsims for #369, which fixed the case of calling a
wrapt.BoundFunctionWrapper. This PR extends that fix to the other places the binding is checked, and supersedes #369. Both are credited in the change note indocs/changes.rst.Closes #368.
Closes #369.