[zypp-devel] Python Bindings and RepoInfo.baseUrls()

I have the following example: import zypp def main(): myRepoInfo = zypp.RepoInfo() myRepoInfo.addBaseUrl(zypp.Url("file:///mounts/mirror/SuSE/ftp.opensuse.org/srv/ftp/pub/opensuse/debug/update/11.1/")) myRepoInfo.setAlias("default") myRepoInfo.setName("default") myRepoInfo.setEnabled(True) myRepoInfo.setType(zypp.RepoType.RPMMD) # for now myRepoInfo.setGpgCheck(False) for url in myRepoInfo.baseUrls(): print url if __name__ == "__main__": main() When I let it run I get the following error: $ python baseurls.py swig/python detected a memory leak of type 'std::set< zypp::Url,std::less<zypp::Url >,std::allocator< zypp::Url > > *', no destructor found. Traceback (most recent call last): File "baseurls.py", line 16, in <module> main() File "baseurls.py", line 12, in main for url in myRepoInfo.baseUrls(): TypeError: 'PySwigObject' object is not iterable It somehow seems that the python bindings are incomplete. Any SWIG expert that can help me here? Cheers, Jan -- To unsubscribe, e-mail: zypp-devel+unsubscribe@opensuse.org For additional commands, e-mail: zypp-devel+help@opensuse.org

On Tue, Mar 10, Jan Blunck wrote:
I have the following example:
This is not related to the python part of the bindings. The same example in ruby is also broken. Basically the std::set stuff for the Url class seems to be odd. I'm neither able to append nor to insert new Url objects. Thankful for any hint, Jan

* Jan Blunck <jblunck@suse.de> [Mar 11. 2009 15:55]:
On Tue, Mar 10, Jan Blunck wrote:
I have the following example:
This is not related to the python part of the bindings. The same example in ruby is also broken. Basically the std::set stuff for the Url class seems to be odd. I'm neither able to append nor to insert new Url objects.
This seems to be deep inside swig/libzypp/C++ and I have no idea how to fix this. Klaus -- To unsubscribe, e-mail: zypp-devel+unsubscribe@opensuse.org For additional commands, e-mail: zypp-devel+help@opensuse.org

On Thu, Mar 12, Klaus Kaempf wrote:
* Jan Blunck <jblunck@suse.de> [Mar 11. 2009 15:55]:
On Tue, Mar 10, Jan Blunck wrote:
I have the following example:
This is not related to the python part of the bindings. The same example in ruby is also broken. Basically the std::set stuff for the Url class seems to be odd. I'm neither able to append nor to insert new Url objects.
This seems to be deep inside swig/libzypp/C++ and I have no idea how to fix this.
I think I found the problem. It seems to be a namespace problem. Here is a patch that fixes the issue. I also added a testcase for the RepoInfo class. At the same time I extended the class by equivalents for dumpOn() and friends. I'm usure if they are really necessary. At least the original methods need to be ignored because of the std:ostream arguments. Comments? Jan commit fca020d7476ac4e09e16895c7a16a64baa2caf48 Author: Jan Blunck <jblunck@suse.de> Date: Thu Mar 12 14:50:37 2009 +0100 Fix RepoInfo::baseUrls() etc. Add a testcase for RepoInfo and fix the problems with UrlSet. diff --git a/swig/RepoInfo.i b/swig/RepoInfo.i index 6ca7ccf..f86a254 100644 --- a/swig/RepoInfo.i +++ b/swig/RepoInfo.i @@ -1,6 +1,6 @@ #ifdef SWIGPERL5 #else - %template(UrlSet) std::set<Url>; +%template(UrlSet) std::set<zypp::Url>; #endif namespace zypp @@ -12,13 +12,45 @@ namespace zypp %ignore operator<<; %ignore operator<; } + typedef std::list<Url> UrlSet; } -%include <zypp/repo/RepoInfoBase.h> +%ignore zypp::repo::RepoInfoBase::dumpOn(std::ostream &) const; +%ignore zypp::repo::RepoInfoBase::dumpAsIniOn(std::ostream &) const; +%ignore zypp::repo::RepoInfoBase::dumpAsXMLOn(std::ostream &) const; +%include <zypp/repo/RepoInfoBase.h> // This is due to a typo in libzypp < 5.4.0 %ignore zypp::RepoInfo::defaultPrioity(); +%ignore zypp::RepoInfo::dumpOn(std::ostream &) const; +%ignore zypp::RepoInfo::dumpAsIniOn(std::ostream &) const; +%ignore zypp::RepoInfo::dumpAsXMLOn(std::ostream &) const; + %include <zypp/RepoInfo.h> typedef std::list<zypp::RepoInfo> RepoInfoList; %template(RepoInfoList) std::list<zypp::RepoInfo>; + +%extend zypp::RepoInfo +{ + std::string dump(void) const + { + std::ostringstream str; + self->dumpOn(str); + return str.str(); + } + + std::string dumpAsIni(void) const + { + std::ostringstream str; + self->dumpAsIniOn(str); + return str.str(); + } + + std::string dumpAsXML(void) const + { + std::ostringstream str; + self->dumpAsXMLOn(str); + return str.str(); + } +} diff --git a/swig/python/tests/CMakeLists.txt b/swig/python/tests/CMakeLists.txt index 216dd21..70eb6df 100644 --- a/swig/python/tests/CMakeLists.txt +++ b/swig/python/tests/CMakeLists.txt @@ -5,3 +5,4 @@ ENABLE_TESTING() ADD_TEST(bindings_python_loading python ${CMAKE_CURRENT_SOURCE_DIR}/loading.py) +ADD_TEST(bindings_python_repoinfo python ${CMAKE_CURRENT_SOURCE_DIR}/repoinfo.py) diff --git a/swig/python/tests/repoinfo.py b/swig/python/tests/repoinfo.py new file mode 100755 index 0000000..e134709 --- /dev/null +++ b/swig/python/tests/repoinfo.py @@ -0,0 +1,64 @@ +#!/usr/bin/python +# +# Author: Jan Blunck <jblunck@suse.de> +# + +import unittest +from zypp import RepoInfo, Url, UrlSet, RepoType +import sys + +repo_urls = [ "file:/mounts/mirror/SuSE/ftp.opensuse.org/srv/ftp/pub/opensuse/debug/update/11.1/", + "http://download.opensuse.org/debug/update/11.1/" ] + +class RepoInfoTestCase(unittest.TestCase): + + def setUp(self): + self.info = RepoInfo() + self.info.addBaseUrl(Url(repo_urls[0])) + self.info.addBaseUrl(Url(repo_urls[1])) + self.info.setAlias("default") + self.info.setName("default") + self.info.setEnabled(True) + self.info.setType(RepoType.RPMMD) + self.info.setGpgCheck(False) + + def testUrlSetIsUrlSet(self): + urls = UrlSet() + assert urls.__class__.__name__ == "UrlSet", 'Incorrect class (' + urls.__class__.__name__ + ')' + + def testUrlSetAppend(self): + urls = UrlSet() + urls.append(Url(repo_urls[0])) + urls.append(Url(repo_urls[1])) + assert urls.size() == 2, 'Incorrect size ' + urls.size() + + def testBaseUrlsReturnsTuple(self): + baseurls = self.info.baseUrls() + assert baseurls.__class__.__name__ == "tuple", 'Incorrect class (' + baseurls.__class__.__name__ + ')' + + def testBaseUrlsIteratable(self): + baseurls = self.info.baseUrls() + for url in baseurls: + assert url.__str__() in repo_urls, 'Incorrect URL ' + url.__str__() + + def testSetBaseUrl(self): + baseurls = self.info.baseUrls() + assert len(baseurls) == 2 + self.info.setBaseUrl(Url(repo_urls[0])) + baseurls = self.info.baseUrls() + assert len(baseurls) == 1 + + def testDump(self): + out = self.info.dump() + assert len(out) == 414, 'Invalid output length %d' % len(out) + + def testDumpIni(self): + out = self.info.dumpAsIni() + assert len(out) == 208, 'Invalid output length %d' % len(out) + + def testDumpXML(self): + out = self.info.dumpAsXML() + assert len(out) == 253, 'Invalid output length %d' % len(out) + +if __name__ == "__main__": + unittest.main() diff --git a/swig/zypp.i b/swig/zypp.i index 896119c..cabcde8 100644 --- a/swig/zypp.i +++ b/swig/zypp.i @@ -49,7 +49,6 @@ using namespace zypp::repo; using namespace zypp::resfilter; using namespace zypp::filesystem; -typedef std::set<Url> UrlSet; typedef std::list<std::string> StringList; %} -- To unsubscribe, e-mail: zypp-devel+unsubscribe@opensuse.org For additional commands, e-mail: zypp-devel+help@opensuse.org

* Jan Blunck <jblunck@suse.de> [Mar 12. 2009 15:00]:
On Thu, Mar 12, Klaus Kaempf wrote:
* Jan Blunck <jblunck@suse.de> [Mar 11. 2009 15:55]:
On Tue, Mar 10, Jan Blunck wrote:
I have the following example:
This is not related to the python part of the bindings. The same example in ruby is also broken. Basically the std::set stuff for the Url class seems to be odd. I'm neither able to append nor to insert new Url objects.
This seems to be deep inside swig/libzypp/C++ and I have no idea how to fix this.
I think I found the problem. It seems to be a namespace problem. Here is a patch that fixes the issue. I also added a testcase for the RepoInfo class.
At the same time I extended the class by equivalents for dumpOn() and friends. I'm usure if they are really necessary. At least the original methods need to be ignored because of the std:ostream arguments.
Comments?
Great, thanks for your fix. Pushed to libzypp-bindings HEAD. Klaus -- To unsubscribe, e-mail: zypp-devel+unsubscribe@opensuse.org For additional commands, e-mail: zypp-devel+help@opensuse.org
participants (2)
-
Jan Blunck
-
Klaus Kaempf