Skip to content

Commit 47bfa16

Browse files
sudo-pandavgvassilev
authored andcommitted
Add type info in the error msg of func arg conv
1 parent 4264b77 commit 47bfa16

File tree

4 files changed

+54
-15
lines changed

4 files changed

+54
-15
lines changed

src/CPPMethod.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ bool CPyCppyy::CPPMethod::ConvertAndSetArgs(CPyCppyy_PyArgs_t args, size_t nargs
875875
Parameter* cppArgs = ctxt->GetArgs(argc);
876876
for (int i = 0; i < (int)argc; ++i) {
877877
if (!fConverters[i]->SetArg(CPyCppyy_PyArgs_GET_ITEM(args, i), cppArgs[i], ctxt)) {
878-
SetPyError_(CPyCppyy_PyText_FromFormat("could not convert argument %d", i+1));
878+
SetPyError_(CPyCppyy_PyText_FromFormat("could not convert argument %d: %s", i+1, fConverters[i]->GetFailureMsg().c_str()));
879879
isOK = false;
880880
break;
881881
}

src/Converters.cxx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2355,8 +2355,8 @@ bool CPyCppyy::VoidPtrRefConverter::SetArg(
23552355
}
23562356

23572357
//----------------------------------------------------------------------------
2358-
CPyCppyy::VoidPtrPtrConverter::VoidPtrPtrConverter(cdims_t dims) :
2359-
fShape(dims) {
2358+
CPyCppyy::VoidPtrPtrConverter::VoidPtrPtrConverter(cdims_t dims, const std::string &failureMsg) :
2359+
fShape(dims), fFailureMsg (failureMsg) {
23602360
fIsFixed = dims ? fShape[0] != UNKNOWN_SIZE : false;
23612361
}
23622362

@@ -3144,14 +3144,15 @@ CPyCppyy::Converter* CPyCppyy::CreateConverter(const std::string& fullType, cdim
31443144
resolvedType.substr(0, pos1), resolvedType.substr(pos1+sm.length(), pos2-1));
31453145
}
31463146
}
3147+
const std::string failure_msg("Failed to convert type: " + fullType + "; resolved: " + resolvedType + "; real: " + realType + "; cpd: " + cpd);
31473148

31483149
if (!result && cpd == "&&") {
31493150
// for builtin, can use const-ref for r-ref
31503151
h = gConvFactories.find("const " + realType + "&");
31513152
if (h != gConvFactories.end())
31523153
return (h->second)(dims);
31533154
// else, unhandled moves
3154-
result = new NotImplementedConverter();
3155+
result = new NotImplementedConverter(failure_msg);
31553156
}
31563157

31573158
if (!result && h != gConvFactories.end())
@@ -3160,11 +3161,11 @@ CPyCppyy::Converter* CPyCppyy::CreateConverter(const std::string& fullType, cdim
31603161
else if (!result) {
31613162
// default to something reasonable, assuming "user knows best"
31623163
if (cpd.size() == 2 && cpd != "&&") // "**", "*[]", "*&"
3163-
result = new VoidPtrPtrConverter(dims.ndim());
3164+
result = new VoidPtrPtrConverter(dims.ndim(), failure_msg);
31643165
else if (!cpd.empty())
3165-
result = new VoidArrayConverter(); // "user knows best"
3166+
result = new VoidArrayConverter(/* keepControl= */ true, failure_msg); // "user knows best"
31663167
else
3167-
result = new NotImplementedConverter(); // fails on use
3168+
result = new NotImplementedConverter(failure_msg); // fails on use
31683169
}
31693170

31703171
return result;
@@ -3346,6 +3347,7 @@ CPyCppyy::Converter* CPyCppyy::CreateConverter(Cppyy::TCppType_t type, cdims_t d
33463347
result = new FunctionPointerConverter(
33473348
resolvedTypeStr.substr(0, pos1), resolvedTypeStr.substr(pos2+2, pos3-pos2-1));
33483349
}
3350+
const std::string failure_msg("Failed to convert type: " + fullType + "; resolved: " + resolvedTypeStr + "; real: " + realTypeStr + "; realUnresolvedType: " + realUnresolvedTypeStr + "; cpd: " + cpd);
33493351

33503352
if (!result && cpd == "&&") {
33513353
// for builtin, can use const-ref for r-ref
@@ -3357,7 +3359,7 @@ CPyCppyy::Converter* CPyCppyy::CreateConverter(Cppyy::TCppType_t type, cdims_t d
33573359
if (h != gConvFactories.end())
33583360
return (h->second)(dims);
33593361
// else, unhandled moves
3360-
result = new NotImplementedConverter();
3362+
result = new NotImplementedConverter(failure_msg);
33613363
}
33623364

33633365
if (!result && h != gConvFactories.end()) {
@@ -3366,11 +3368,11 @@ CPyCppyy::Converter* CPyCppyy::CreateConverter(Cppyy::TCppType_t type, cdims_t d
33663368
} else if (!result) {
33673369
// default to something reasonable, assuming "user knows best"
33683370
if (cpd.size() == 2 && cpd != "&&") {// "**", "*[]", "*&"
3369-
result = new VoidPtrPtrConverter(dims.ndim());
3371+
result = new VoidPtrPtrConverter(dims.ndim(), failure_msg);
33703372
} else if (!cpd.empty()) {
3371-
result = new VoidArrayConverter(); // "user knows best"
3373+
result = new VoidArrayConverter(/* keepControl= */ true, failure_msg); // "user knows best"
33723374
} else {
3373-
result = new NotImplementedConverter(); // fails on use
3375+
result = new NotImplementedConverter(failure_msg); // fails on use
33743376
}
33753377
}
33763378

src/Converters.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class CPYCPPYY_CLASS_EXPORT Converter {
2222
virtual PyObject* FromMemory(void* address);
2323
virtual bool ToMemory(PyObject* value, void* address, PyObject* ctxt = nullptr);
2424
virtual bool HasState() { return false; }
25+
virtual std::string GetFailureMsg() { return "[Converter]"; }
2526
};
2627

2728
// create/destroy converter from fully qualified type (public API)
@@ -36,17 +37,20 @@ CPYCPPYY_EXPORT bool UnregisterConverter(const std::string& name);
3637
// converters for special cases (only here b/c of external use of StrictInstancePtrConverter)
3738
class VoidArrayConverter : public Converter {
3839
public:
39-
VoidArrayConverter(bool keepControl = true) { fKeepControl = keepControl; }
40+
VoidArrayConverter(bool keepControl = true, const std::string &failureMsg = std::string())
41+
: fFailureMsg(failureMsg) { fKeepControl = keepControl; }
4042

4143
public:
4244
virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
4345
virtual PyObject* FromMemory(void* address);
4446
virtual bool ToMemory(PyObject* value, void* address, PyObject* ctxt = nullptr);
4547
virtual bool HasState() { return true; }
48+
virtual std::string GetFailureMsg() { return "[VoidArrayConverter] " + fFailureMsg; }
4649

4750
protected:
4851
virtual bool GetAddressSpecialCase(PyObject* pyobject, void*& address);
4952
bool KeepControl() { return fKeepControl; }
53+
const std::string fFailureMsg;
5054

5155
private:
5256
bool fKeepControl;
@@ -55,8 +59,8 @@ class VoidArrayConverter : public Converter {
5559
template <bool ISCONST>
5660
class InstancePtrConverter : public VoidArrayConverter {
5761
public:
58-
InstancePtrConverter(Cppyy::TCppType_t klass, bool keepControl = false) :
59-
VoidArrayConverter(keepControl), fClass(klass) {}
62+
InstancePtrConverter(Cppyy::TCppType_t klass, bool keepControl = false, const std::string &failureMsg = std::string()) :
63+
VoidArrayConverter(keepControl, failureMsg), fClass(klass) {}
6064

6165
public:
6266
virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);

src/DeclareConverters.h

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ public: \
2020
virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr); \
2121
virtual PyObject* FromMemory(void*); \
2222
virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr); \
23+
virtual std::string GetFailureMsg() { return "[" #name "Converter]"; } \
2324
}; \
2425
\
2526
class Const##name##RefConverter : public Converter { \
2627
public: \
2728
virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr); \
2829
virtual PyObject* FromMemory(void*); \
30+
virtual std::string GetFailureMsg() { return "[Const" #name "RefConverter]"; }\
2931
}
3032

3133

@@ -34,19 +36,22 @@ class name##Converter : public base##Converter { \
3436
public: \
3537
virtual PyObject* FromMemory(void*); \
3638
virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr); \
39+
virtual std::string GetFailureMsg() { return "[" #name "Converter]"; } \
3740
}; \
3841
\
3942
class Const##name##RefConverter : public Converter { \
4043
public: \
4144
virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr); \
4245
virtual PyObject* FromMemory(void*); \
46+
virtual std::string GetFailureMsg() { return "[Const" #name "RefConverter]"; }\
4347
}
4448

4549
#define CPPYY_DECLARE_REFCONVERTER(name) \
4650
class name##RefConverter : public Converter { \
4751
public: \
4852
virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr); \
4953
virtual PyObject* FromMemory(void*); \
54+
virtual std::string GetFailureMsg() { return "[" #name "RefConverter]"; }\
5055
};
5156

5257
#define CPPYY_DECLARE_ARRAY_CONVERTER(name) \
@@ -59,6 +64,7 @@ public: \
5964
virtual PyObject* FromMemory(void*); \
6065
virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr); \
6166
virtual bool HasState() { return true; } \
67+
virtual std::string GetFailureMsg() { return "[" #name "ArrayConverter]"; }\
6268
protected: \
6369
dims_t fShape; \
6470
bool fIsFixed; \
@@ -126,6 +132,7 @@ class CStringConverter : public Converter {
126132
virtual PyObject* FromMemory(void* address);
127133
virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
128134
virtual bool HasState() { return true; }
135+
virtual std::string GetFailureMsg() { return "[CStringConverter]"; }
129136

130137
protected:
131138
std::string fBuffer;
@@ -139,6 +146,7 @@ class NonConstCStringConverter : public CStringConverter {
139146
public:
140147
virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
141148
virtual PyObject* FromMemory(void* address);
149+
virtual std::string GetFailureMsg() { return "[NonConstCStringConverter]"; }
142150
};
143151

144152
class WCStringConverter : public Converter {
@@ -154,6 +162,7 @@ class WCStringConverter : public Converter {
154162
virtual PyObject* FromMemory(void* address);
155163
virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
156164
virtual bool HasState() { return true; }
165+
virtual std::string GetFailureMsg() { return "[WCStringConverter]"; };
157166

158167
protected:
159168
wchar_t* fBuffer;
@@ -173,6 +182,7 @@ class CString16Converter : public Converter {
173182
virtual PyObject* FromMemory(void* address);
174183
virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
175184
virtual bool HasState() { return true; }
185+
virtual std::string GetFailureMsg() { return "[CString16Converter]"; };
176186

177187
protected:
178188
char16_t* fBuffer;
@@ -192,6 +202,7 @@ class CString32Converter : public Converter {
192202
virtual PyObject* FromMemory(void* address);
193203
virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
194204
virtual bool HasState() { return true; }
205+
virtual std::string GetFailureMsg() { return "[CString32Converter]"; };
195206

196207
protected:
197208
char32_t* fBuffer;
@@ -224,6 +235,7 @@ class CStringArrayConverter : public SCharArrayConverter {
224235
using SCharArrayConverter::SCharArrayConverter;
225236
virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
226237
virtual PyObject* FromMemory(void* address);
238+
virtual std::string GetFailureMsg() { return "[CStringArrayConverter]"; };
227239

228240
private:
229241
std::vector<const char*> fBuffer;
@@ -233,6 +245,7 @@ class NonConstCStringArrayConverter : public CStringArrayConverter {
233245
public:
234246
using CStringArrayConverter::CStringArrayConverter;
235247
virtual PyObject* FromMemory(void* address);
248+
virtual std::string GetFailureMsg() { return "[NonConstCStringArrayConverter]"; };
236249
};
237250

238251
// converters for special cases
@@ -247,6 +260,7 @@ class InstanceConverter : public StrictInstancePtrConverter {
247260
virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
248261
virtual PyObject* FromMemory(void*);
249262
virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr);
263+
virtual std::string GetFailureMsg() { return "[InstanceConverter]"; };
250264
};
251265

252266
class InstanceRefConverter : public Converter {
@@ -258,6 +272,7 @@ class InstanceRefConverter : public Converter {
258272
virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
259273
virtual PyObject* FromMemory(void* address);
260274
virtual bool HasState() { return true; }
275+
virtual std::string GetFailureMsg() { return "[InstanceRefConverter]"; };
261276

262277
protected:
263278
Cppyy::TCppType_t fClass;
@@ -268,6 +283,7 @@ class InstanceMoveConverter : public InstanceRefConverter {
268283
public:
269284
InstanceMoveConverter(Cppyy::TCppType_t klass) : InstanceRefConverter(klass, true) {}
270285
virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
286+
virtual std::string GetFailureMsg() { return "[InstanceMoveConverter]"; };
271287
};
272288

273289
template <bool ISREFERENCE>
@@ -279,6 +295,7 @@ class InstancePtrPtrConverter : public InstancePtrConverter<false> {
279295
virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
280296
virtual PyObject* FromMemory(void* address);
281297
virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
298+
virtual std::string GetFailureMsg() { return "[InstancePtrPtrConverter]"; };
282299
};
283300

284301
class InstanceArrayConverter : public InstancePtrConverter<false> {
@@ -292,6 +309,7 @@ class InstanceArrayConverter : public InstancePtrConverter<false> {
292309
virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
293310
virtual PyObject* FromMemory(void* address);
294311
virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
312+
virtual std::string GetFailureMsg() { return "[InstanceArrayConverter]"; };
295313

296314
protected:
297315
dims_t fShape;
@@ -307,6 +325,7 @@ class ComplexDConverter: public InstanceConverter {
307325
virtual PyObject* FromMemory(void* address);
308326
virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
309327
virtual bool HasState() { return true; }
328+
virtual std::string GetFailureMsg() { return "[ComplexDConverter]"; };
310329

311330
private:
312331
std::complex<double> fBuffer;
@@ -318,25 +337,29 @@ class ComplexDConverter: public InstanceConverter {
318337
class STLIteratorConverter : public Converter {
319338
public:
320339
virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
340+
virtual std::string GetFailureMsg() { return "[STLIteratorConverter]"; };
321341
};
322342
// -- END CLING WORKAROUND
323343

324344

325345
class VoidPtrRefConverter : public Converter {
326346
public:
327347
virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
348+
virtual std::string GetFailureMsg() { return "[VoidPtrRefConverter]"; };
328349
};
329350

330351
class VoidPtrPtrConverter : public Converter {
331352
public:
332-
VoidPtrPtrConverter(cdims_t dims);
353+
VoidPtrPtrConverter(cdims_t dims, const std::string &failureMsg = std::string());
333354
virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
334355
virtual PyObject* FromMemory(void* address);
335356
virtual bool HasState() { return true; }
357+
virtual std::string GetFailureMsg() { return "[VoidPtrPtrConverter] " + fFailureMsg; }
336358

337359
protected:
338360
dims_t fShape;
339361
bool fIsFixed;
362+
const std::string fFailureMsg;
340363
};
341364

342365
CPPYY_DECLARE_BASIC_CONVERTER(PyObject);
@@ -351,6 +374,7 @@ public: \
351374
virtual PyObject* FromMemory(void* address); \
352375
virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr); \
353376
virtual bool HasState() { return true; } \
377+
virtual std::string GetFailureMsg() { return "[" #name "Converter]"; }; \
354378
protected: \
355379
strtype fBuffer; \
356380
}
@@ -371,6 +395,7 @@ class STLStringMoveConverter : public STLStringConverter {
371395

372396
public:
373397
virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
398+
virtual std::string GetFailureMsg() { return "[STLStringMoveConverter]"; };
374399
};
375400

376401

@@ -385,6 +410,7 @@ class FunctionPointerConverter : public Converter {
385410
virtual PyObject* FromMemory(void* address);
386411
virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr);
387412
virtual bool HasState() { return true; }
413+
virtual std::string GetFailureMsg() { return "[FunctionPointerConverter]"; };
388414

389415
protected:
390416
std::string fRetType;
@@ -404,6 +430,7 @@ class StdFunctionConverter : public FunctionPointerConverter {
404430
virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
405431
virtual PyObject* FromMemory(void* address);
406432
virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
433+
virtual std::string GetFailureMsg() { return "[StdFunctionConverter]"; };
407434

408435
protected:
409436
Converter* fConverter;
@@ -425,6 +452,7 @@ class SmartPtrConverter : public Converter {
425452
virtual PyObject* FromMemory(void* address);
426453
//virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr);
427454
virtual bool HasState() { return true; }
455+
virtual std::string GetFailureMsg() { return "[SmartPtrConverter]"; };
428456

429457
protected:
430458
virtual bool GetAddressSpecialCase(PyObject*, void*&) { return false; }
@@ -449,6 +477,7 @@ class InitializerListConverter : public InstanceConverter {
449477
public:
450478
virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
451479
virtual bool HasState() { return true; }
480+
virtual std::string GetFailureMsg() { return "[FunctionPointerConverter]"; };
452481

453482
protected:
454483
void Clear();
@@ -464,7 +493,11 @@ class InitializerListConverter : public InstanceConverter {
464493
// raising converter to take out overloads
465494
class NotImplementedConverter : public Converter {
466495
public:
496+
NotImplementedConverter(const std::string &failureMsg = std::string()) : fFailureMsg{failureMsg} {}
467497
virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
498+
virtual std::string GetFailureMsg() { return "[NotImplementedConverter] " + fFailureMsg; }
499+
protected:
500+
const std::string fFailureMsg;
468501
};
469502

470503
} // unnamed namespace

0 commit comments

Comments
 (0)