aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken2
diff options
context:
space:
mode:
authorCristián Maureira-Fredes <cristian.maureira-fredes@qt.io>2019-05-29 11:22:44 +0200
committerCristian Maureira-Fredes <cristian.maureira-fredes@qt.io>2019-07-17 10:47:31 +0200
commit78dad8180d797a647645b74255bfc29c46d7264a (patch)
treefa35a636fb31d0e0b56bb3acdbd56f123526a179 /sources/shiboken2
parent4905e2e2feacd346c2f781aa817b3d13ace3411d (diff)
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 <qt_ci_bot@qt-project.org> Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'sources/shiboken2')
-rw-r--r--sources/shiboken2/generator/shiboken2/cppgenerator.cpp10
1 files changed, 7 insertions, 3 deletions
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;