move throwing Python variable errors to PythonImpl class and change API accordingly.

This commit is contained in:
Axel Kohlmeyer
2025-06-06 00:19:33 -04:00
parent a246619ecf
commit e1aa3cf7ec
5 changed files with 44 additions and 97 deletions

View File

@ -959,31 +959,10 @@ void Variable::python_command(int narg, char **arg)
int Variable::equalstyle(int ivar)
{
if (style[ivar] == EQUAL || style[ivar] == TIMER ||
style[ivar] == INTERNAL) return 1;
if (style[ivar] == PYTHON) {
pyindex[ivar] = python->function_match(data[ivar][0],names[ivar],1);
if (pyindex[ivar] < 0) {
int ierror = pyindex[ivar];
if (ierror == -1) {
error->all(FLERR, "Python function {} specified by variable {} not found",
data[ivar][0], names[ivar]);
} else if (ierror == -2) {
error->all(FLERR, "Python function {} for variable {} does not return a value",
data[ivar][0], names[ivar]);
} else if (ierror == -3) {
error->all(FLERR, "Python function {} and variable {} do not not link to each other",
data[ivar][0], names[ivar]);
} else if (ierror == -4) {
error->all(FLERR, "Python function {} for variable {} returns a string",
data[ivar][0], names[ivar]);
} else {
error->all(FLERR, "Unknown error linking Python function {} to variable {}",
data[ivar][0],names[ivar]);
}
return 0;
} else return 1;
}
if (style[ivar] == EQUAL || style[ivar] == TIMER || style[ivar] == INTERNAL) return 1;
if ((style[ivar] == PYTHON) && (python->function_match(data[ivar][0], names[ivar], 1, error) >= 0))
return 1;
return 0;
}
@ -1103,22 +1082,7 @@ char *Variable::retrieve(const char *name)
str = data[ivar][1] = utils::strdup(result);
} else if (style[ivar] == PYTHON) {
int ifunc = python->function_match(data[ivar][0],name,0);
if (ifunc < 0) {
if (ifunc == -1) {
error->all(FLERR, "Python function {} specified by variable {} not found",
data[ivar][0], name);
} else if (ifunc == -2) {
error->all(FLERR, "Python function {} for variable {} does not return a value",
data[ivar][0], name);
} else if (ifunc == -3) {
error->all(FLERR, "Python function {} and variable {} do not not link to each other",
data[ivar][0], name);
} else {
error->all(FLERR, "Unknown error linking Python function {} to variable {}",
data[ivar][0],name);
}
}
int ifunc = python->function_match(data[ivar][0],name,0,error);
python->invoke_function(ifunc,data[ivar][1],nullptr);
str = data[ivar][1];
@ -4208,32 +4172,7 @@ int Variable::math_function(char *word, char *contents, Tree **tree, Tree **tree
// jvars = returned indices of narg internal variables used by Python function
int *jvars = new int[narg];
pyindex[pyvar] = python->wrapper_match(data[pyvar][0],names[pyvar],narg,jvars);
if (pyindex[pyvar] < 0) {
int ierror = pyindex[pyvar];
if (ierror == -1) {
error->all(FLERR, "Python function {} specified by variable {} not found",
data[ivar][0], names[ivar]);
} else if (ierror == -2) {
error->all(FLERR, "Python function {} for variable {} does not return a value",
data[ivar][0], names[ivar]);
} else if (ierror == -3) {
error->all(FLERR, "Python function {} and variable {} do not not link to each other",
data[ivar][0], names[ivar]);
} else if (ierror == -4) {
error->all(FLERR, "Python function {} for variable {} returns a string",
data[ivar][0], names[ivar]);
} else if (ierror == -5) {
error->all(FLERR, "Python function {} does not use {} internal variable args",
data[ivar][0], narg);
} else if (ierror == -6) {
error->all(FLERR,"Python function {} cannot find an internal variable",
data[ivar][0]);
} else {
error->all(FLERR, "Unknown error linking Python function {} to variable {}",
data[ivar][0],names[ivar]);
}
}
pyindex[pyvar] = python->wrapper_match(data[pyvar][0],names[pyvar],narg,jvars,error);
// if tree: store python variable and arg info in tree for later eval
// else: one-time eval of python-coded function now via python variable