summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRenato Filho <renato.filho@openbossa.org>2011-03-22 10:55:13 -0300
committerRenato Filho <renato.filho@openbossa.org>2011-03-23 14:42:13 -0300
commitf5c5a9770c7206dd426483d38a0b2b93140f5062 (patch)
treecbccefc19fbc5fd68493ebf08e299ce060216fa1
parent7a21a4f1373c069ec3161fe759269f467a592dab (diff)
downloadshiboken-f5c5a9770c7206dd426483d38a0b2b93140f5062.tar.gz
shiboken-f5c5a9770c7206dd426483d38a0b2b93140f5062.tar.xz
shiboken-f5c5a9770c7206dd426483d38a0b2b93140f5062.zip
Updated module reload test.
Avoid to load a module twice. Fixes bug #734. Reviewer: Marcelo Lira <marcelo.lira@openbossa.org> Luciano Wolf <luciano.wolf@openbossa.org>
-rw-r--r--generator/cppgenerator.cpp1
-rw-r--r--libshiboken/basewrapper.cpp11
-rw-r--r--tests/samplebinding/module_reload_test.py26
-rw-r--r--tests/samplebinding/test_module_template.py2
4 files changed, 16 insertions, 24 deletions
diff --git a/generator/cppgenerator.cpp b/generator/cppgenerator.cpp
index 77f15683..a6d81015 100644
--- a/generator/cppgenerator.cpp
+++ b/generator/cppgenerator.cpp
@@ -3418,6 +3418,7 @@ void CppGenerator::writeClassRegister(QTextStream& s, const AbstractMetaClass* m
s << INDENT << "Shiboken::ObjectType::setDestructorFunction(&" << cpythonTypeName(metaClass) << ", &Shiboken::callCppDestructor<" << dtorClassName << " >);" << endl;
}
+ s << INDENT << "Py_INCREF((PyObject*)&" << pyTypeName << "); //Incref due the 'PyModule_AddObject' steals the reference." << endl;
s << INDENT << "if (PyType_Ready((PyTypeObject*)&" << pyTypeName << ") < 0)" << endl;
s << INDENT << INDENT << "return;" << endl << endl;
diff --git a/libshiboken/basewrapper.cpp b/libshiboken/basewrapper.cpp
index d788a2d1..5800ba04 100644
--- a/libshiboken/basewrapper.cpp
+++ b/libshiboken/basewrapper.cpp
@@ -313,11 +313,16 @@ void walkThroughClassHierarchy(PyTypeObject* currentType, HierarchyVisitor* visi
bool importModule(const char* moduleName, PyTypeObject*** cppApiPtr)
{
- Shiboken::AutoDecRef module(PyImport_ImportModule(moduleName));
- if (module.isNull())
- return false;
+ PyObject* sysModules = PyImport_GetModuleDict();
+ PyObject* module = PyDict_GetItemString(sysModules, moduleName);
+ if (!module)
+ module = PyImport_ImportModule(moduleName);
+ else
+ Py_INCREF(module);
Shiboken::AutoDecRef cppApi(PyObject_GetAttrString(module, "_Cpp_Api"));
+ Py_DECREF(module);
+
if (cppApi.isNull())
return false;
diff --git a/tests/samplebinding/module_reload_test.py b/tests/samplebinding/module_reload_test.py
index 17551bb8..48d6d957 100644
--- a/tests/samplebinding/module_reload_test.py
+++ b/tests/samplebinding/module_reload_test.py
@@ -10,34 +10,18 @@ dst = os.path.join(workdir, 'test_module.py')
shutil.copyfile(src, dst)
sys.path.append(workdir)
-def increment_module_value():
- modfile = open(dst, 'a')
- modfile.write('MyOtherObjectType.value += 1' + os.linesep)
- modfile.flush()
- modfile.close()
- try:
- os.remove(dst + 'c')
- except:
- os.remove(dst + 'o')
-
class TestModuleReloading(unittest.TestCase):
def testModuleReloading(self):
'''Test module reloading with on-the-fly modifications.'''
import test_module
- self.assertEqual(test_module.MyOtherObjectType.value, 10)
-
- increment_module_value()
- reload(sys.modules['test_module'])
- self.assertEqual(test_module.MyOtherObjectType.value, 11)
-
- reload(sys.modules['test_module'])
- self.assertEqual(test_module.MyOtherObjectType.value, 11)
- increment_module_value()
- reload(sys.modules['test_module'])
- self.assertEqual(test_module.MyOtherObjectType.value, 12)
+ for i in range(3):
+ oldObject = test_module.obj
+ self.assertTrue(oldObject is test_module.obj)
+ reload(test_module)
+ self.assertFalse(oldObject is test_module.obj)
if __name__ == "__main__":
unittest.main()
diff --git a/tests/samplebinding/test_module_template.py b/tests/samplebinding/test_module_template.py
index b2e917de..b6cfb838 100644
--- a/tests/samplebinding/test_module_template.py
+++ b/tests/samplebinding/test_module_template.py
@@ -8,3 +8,5 @@ class MyObjectType(ObjectType):
class MyOtherObjectType(OtherObjectType):
value = 10
+
+obj = MyObjectType()