- exception now also in Python
authorArvin Schnell <aschnell@suse.de>
Tue, 25 Sep 2007 14:41:52 +0000 (14:41 +0000)
committerArvin Schnell <aschnell@suse.de>
Tue, 25 Sep 2007 14:41:52 +0000 (14:41 +0000)
examples/python/exception.py [new file with mode: 0755]
examples/ruby/exception.rb
swig/python/python.i

diff --git a/examples/python/exception.py b/examples/python/exception.py
new file mode 100755 (executable)
index 0000000..4d65b64
--- /dev/null
@@ -0,0 +1,33 @@
+#!/usr/bin/python
+
+from zypp import TmpDir, RepoManagerOptions, RepoManager, RepoInfo, Url
+
+tmp_cache_path = TmpDir()
+tmp_raw_cache_path = TmpDir()
+tmp_known_repos_path = TmpDir()
+
+opts = RepoManagerOptions()
+opts.repoCachePath = tmp_cache_path.path()
+opts.repoRawCachePath = tmp_raw_cache_path.path()
+opts.knownReposPath = tmp_known_repos_path.path()
+
+repo_manager = RepoManager(opts)
+
+repo_info = RepoInfo()
+
+repo_info.setAlias("factorytest")
+repo_info.setName("Test Repo for Factory.")
+repo_info.setEnabled(True)
+repo_info.setAutorefresh(False)
+repo_info.addBaseUrl(Url("file:///tmp/does-not-exist"))
+
+try:
+    repo_manager.addRepository(repo_info)
+    repo_manager.refreshMetadata(repo_info)
+    repo_manager.buildCache(repo_info)
+except RuntimeError, strerror:
+    print "RuntimeError"
+    print strerror
+else:
+    print "Oh, no exception"
+
index 95028fe..346d2d3 100755 (executable)
@@ -20,7 +20,7 @@ repo_info.set_alias("factorytest")
 repo_info.set_name("Test Repo for Factory.")
 repo_info.set_enabled(true)
 repo_info.set_autorefresh(false)
-repo_info.add_base_url("file:///tmp/does-not-exist")
+repo_info.add_base_url(Url.new("file:///tmp/does-not-exist"))
 
 begin
   repo_manager.add_repository(repo_info)
@@ -28,7 +28,7 @@ begin
   repo_manager.build_cache(repo_info)
 rescue ZYppException => e
   puts "ZYppException caught"
-  puts e.to_s
+  puts e
 else
   puts "Oh, no exception"
 end
index 98e40dc..3102cf7 100644 (file)
@@ -22,3 +22,16 @@ namespace zypp
 };
 %enddef
 
+
+%exception
+{
+  try {
+    $action
+  }
+  catch (const Exception& e) {
+    std::string tmp = e.historyAsString() + e.asUserString();
+    PyErr_SetString(PyExc_RuntimeError, const_cast<char*>(tmp.c_str()));
+    return NULL;
+  }
+}
+