diff options
author | Jose <pepone@users.noreply.github.com> | 2022-06-27 10:19:44 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-27 10:19:44 +0200 |
commit | e4dca525245d50421a887135f61d004aec78a4ab (patch) | |
tree | 6f5bb5baeca74fc36b61e04ab19e48ff4a2bc7af /cpp/src | |
parent | Allow to build Python extension with recent Visual Studio versions (#1367) (diff) | |
download | ice-e4dca525245d50421a887135f61d004aec78a4ab.tar.bz2 ice-e4dca525245d50421a887135f61d004aec78a4ab.tar.xz ice-e4dca525245d50421a887135f61d004aec78a4ab.zip |
Set default value for python optional parameters (#1365)
Diffstat (limited to 'cpp/src')
-rw-r--r-- | cpp/src/Slice/PythonUtil.cpp | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/cpp/src/Slice/PythonUtil.cpp b/cpp/src/Slice/PythonUtil.cpp index ad40a0e34c7..ce4ff6dfc70 100644 --- a/cpp/src/Slice/PythonUtil.cpp +++ b/cpp/src/Slice/PythonUtil.cpp @@ -805,7 +805,20 @@ Slice::Python::CodeVisitor::visitClassDefStart(const ClassDefPtr& p) TypePtr ret = (*oli)->returnType(); ParamDeclList paramList = (*oli)->parameters(); string inParams; + string inParamsDecl; + // Find the last required parameter, all optional parameters after the last required parameter will use + // Ice.Unset as the default. + ParamDeclPtr lastRequiredParameter; + for(ParamDeclList::const_iterator q = paramList.begin(); q != paramList.end(); ++q) + { + if(!(*q)->isOutParam() && !(*q)->optional()) + { + lastRequiredParameter = *q; + } + } + + bool afterLastRequiredParameter = lastRequiredParameter == ICE_NULLPTR; for(ParamDeclList::const_iterator q = paramList.begin(); q != paramList.end(); ++q) { if(!(*q)->isOutParam()) @@ -813,17 +826,29 @@ Slice::Python::CodeVisitor::visitClassDefStart(const ClassDefPtr& p) if(!inParams.empty()) { inParams.append(", "); + inParamsDecl.append(", "); + } + string param = fixIdent((*q)->name()); + inParams.append(param); + if(afterLastRequiredParameter) + { + param += "=Ice.Unset"; + } + inParamsDecl.append(param); + + if(*q == lastRequiredParameter) + { + afterLastRequiredParameter = true; } - inParams.append(fixIdent((*q)->name())); } } _out << sp; writeDocstring(*oli, DocSync, false); _out << nl << "def " << fixedOpName << "(self"; - if(!inParams.empty()) + if(!inParamsDecl.empty()) { - _out << ", " << inParams; + _out << ", " << inParamsDecl; } const string contextParamName = getEscapedParamName(*oli, "context"); _out << ", " << contextParamName << "=None):"; |