aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--cppgenerator.cpp8
-rw-r--r--doc/typesystemvariables.rst6
-rw-r--r--shibokengenerator.cpp19
-rw-r--r--shibokengenerator.h6
-rw-r--r--tests/samplebinding/typesystem_sample.xml2
5 files changed, 33 insertions, 8 deletions
diff --git a/cppgenerator.cpp b/cppgenerator.cpp
index cbb3b8d3c..46391ce3f 100644
--- a/cppgenerator.cpp
+++ b/cppgenerator.cpp
@@ -123,7 +123,7 @@ void CppGenerator::generateClass(QTextStream &s, const AbstractMetaClass *metaCl
// class inject-code native/beginning
if (!metaClass->typeEntry()->codeSnips().isEmpty()) {
- writeCodeSnips(s, metaClass->typeEntry()->codeSnips(), CodeSnip::Beginning, TypeSystem::NativeCode);
+ writeCodeSnips(s, metaClass->typeEntry()->codeSnips(), CodeSnip::Beginning, TypeSystem::NativeCode, 0, metaClass);
s << endl;
}
@@ -260,7 +260,7 @@ void CppGenerator::generateClass(QTextStream &s, const AbstractMetaClass *metaCl
// class inject-code native/end
if (!metaClass->typeEntry()->codeSnips().isEmpty()) {
- writeCodeSnips(s, metaClass->typeEntry()->codeSnips(), CodeSnip::End, TypeSystem::NativeCode);
+ writeCodeSnips(s, metaClass->typeEntry()->codeSnips(), CodeSnip::End, TypeSystem::NativeCode, 0, metaClass);
s << endl;
}
}
@@ -1751,7 +1751,7 @@ void CppGenerator::writeClassRegister(QTextStream& s, const AbstractMetaClass* m
// class inject-code target/beginning
if (!metaClass->typeEntry()->codeSnips().isEmpty()) {
- writeCodeSnips(s, metaClass->typeEntry()->codeSnips(), CodeSnip::Beginning, TypeSystem::TargetLangCode);
+ writeCodeSnips(s, metaClass->typeEntry()->codeSnips(), CodeSnip::Beginning, TypeSystem::TargetLangCode, 0, metaClass);
s << endl;
}
@@ -1785,7 +1785,7 @@ void CppGenerator::writeClassRegister(QTextStream& s, const AbstractMetaClass* m
// class inject-code target/end
if (!metaClass->typeEntry()->codeSnips().isEmpty()) {
s << endl;
- writeCodeSnips(s, metaClass->typeEntry()->codeSnips(), CodeSnip::End, TypeSystem::TargetLangCode);
+ writeCodeSnips(s, metaClass->typeEntry()->codeSnips(), CodeSnip::End, TypeSystem::TargetLangCode, 0, metaClass);
}
s << '}' << endl << endl;
diff --git a/doc/typesystemvariables.rst b/doc/typesystemvariables.rst
index 63177569d..0a3da0bcb 100644
--- a/doc/typesystemvariables.rst
+++ b/doc/typesystemvariables.rst
@@ -59,6 +59,12 @@ Variables
bounded to the Python wrapper method which receives the custom code.
+**%PYTHONTYPEOBJECT**
+
+ Replaced by the Python type object for the context in which it is inserted:
+ method or class modification.
+
+
**%RETURN_TYPE**
Replaced by the type returned by a function or method.
diff --git a/shibokengenerator.cpp b/shibokengenerator.cpp
index b85fc1799..2194934f2 100644
--- a/shibokengenerator.cpp
+++ b/shibokengenerator.cpp
@@ -819,7 +819,8 @@ void ShibokenGenerator::writeCodeSnips(QTextStream& s,
const CodeSnipList& codeSnips,
CodeSnip::Position position,
TypeSystem::Language language,
- const AbstractMetaFunction* func)
+ const AbstractMetaFunction* func,
+ const AbstractMetaClass* context)
{
static QRegExp toPythonRegex("%CONVERTTOPYTHON\\[([^\\[]*)\\]");
static QRegExp pyArgsRegex("%PYARG_(\\d+)");
@@ -849,6 +850,13 @@ void ShibokenGenerator::writeCodeSnips(QTextStream& s,
Indentation indent2(INDENT);
formatCode(tmpStream, snip.code(), INDENT);
+ if (context) {
+ // replace template variable for the Python Type object for the
+ // class context in which the variable is used
+ QString pytype = cpythonTypeName(context);
+ code.replace("%PYTHONTYPEOBJECT", pytype);
+ }
+
if (func) {
// replace "toPython "converters
code.replace(toPythonRegex, "Shiboken::Converter<\\1>::toPython");
@@ -874,6 +882,15 @@ void ShibokenGenerator::writeCodeSnips(QTextStream& s,
if (func->implementingClass()) {
code.replace("%CPPSELF.", "cppSelf->");
code.replace("%CPPSELF", "cppSelf");
+
+ // replace template variable for the Python Type object for the
+ // class implementing the method in which the code snip is written
+ if (func->isStatic()) {
+ code.replace("%PYTHONTYPEOBJECT", cpythonTypeName(func->implementingClass()));
+ } else {
+ code.replace("%PYTHONTYPEOBJECT.", "self->ob_type->");
+ code.replace("%PYTHONTYPEOBJECT", "self->ob_type");
+ }
}
// replace template variables for individual arguments
diff --git a/shibokengenerator.h b/shibokengenerator.h
index ad2971e18..7fdfc8912 100644
--- a/shibokengenerator.h
+++ b/shibokengenerator.h
@@ -98,16 +98,18 @@ public:
* Write a code snip into the buffer \p s.
* CodeSnip are codes inside inject-code tags.
* \param s the buffer
- * \param func the cpp function
* \param code_snips a list of code snips
* \param position the position to insert the code snip
* \param language the kind of code snip
+ * \param func the cpp function
+ * \param context the class context for the place where the code snip will be written
*/
void writeCodeSnips(QTextStream &s,
const CodeSnipList &code_snips,
CodeSnip::Position position,
TypeSystem::Language language,
- const AbstractMetaFunction* func = 0);
+ const AbstractMetaFunction* func = 0,
+ const AbstractMetaClass* context = 0);
/**
* Returns a function's code snippets.
diff --git a/tests/samplebinding/typesystem_sample.xml b/tests/samplebinding/typesystem_sample.xml
index 1da279b1c..56774bd15 100644
--- a/tests/samplebinding/typesystem_sample.xml
+++ b/tests/samplebinding/typesystem_sample.xml
@@ -235,7 +235,7 @@
Tested in InjectCodeTest.testTypeNativeBeginning_TypeTargetBeginning:
-->
<inject-code class="target" position="beginning">
- PyInjectCode_Type.tp_str = InjectCode_tpstr;
+ %PYTHONTYPEOBJECT.tp_str = InjectCode_tpstr;
</inject-code>
<!-- Tested in InjectCodeTest.testFunctionTargetBeginning_FunctionTargetEnd -->