Skip to content

Commit d9454d8

Browse files
committed
simplify code of tryf
Python's `raise` statement converts a raised class into an instance by instantiating it with no args. So we don't need special handling for the case where the exception being caught is a class.
1 parent f2116c5 commit d9454d8

1 file changed

Lines changed: 16 additions & 13 deletions

File tree

unpythonic/misc.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -309,35 +309,38 @@ def isexceptiontype(exc):
309309
pass
310310
return False
311311

312-
def determine_exctype(exc):
313-
if isinstance(exc, BaseException): # "raise SomeError()"
314-
return type(exc)
315-
if isexceptiontype(exc): # "raise SomeError"
316-
return exc
317-
assert False # Python can only raise stuff that inherits from BaseException
318-
312+
# validate handlers
313+
for excspec, handler in handlers:
314+
if isinstance(excspec, tuple): # tuple of exception types
315+
if not all(isexceptiontype(t) for t in excspec):
316+
raise TypeError("All elements of a tuple excspec must be exception types, got {}".format(excspec))
317+
elif not isexceptiontype(excspec): # single exception type
318+
raise TypeError("excspec must be an exception type or tuple of exception types, got {}".format(excspec))
319+
320+
# run
319321
try:
320322
ret = body()
321323
except BaseException as exception:
322-
exctype = determine_exctype(exception)
324+
# Even if a class is raised, as in `raise StopIteration`, the `raise` statement
325+
# converts it into an instance by instantiating with no args. So we need no
326+
# special handling for the "class raised" case.
327+
# https://docs.python.org/3/reference/simple_stmts.html#the-raise-statement
328+
# https://stackoverflow.com/questions/19768515/is-there-a-difference-between-raising-exception-class-and-exception-instance/19768732
329+
exctype = type(exception)
323330
for excspec, handler in handlers:
324331
if isinstance(excspec, tuple): # tuple of exception types
325-
if not all(isexceptiontype(t) for t in excspec):
326-
raise TypeError("All elements of a tuple excspec must be exception types")
327332
# this is safe, exctype is always a class at this point.
328333
if any(issubclass(exctype, t) for t in excspec):
329334
if accepts_arg(handler):
330335
return handler(exception)
331336
else:
332337
return handler()
333-
elif isexceptiontype(excspec): # single exception type
338+
else: # single exception type
334339
if issubclass(exctype, excspec):
335340
if accepts_arg(handler):
336341
return handler(exception)
337342
else:
338343
return handler()
339-
else:
340-
raise TypeError("excspec must be an exception type or tuple of exception types")
341344
else:
342345
if elsef is not None:
343346
return elsef()

0 commit comments

Comments
 (0)