Skip to content

Conversation

@guitargeek
Copy link
Contributor

In case the Utility::ConstructTemplateArgs() method fails to create the C++ template names, it sets a useful Python exception that is unfortunately not propagated, because the calling code in TemplateProxy.cxx is ignoring it. This is fixed in this commit.

The place where the error is set is also moved from Utility::ConstructTemplateArgs() to the actual implementation: AddTypeName(). This means the error is set by the function that actually has the context of the error, which is conceptually cleaner and will allow us also to set more detailed errors for different failure modes of AddTypeName().

Demo code:

import cppyy

cppyy.cppdef("""

template<class T>
class Foo {};

template<class T, class Y=void>
auto make_foo () { return Foo<T>{}; }

template<class T, class Y=void>
auto make_foo (T x) { return Foo<T>{}; }

""")

cppyy.gbl.make_foo[set(), list()]()

Before:

    cppyy.gbl.make_foo[set(), list()]()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
TypeError: Could not find "make_foo" (set cppyy.set_debug() for C++ errors):
  Template method resolution failed:
  Failed to instantiate "make_foo()"
  Failed to instantiate "make_foo()"

After:

    cppyy.gbl.make_foo[set(), list()]()
    ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
TypeError: could not construct C++ name from template argument set()

Note: this work is orthogonal to the rebase of cppyy on CppInterOp.

Copy link
Member

@vepadulano vepadulano left a comment

Choose a reason for hiding this comment

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

I think the changes are good, but could you also add a test with an example of the error being raised?

In case the `Utility::ConstructTemplateArgs()` method fails to create
the C++ template names, it sets a useful Python exception that is
unfortunately not propagated, because the calling code in
`TemplateProxy.cxx` is ignoring it. This is fixed in this commit.

The place where the error is set is also moved from
`Utility::ConstructTemplateArgs()` to the actual implementation:
`AddTypeName()`. This means the error is set by the function that
actually has the context of the error, which is conceptually cleaner and
will allow us also to set more detailed errors for different failure
modes of `AddTypeName()`.

A unit test is also implemented.

Demo code:
```python
import cppyy

cppyy.cppdef("""

template<class T>
class Foo {};

template<class T, class Y=void>
auto make_foo () { return Foo<T>{}; }

template<class T, class Y=void>
auto make_foo (T x) { return Foo<T>{}; }

""")

cppyy.gbl.make_foo[set(), list()]()

```

Before:
```
    cppyy.gbl.make_foo[set(), list()]()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
TypeError: Could not find "make_foo" (set cppyy.set_debug() for C++ errors):
  Template method resolution failed:
  Failed to instantiate "make_foo()"
  Failed to instantiate "make_foo()"
```

After:
```
    cppyy.gbl.make_foo[set(), list()]()
    ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
TypeError: could not construct C++ name from template argument set()
```

Note: this work is orthogonal to the rebase of `cppyy` on CppInterOp.
Copy link
Member

@vepadulano vepadulano left a comment

Choose a reason for hiding this comment

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

LGTM, feel free to merge once the CI is green

@guitargeek guitargeek merged commit f687d1d into root-project:master Jan 21, 2026
26 of 29 checks passed
@guitargeek guitargeek deleted the template_name branch January 21, 2026 13:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants