From 7a5a2b9f3dd289f1f1731c97c836685f6c619b17 Mon Sep 17 00:00:00 2001 From: Jonas Rembser Date: Wed, 21 Jan 2026 13:21:49 +0100 Subject: [PATCH] [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] https://github.com/wlav/cppyy/commit/62a97c9418c647ec82a5ce81496a90fe997e7c5f --- bindings/pyroot/cppyy/cppyy/python/cppyy/__init__.py | 9 ++++++++- bindings/pyroot/cppyy/cppyy/test/test_cpp11features.py | 2 -- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/bindings/pyroot/cppyy/cppyy/python/cppyy/__init__.py b/bindings/pyroot/cppyy/cppyy/python/cppyy/__init__.py index c8dd458bfdd70..74876a6aed8c7 100644 --- a/bindings/pyroot/cppyy/cppyy/python/cppyy/__init__.py +++ b/bindings/pyroot/cppyy/cppyy/python/cppyy/__init__.py @@ -153,12 +153,19 @@ def __init__(self, ptrcls, maker): def __call__(self, ptr): return py_make_smartptr(type(ptr), self.ptrcls)(ptr) def __getitem__(self, cls): + # For the builtin types that directly map to C++ types, we get the name + # immediately so we don't reach the fallback in the end. + # See also Utility::ConstructTemplateArgs() in CPyCpyy. + if cls is int: + cls = "int" + elif cls is float: + cls = "float" try: if not cls.__module__ == int.__module__: return py_make_smartptr(cls, self.ptrcls) except AttributeError: pass - if isinstance(cls, str) and not cls in ('int', 'float'): + if isinstance(cls, str): return py_make_smartptr(getattr(gbl, cls), self.ptrcls) return self.maker[cls] diff --git a/bindings/pyroot/cppyy/cppyy/test/test_cpp11features.py b/bindings/pyroot/cppyy/cppyy/test/test_cpp11features.py index 71c30153dfe02..e3ca315d3034c 100644 --- a/bindings/pyroot/cppyy/cppyy/test/test_cpp11features.py +++ b/bindings/pyroot/cppyy/cppyy/test/test_cpp11features.py @@ -438,7 +438,6 @@ def test14_shared_ptr_passing(self): gc.collect() assert TestSmartPtr.s_counter == 0 - @mark.xfail(strict=True, condition=IS_WINDOWS | IS_MAC_ARM, reason='ValueError: Could not find "make_unique"') def test15_unique_ptr_template_deduction(self): """Argument type deduction with std::unique_ptr""" @@ -458,7 +457,6 @@ def test15_unique_ptr_template_deduction(self): with raises(ValueError): # not an RValue cppyy.gbl.UniqueTempl.returnptr[int](uptr_in) - @mark.xfail(strict=True, condition=IS_WINDOWS | IS_MAC_ARM, reason='TypeError: Could not find "make_unique"') def test16_unique_ptr_moves(self): """std::unique_ptr requires moves"""