diff options
author | Renato Filho <renato.filho@openbossa.org> | 2011-03-22 10:55:13 -0300 |
---|---|---|
committer | Hugo Parente Lima <hugo.pl@gmail.com> | 2012-03-08 16:15:18 -0300 |
commit | 0882f35b2d010515593ee3ddfe64e94f7f9942e5 (patch) | |
tree | 8aff61aadbe2eb6a8b34f20c39afcadff733fc31 | |
parent | 17546cd6200fd45d57e8b07b77f2f99ade36776b (diff) |
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.cpp | 1 | ||||
-rw-r--r-- | libshiboken/basewrapper.cpp | 11 | ||||
-rw-r--r-- | tests/samplebinding/module_reload_test.py | 26 | ||||
-rw-r--r-- | tests/samplebinding/test_module_template.py | 2 |
4 files changed, 16 insertions, 24 deletions
diff --git a/generator/cppgenerator.cpp b/generator/cppgenerator.cpp index 77f156836..a6d810158 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 b4a981818..ac3eb07e6 100644 --- a/libshiboken/basewrapper.cpp +++ b/libshiboken/basewrapper.cpp @@ -314,11 +314,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 17551bb86..48d6d9572 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 b2e917de4..b6cfb8389 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() |