From 78dad8180d797a647645b74255bfc29c46d7264a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cristi=C3=A1n=20Maureira-Fredes?= Date: Wed, 29 May 2019 11:22:44 +0200 Subject: Add optional support for types in QSettings::value This add an optional named parameter to the function value() to automatically cast the type that is being returned by the function. An example of this situation could be an ini file that contains the value of a one-element list: settings.setValue('var', ['a']) The the ini file will be: [General] var=a # we cannot know that this is a list! Once we read it, we could specify if we want the default behavior, a str, or to cast the output to a list. settings.value('var') # Will get "a" settings.value('var', type=list) # Will get ["a"] The cppgenerator was modified to add a verification step before trying to get the named parameter, since it could be optional and having one named parameter was assumming that all of them were provided. Change-Id: I8f379debea86b42cf89019d432e990084c9e6614 Fixes: PYSIDE-1010 Reviewed-by: Qt CI Bot Reviewed-by: Christian Tismer --- sources/shiboken2/generator/shiboken2/cppgenerator.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'sources/shiboken2') diff --git a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp index 84f0cd1f5..e36b6edc3 100644 --- a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp +++ b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp @@ -3032,18 +3032,21 @@ void CppGenerator::writeNamedArgumentResolution(QTextStream &s, const AbstractMe s << INDENT << "if (kwds) {" << endl; { Indentation indent(INDENT); - s << INDENT << "PyObject *"; + s << INDENT << "PyObject *keyName = nullptr;" << endl; + s << INDENT << "PyObject *value = nullptr;" << endl; for (const AbstractMetaArgument *arg : args) { int pyArgIndex = arg->argumentIndex() - OverloadData::numberOfRemovedArguments(func, arg->argumentIndex()); QString pyArgName = usePyArgs ? pythonArgsAt(pyArgIndex) : QLatin1String(PYTHON_ARG); - s << "value = PyDict_GetItemString(kwds, \"" << arg->name() << "\");" << endl; + s << INDENT << "keyName = Py_BuildValue(\"s\",\"" << arg->name() << "\");" << endl; + s << INDENT << "if (PyDict_Contains(kwds, keyName)) {" << endl; + s << INDENT << "value = PyDict_GetItemString(kwds, \"" << arg->name() << "\");" << endl; s << INDENT << "if (value && " << pyArgName << ") {" << endl; { Indentation indent(INDENT); s << INDENT << pyErrString.arg(arg->name()) << endl; s << INDENT << returnStatement(m_currentErrorCode) << endl; } - s << INDENT << "} else if (value) {" << endl; + s << INDENT << INDENT << "} else if (value) {" << endl; { Indentation indent(INDENT); s << INDENT << pyArgName << " = value;" << endl; @@ -3058,6 +3061,7 @@ void CppGenerator::writeNamedArgumentResolution(QTextStream &s, const AbstractMe s << INDENT << '}' << endl; if (arg != args.constLast()) s << INDENT; + s << "}" << endl; } } s << INDENT << '}' << endl; -- cgit v1.2.3