Fix re-raising exception

In Python 2 the raise command can take 3 objects as arguments which
are used to specify the exception to be raise as well as detailed
information and traceback [1].

In Python 3 the sintax is changed and now the keyword "from" and
"with_traceback" keyworkds are used to specify this information. [2]

Example which has the same behaviour for both Python 2 and 3:

    try:
        a = 1/0
    except Exception:
        import sys
        err = sys.exc_info()

        try:
                raise Exception("Test")
        except:
                pass

        if sys.version_info[0] == 2:
            exec("raise err[0], err[1], err[2]")
        else:
            raise
This commit is contained in:
Radostin Stoyanov 2017-10-11 12:35:52 +01:00 committed by Cole Robinson
parent 95c695d774
commit 08c23477fd

View File

@ -399,13 +399,20 @@ class Guest(XMLBuilder):
try:
domain.create()
except Exception:
# pylint: disable=exec-used, unused-variable
import sys
exc_info = sys.exc_info()
try:
domain.undefine()
except Exception:
pass
raise exc_info[0], exc_info[1], exc_info[2]
if sys.version_info[0] == 3:
raise
else:
# Use exec() to avoid syntax error from Python 3
exec("raise exc_info[0], exc_info[1], exc_info[2]")
if install_xml and install_xml != final_xml:
domain = self.conn.defineXML(final_xml)