Skip to content

Commit 25de749

Browse files
committed
[cppyy] Consider Python int and float for make_unique optimization
The `std::make_unique` function is Pythonized to avoid unnecessary code generation by TClingCallFunc, which might also go wrong (as reported in GitHub issue #19122). There is a fallback to calling the original `make_unique` for builtin types (introduced in [1]), but we don't need that if we know the builtin type can be readily used in the `unique_ptr` constructor, as is the case for `int` and `float`. Hence, this commit suggests to not use the fallback for `int` and `float`, which also closes #19122 because no wrapper code has to be generated anymore. [1] wlav/cppyy@62a97c9
1 parent f687d1d commit 25de749

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

bindings/pyroot/cppyy/cppyy/python/cppyy/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@ def __init__(self, ptrcls, maker):
153153
def __call__(self, ptr):
154154
return py_make_smartptr(type(ptr), self.ptrcls)(ptr)
155155
def __getitem__(self, cls):
156+
# For the builtin types that directly map to C++ types, we get the name
157+
# immediately so we don't reach the fallback in the end.
158+
# See also Utility::ConstructTemplateArgs() in CPyCpyy.
159+
if cls is int:
160+
cls = "int"
161+
elif cls is float:
162+
cls = "float"
156163
try:
157164
if not cls.__module__ == int.__module__:
158165
return py_make_smartptr(cls, self.ptrcls)

bindings/pyroot/cppyy/cppyy/test/test_cpp11features.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,6 @@ def test14_shared_ptr_passing(self):
438438
gc.collect()
439439
assert TestSmartPtr.s_counter == 0
440440

441-
@mark.xfail(strict=True, condition=IS_WINDOWS | IS_MAC_ARM, reason='ValueError: Could not find "make_unique<int>"')
442441
def test15_unique_ptr_template_deduction(self):
443442
"""Argument type deduction with std::unique_ptr"""
444443

@@ -458,7 +457,6 @@ def test15_unique_ptr_template_deduction(self):
458457
with raises(ValueError): # not an RValue
459458
cppyy.gbl.UniqueTempl.returnptr[int](uptr_in)
460459

461-
@mark.xfail(strict=True, condition=IS_WINDOWS | IS_MAC_ARM, reason='TypeError: Could not find "make_unique<int>"')
462460
def test16_unique_ptr_moves(self):
463461
"""std::unique_ptr requires moves"""
464462

0 commit comments

Comments
 (0)