Skip to content

Commit 4af2c3a

Browse files
committed
Don't manually save tracebacks in Python 3
Python 3 automatically saves the traceback with exceptions and uses it when reraising the exception, so we don't have to manually save the traceback. Also, just use "raise" instead of six.reraise when simply choosing to raise the exception without saving it.
1 parent e05f99a commit 4af2c3a

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

gssapi/_utils.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,31 +64,55 @@ def enc(x):
6464
return dict((enc(k), enc(v)) for k, v in six.iteritems(d))
6565

6666

67+
# in case of Python 3, just use exception chaining
6768
@deco.decorator
6869
def catch_and_return_token(func, self, *args, **kwargs):
6970
try:
7071
return func(self, *args, **kwargs)
7172
except GSSError as e:
7273
if e.token is not None and self.__DEFER_STEP_ERRORS__:
7374
self._last_err = e
74-
self._last_tb = sys.exc_info()[2]
75+
# skip the "return func" line above in the traceback
76+
if six.PY2:
77+
self._last_tb = sys.exc_info()[2].tb_next.tb_next
78+
else:
79+
self._last_err.__traceback__ = e.__traceback__.tb_next
7580

7681
return e.token
7782
else:
78-
six.reraise(*sys.exc_info())
83+
raise
7984

8085

8186
@deco.decorator
8287
def check_last_err(func, self, *args, **kwargs):
8388
if self._last_err is not None:
8489
try:
85-
six.reraise(type(self._last_err), self._last_err, self._last_tb)
90+
if six.PY2:
91+
six.reraise(type(self._last_err), self._last_err,
92+
self._last_tb)
93+
else:
94+
# NB(directxman12): not using six.reraise in Python 3 leads
95+
# to cleaner tracebacks, and raise x is valid
96+
# syntax in Python 3 (unlike raise x, y, z)
97+
raise self._last_err
8698
finally:
87-
del self._last_tb # in case of cycles, break glass
99+
if six.PY2:
100+
del self._last_tb # in case of cycles, break glass
101+
88102
self._last_err = None
89103
else:
90104
return func(self, *args, **kwargs)
91105

106+
@deco.decorator
107+
def check_last_err(func, self, *args, **kwargs):
108+
if self._last_err is not None:
109+
try:
110+
raise self._last_err
111+
finally:
112+
self._last_err = None
113+
else:
114+
return func(self, *args, **kwargs)
115+
92116

93117
class CheckLastError(type):
94118
def __new__(cls, name, parents, attrs):

0 commit comments

Comments
 (0)