Minor Python 3 fixes

basestring does not exist anymore in Python 3: use six.string_types instead.

In "try: .../except Exception as err: ...", err is a local variable, it does no
more exist after the except block. Copy the exception in a new cls_error
variable to fix Python 3 support.

Extract of Python 3 documentation: "When an exception has been assigned using
as target, it is cleared at the end of the except clause. (...) Exceptions are
cleared because with the traceback attached to them, they form a reference
cycle with the stack frame, keeping all locals in that frame alive until the
next garbage collection occurs."

http://docs.python.org/3.3/reference/compound_stmts.html#try

Change-Id: I2efb14b3838f78d1ed5e09b3ccd4e796a3448aee
This commit is contained in:
Victor Stinner 2013-12-18 14:23:24 +01:00
parent 64f91d30a8
commit 690868bc99
2 changed files with 4 additions and 3 deletions

@ -28,6 +28,7 @@ __all__ = [
]
from oslo.config import cfg
import six
from stevedore import driver
from oslo.messaging import exceptions
@ -352,8 +353,7 @@ class TransportURL(object):
if not url:
return cls(conf, aliases=aliases)
# FIXME(flaper87): Not PY3K compliant
if not isinstance(url, basestring):
if not isinstance(url, six.string_types):
raise InvalidTransportURL(url, 'Wrong URL type')
url = urlutils.urlparse(url)

@ -119,6 +119,7 @@ class SerializeRemoteExceptionTestCase(test_utils.BaseTestCase):
try:
raise self.cls(*self.args, **self.kwargs)
except Exception as ex:
cls_error = ex
if self.add_remote:
ex = add_remote_postfix(ex)
raise ex
@ -137,7 +138,7 @@ class SerializeRemoteExceptionTestCase(test_utils.BaseTestCase):
self.assertEqual(failure['kwargs'], self.kwargs)
# Note: _Remote prefix not stripped from tracebacks
tb = ex.__class__.__name__ + ': ' + self.msg
tb = cls_error.__class__.__name__ + ': ' + self.msg
self.assertIn(tb, ''.join(failure['tb']))
if self.log_failure: