Commit 00cc21c5 authored by David Gobbi's avatar David Gobbi Committed by Marcus D. Hanwell
Browse files

ENH: Raise a python error if overload resolution is ambiguous.

parent 640aa0d6
...@@ -1595,6 +1595,7 @@ PyObject *PyVTKCallOverloadedMethod( ...@@ -1595,6 +1595,7 @@ PyObject *PyVTKCallOverloadedMethod(
PyMethodDef *methods, PyObject *self, PyObject *args) PyMethodDef *methods, PyObject *self, PyObject *args)
{ {
PyMethodDef *meth = &methods[0]; PyMethodDef *meth = &methods[0];
int matchCount = 1;
// Make sure there is more than one method // Make sure there is more than one method
if (methods[1].ml_meth != 0) if (methods[1].ml_meth != 0)
...@@ -1707,19 +1708,24 @@ PyObject *PyVTKCallOverloadedMethod( ...@@ -1707,19 +1708,24 @@ PyObject *PyVTKCallOverloadedMethod(
} }
// Loop through methods and identify the best match // Loop through methods and identify the best match
int minPenalty = VTK_INT_MAX; int minPenalty = VTK_PYTHON_INCOMPATIBLE;
meth = 0; meth = 0;
matchCount = 0;
for (sig = 0; sig < nsig; sig++) for (sig = 0; sig < nsig; sig++)
{ {
// the "helper->next" check is to ensure that there are no leftover args // the "helper->next" check is to ensure that there are no leftover args
helper = &helperArray[sig]; helper = &helperArray[sig];
int penalty = helper->penalty(); int penalty = helper->penalty();
if (penalty != VTK_PYTHON_INCOMPATIBLE && if (penalty <= minPenalty && penalty < VTK_PYTHON_INCOMPATIBLE &&
penalty < minPenalty &&
!helper->next(&format, &classname)) !helper->next(&format, &classname))
{ {
minPenalty = penalty; if (penalty < minPenalty)
meth = &methods[sig]; {
matchCount = 0;
minPenalty = penalty;
meth = &methods[sig];
}
matchCount++;
} }
} }
...@@ -1730,6 +1736,14 @@ PyObject *PyVTKCallOverloadedMethod( ...@@ -1730,6 +1736,14 @@ PyObject *PyVTKCallOverloadedMethod(
} }
} }
if (meth && matchCount > 1)
{
PyErr_SetString(PyExc_TypeError,
"ambiguous call, multiple overloaded methods match the arguments");
return NULL;
}
if (meth) if (meth)
{ {
return meth->ml_meth(self, args); return meth->ml_meth(self, args);
...@@ -1752,6 +1766,7 @@ PyMethodDef *PyVTKFindConversionMethod( ...@@ -1752,6 +1766,7 @@ PyMethodDef *PyVTKFindConversionMethod(
const char *format, *classname, *dummy1, *dummy2; const char *format, *classname, *dummy1, *dummy2;
int minPenalty = VTK_PYTHON_NEEDS_CONVERSION; int minPenalty = VTK_PYTHON_NEEDS_CONVERSION;
PyMethodDef *method = 0; PyMethodDef *method = 0;
int matchCount = 0;
for (PyMethodDef *meth = methods; meth->ml_meth != NULL; meth++) for (PyMethodDef *meth = methods; meth->ml_meth != NULL; meth++)
{ {
...@@ -1766,12 +1781,19 @@ PyMethodDef *PyVTKFindConversionMethod( ...@@ -1766,12 +1781,19 @@ PyMethodDef *PyVTKFindConversionMethod(
if (penalty < minPenalty) if (penalty < minPenalty)
{ {
matchCount = 1;
minPenalty = penalty; minPenalty = penalty;
method = meth; method = meth;
} }
else if (meth && penalty == minPenalty)
{
matchCount++;
}
} }
} }
// if matchCount > 1, there was ambiguity
return method; return method;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment