summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrenatofilho <renato.filho@openbossa.org>2010-09-14 18:33:21 -0300
committerrenatofilho <renato.filho@openbossa.org>2010-09-15 10:26:48 -0300
commitaa1b98fa000f90b9905fcb4a1f1eccefd3851105 (patch)
tree8be2043b39e111d2e0cb138949cd34e4f1965fdd
parent58692e4b27957b587a9b8bd5075fbcb0daad7998 (diff)
downloadshiboken-aa1b98fa000f90b9905fcb4a1f1eccefd3851105.tar.gz
shiboken-aa1b98fa000f90b9905fcb4a1f1eccefd3851105.tar.xz
shiboken-aa1b98fa000f90b9905fcb4a1f1eccefd3851105.zip
Generate correct python function definitions.
Reviewer: Hugo Parente Lima <hugo.pl@gmail.com> Luciano Wolf <luciano.wolf@openbossa.org>
-rw-r--r--cppgenerator.cpp32
-rw-r--r--cppgenerator.h3
-rw-r--r--overloaddata.h2
-rw-r--r--shibokengenerator.cpp16
-rw-r--r--shibokengenerator.h5
5 files changed, 32 insertions, 26 deletions
diff --git a/cppgenerator.cpp b/cppgenerator.cpp
index 507ef7f2..f264a1ea 100644
--- a/cppgenerator.cpp
+++ b/cppgenerator.cpp
@@ -473,7 +473,7 @@ void CppGenerator::writeVirtualMethodNative(QTextStream &s, const AbstractMetaFu
if (func->isAbstract()) {
s << INDENT << "PyErr_SetString(PyExc_NotImplementedError, \"pure virtual method '";
s << func->ownerClass()->name() << '.' << func->name();
- s << "()' not implemented.\");" << endl;
+ s << "()' not implemented.\");" << endl;
s << INDENT << "return ";
if (func->type()) {
writeMinimalConstructorCallArguments(s, func->type());
@@ -1230,7 +1230,7 @@ void CppGenerator::writeArgumentsInitializer(QTextStream& s, OverloadData& overl
QStringList palist;
s << INDENT << "PyObject* ";
- if (maxArgs == 1) {
+ if (!pythonFunctionWrapperUsesListOfArguments(overloadData)) {
s << "arg = 0";
palist << "&arg";
} else {
@@ -1343,7 +1343,7 @@ void CppGenerator::writeErrorSection(QTextStream& s, OverloadData& overloadData)
Indentation indentation(INDENT);
QString funcName = fullPythonFunctionName(rfunc);
- QString argsVar = !rfunc->isConstructor() && overloadData.maxArgs() == 1 ? "arg" : "args";
+ QString argsVar = pythonFunctionWrapperUsesListOfArguments(overloadData) ? "args" : "arg";;
if (verboseErrorMessagesDisabled()) {
s << INDENT << "Shiboken::setErrorAboutWrongArguments(" << argsVar << ", \"" << funcName << "\", 0);" << endl;
} else {
@@ -1640,7 +1640,7 @@ void CppGenerator::writeOverloadedFunctionDecisorEngine(QTextStream& s, const Ov
int startArg = od->argPos();
int sequenceArgCount = 0;
while (od && !od->argType()->isVarargs()) {
- if (usePyArgs && maxArgs > 1)
+ if (usePyArgs)
pyArgName = QString("pyargs[%1]").arg(od->argPos());
writeTypeCheck(tck, od, pyArgName);
@@ -1728,7 +1728,7 @@ void CppGenerator::writeSingleFunctionCall(QTextStream& s, const OverloadData& o
const AbstractMetaClass* implementingClass = overloadData.referenceFunction()->implementingClass();
- bool usePyArgs = pythonFunctionWrapperUsesListOfArguments(overloadData) && overloadData.maxArgs() > 1;
+ bool usePyArgs = pythonFunctionWrapperUsesListOfArguments(overloadData);
// Handle named arguments.
writeNamedArgumentResolution(s, func, usePyArgs);
@@ -2317,14 +2317,6 @@ bool CppGenerator::shouldGenerateGetSetList(const AbstractMetaClass* metaClass)
return false;
}
-bool CppGenerator::pythonFunctionWrapperUsesListOfArguments(const OverloadData& overloadData)
-{
- bool usePyArgs = overloadData.maxArgs() > 1
- || overloadData.referenceFunction()->isConstructor()
- || overloadData.hasArgumentWithDefaultValue();
- return usePyArgs;
-}
-
void CppGenerator::writeClassDefinition(QTextStream& s, const AbstractMetaClass* metaClass)
{
QString tp_flags;
@@ -2855,14 +2847,14 @@ void CppGenerator::writeMethodDefinitionEntry(QTextStream& s, const AbstractMeta
OverloadData overloadData(overloads, this);
bool usePyArgs = pythonFunctionWrapperUsesListOfArguments(overloadData);
const AbstractMetaFunction* func = overloadData.referenceFunction();
+ int min = overloadData.minArgs();
+ int max = overloadData.maxArgs();
s << '"' << func->name() << "\", (PyCFunction)" << cpythonFunctionName(func) << ", ";
- if (overloadData.maxArgs() < 2 && !usePyArgs) {
- bool minZero = overloadData.minArgs() == 0;
- bool maxOne = overloadData.maxArgs() == 1;
- if (minZero)
- s << "METH_NOARGS" << (maxOne ? "|" : "");
- if (maxOne)
+ if ((min == max) && (max < 2) && !usePyArgs) {
+ if (max == 0)
+ s << "METH_NOARGS";
+ else
s << "METH_O";
} else {
s << "METH_VARARGS";
@@ -3738,7 +3730,7 @@ bool CppGenerator::writeParentChildManagement(QTextStream& s, const AbstractMeta
if (argOwner.index == -2) //invalid
argOwner = func->argumentOwner(dClass, argIndex);
- bool usePyArgs = OverloadData(getFunctionGroups(func->implementingClass())[func->name()], this).maxArgs() > 1;
+ bool usePyArgs = pythonFunctionWrapperUsesListOfArguments(OverloadData(getFunctionGroups(func->implementingClass())[func->name()], this));
ArgumentOwner::Action action = argOwner.action;
int parentIndex = argOwner.index;
diff --git a/cppgenerator.h b/cppgenerator.h
index 7ce84935..1d2d122c 100644
--- a/cppgenerator.h
+++ b/cppgenerator.h
@@ -186,9 +186,6 @@ private:
/// Returns true if generator should produce getters and setters for the given class.
bool shouldGenerateGetSetList(const AbstractMetaClass* metaClass);
- /// Returns true if the Python wrapper for the received OverloadData must accept a list of arguments.
- static bool pythonFunctionWrapperUsesListOfArguments(const OverloadData& overloadData);
-
void writeHashFunction(QTextStream& s, const AbstractMetaClass* metaClass);
void writeObjCopierFunction(QTextStream& s, const AbstractMetaClass* metaClass);
diff --git a/overloaddata.h b/overloaddata.h
index 3e5d0cc6..a0bd4640 100644
--- a/overloaddata.h
+++ b/overloaddata.h
@@ -28,7 +28,7 @@
#include <QtCore/QList>
#include <QtCore/QBitArray>
-#include "shibokengenerator.h"
+class ShibokenGenerator;
class OverloadData;
typedef QList<OverloadData*> OverloadDataList;
diff --git a/shibokengenerator.cpp b/shibokengenerator.cpp
index b6edd6a3..e7d0b7de 100644
--- a/shibokengenerator.cpp
+++ b/shibokengenerator.cpp
@@ -1087,7 +1087,7 @@ void ShibokenGenerator::writeCodeSnips(QTextStream& s,
argsRemoved++;
}
numArgs = func->arguments().size() - argsRemoved;
- usePyArgs = getMinMaxArguments(func).second > 1;
+ usePyArgs = pythonFunctionWrapperUsesListOfArguments(OverloadData(getFunctionGroups(func->implementingClass())[func->name()], this));
}
foreach (CodeSnip snip, codeSnips) {
@@ -1114,7 +1114,7 @@ void ShibokenGenerator::writeCodeSnips(QTextStream& s,
// replace %PYARG_# variables
code.replace("%PYARG_0", PYTHON_RETURN_VAR);
if (snip.language == TypeSystem::TargetLangCode) {
- if (numArgs > 1) {
+ if (usePyArgs) {
code.replace(pyArgsRegex, "pyargs[\\1-1]");
} else {
static QRegExp pyArgsRegexCheck("%PYARG_([2-9]+)");
@@ -1637,3 +1637,15 @@ bool ShibokenGenerator::verboseErrorMessagesDisabled() const
return m_verboseErrorMessagesDisabled;
}
+
+bool ShibokenGenerator::pythonFunctionWrapperUsesListOfArguments(const OverloadData& overloadData)
+{
+ int maxArgs = overloadData.maxArgs();
+ int minArgs = overloadData.minArgs();
+ bool usePyArgs = (minArgs != maxArgs)
+ || (maxArgs > 1)
+ || overloadData.referenceFunction()->isConstructor()
+ || overloadData.hasArgumentWithDefaultValue();
+ return usePyArgs;
+}
+
diff --git a/shibokengenerator.h b/shibokengenerator.h
index 37f484a9..1a7c5a63 100644
--- a/shibokengenerator.h
+++ b/shibokengenerator.h
@@ -32,6 +32,8 @@
#include <generator.h>
#include <QtCore/QTextStream>
+#include "overloaddata.h"
+
class DocParser;
/**
@@ -346,6 +348,9 @@ protected:
/// Returns all extended conversions for the current module.
ExtendedConverterData getExtendedConverters() const;
+ /// Returns true if the Python wrapper for the received OverloadData must accept a list of arguments.
+ static bool pythonFunctionWrapperUsesListOfArguments(const OverloadData& overloadData);
+
Indentor INDENT;
private:
bool m_useCtorHeuristic;