[cppyy] Disallow calling functions with non-const pointer references#21655
Draft
guitargeek wants to merge 1 commit intoroot-project:masterfrom
Draft
[cppyy] Disallow calling functions with non-const pointer references#21655guitargeek wants to merge 1 commit intoroot-project:masterfrom
guitargeek wants to merge 1 commit intoroot-project:masterfrom
Conversation
Test Results 22 files 22 suites 3d 8h 2m 38s ⏱️ For more details on these failures, see this check. Results for commit e79d656. ♻️ This comment has been updated with latest results. |
Functions taking non-const references to pointers `(T*&)` allow
rebinding the pointer inside the callee. This behavior does not map
cleanly to Python, which has no equivalent concept of pointer rebinding.
In cppyy, this can lead to confusing and unintended side effects due to
pointer deduplication and proxy identity semantics.
This change explicitly rejects such argument types and raises a
`TypeError` with a clear explanation and suggested alternatives.
Here is an example of previously confusing behavior:
```Python
import cppyy
cppyy.cppdef("""
struct MyClass {
int val = 0;
};
MyClass *staticPtr() {
static MyClass x;
x.val = 1;
return &x;
}
void changePtr(std::string ptr) {
}
void changePtr(MyClass *& ptr) {
static MyClass y;
y.val = 2;
ptr = &y;
}
""")
p1 = cppyy.gbl.staticPtr()
p2 = cppyy.gbl.staticPtr()
cppyy.gbl.changePtr(p2)
print(p1.val)
print(p2.val)
```
Output:
```txt
2
2
```
Here, it was completely not obvious that changing `p2` is changing the
`p1` pointer.
8876264 to
e79d656
Compare
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.
Functions taking non-const references to pointers
(T*&)allow rebinding the pointer inside the callee. This behavior does not map cleanly to Python, which has no equivalent concept of pointer rebinding.In cppyy, this can lead to confusing and unintended side effects due to pointer deduplication and proxy identity semantics.
This change explicitly rejects such argument types and raises a
TypeErrorwith a clear explanation and suggested alternatives.Here is an example of previously confusing behavior:
Output:
Here, it was completely not obvious that changing
p2is changing thep1pointer.