Open maximusron opened 1 year ago
The mapping should be even more general: any implicit cast should be allowed. Baidyanath started this, but then other distractions popped up (conferences, a paper, and more work on InterOp). Pull requests are here:
https://github.com/wlav/CPyCppyy/pull/4 https://github.com/wlav/cppyy-backend/pull/5
More generally, the work done to provide cppyy on top of libInterOp instead of on top of Cling will simplify the above as it won't be based on string comparisons, but decl comparisons. I.e., the implementation provided by Baidyanath may be obsolete soon, but the general approach of finding compatible types (as opposed to iterating all possible types and typedefs) is the way to go.
Does cppyy support C++ template functions?
Does cppyy support C++ template functions?
Yes.
I stumbled upon an odd bug that led me to try and fix the inherently flawed type mapping as well as overload call for multiple arguments in numba_ext.py
The cpp2numba dictionary is perfect since it does exhibit a one-to-one mapping for cpp types and numba types. While doing traces for adding support for const char* arguments, I realized that the primary problem
I though this shortcoming only affected this odd case where I can't add a duplicate nb_type for const char*.
However while continuing to work on trying to figure out ideas to improve the type resolution for the TemplateProxy object, I received a long long signature mismatch error despite using an int64_t type in my C++ definition.
Running this function:
Would give me the following errors:
This is similar to the error obtained by @sudo-panda in this issue https://github.com/wlav/cppyy/issues/84 (have not examined the Literal case yet though)
I realized that despite having an apparently clean mapping(that is expected to handle such a trivial function) for int64_t in cpp2numba: https://github.com/wlav/cppyy/blob/340e8330579f528f8f8e31de29a0ba51cf4d3d9e/python/cppyy/numba_ext.py#L51 the nb_types are not unique as they would seem. On resolving nb_types., it turns out this is the actual mapping:
*_nb_types. <-> cpp type_**
Where we can observe the repetition of
int64
multiple times. In this case three different nb_types.* are mapped to the same thing:This means the actual value in _numba2cpp actually depends on the order in which the cpp2numba maps to an equivalent nb_types.* type, which just should not happen.
In this case, final _numba2cpp dictionary being generated actually has the last mapping to a nb_types,* that resolves to a int64: https://github.com/wlav/cppyy/blob/340e8330579f528f8f8e31de29a0ba51cf4d3d9e/python/cppyy/numba_ext.py#L55 overwrites the previous mappings, and ends up being the only cpp type that the numba inferred type (int64) is mapped to.
My solution was to modify the
_numba2cpp
dictionary to contain a one-to-many mapping:_numba2cpp:
Since the signature has to match one of the signatures from
CPyCppyy::CPPOverload::FindOverload
while calling the__overload__
method of thecppyy.CppOverload
object(orcppyy.TemplateProxy
) while creating the instance of theCppFunctionNumbaType
, I generated a set of possible combinations that would replace theol = CppFunctionNumbaType(self._func.__overload__(*(numba2cpp(x) for x in args)), self._is_method)
call.I did run into some issues that exposed the format of the current pass to overload being supported for only a single argument.
For CPyCppyy to obtain the PyString in a way that matches the PyText as a string from the
GetSignature
method when the method proxy overload is called in CPPOverload.cxx, the following formatting was done:signature = ", ".join(arg_combo)
This prevented the errors of this type:
__overload__() takes at most 2 arguments (3 given)
The result is this:
This now allows the support of functions with such arguments, as well as extends it to work with n number of possible argument sets like so:
which provides this set matching, where the correct C++ signature is in the last line of this block:
And even:
with output:
note: the earlier outputs did not break out of the loop while testing
ol = CppFunctionNumbaType(self._func.__overload__(signature), self._is_method)
, the above output breaks out once the CPyCppyy signature match is found.This even adds support for multi argument template functions that did not work earlier, since after the initial CppFunctionNumbaType call with the TemplateProxy object, the call is the same with the CPPOverload object.
Function:
which runs perfectly. I am not entirely sure if I am making any silly mistakes while going down this rabbit hole but I was motivated by the fact that these functions(especially the template one) are running successfully. would love to know how to proceed ^_^