def compile_catch_expression(self, expr): catch = expr.pop(0) # catch try: exceptions = expr.pop(0) except IndexError: exceptions = HyList() # exceptions catch should be either: # [[list of exceptions]] # or # [variable [list of exceptions]] # or # [variable exception] # or # [exception] # or # [] if not isinstance(exceptions, HyList): raise HyTypeError(exceptions, "`%s' exceptions list is not a list" % catch) if len(exceptions) > 2: raise HyTypeError(exceptions, "`%s' exceptions list is too long" % catch) # [variable [list of exceptions]] # let's pop variable and use it as name if len(exceptions) == 2: name = exceptions.pop(0) if sys.version_info[0] >= 3: # Python3 features a change where the Exception handler # moved the name from a Name() to a pure Python String type. # # We'll just make sure it's a pure "string", and let it work # it's magic. name = ast_str(name) else: # Python2 requires an ast.Name, set to ctx Store. name = self._storeize(self.compile(name)) else: name = None try: exceptions_list = exceptions.pop(0) except IndexError: exceptions_list = [] if isinstance(exceptions_list, list): if len(exceptions_list): # [FooBar BarFoo] → catch Foobar and BarFoo exceptions _type = ast.Tuple(elts=[self.compile(x) for x in exceptions_list], lineno=expr.start_line, col_offset=expr.start_column, ctx=ast.Load()) else: # [] → all exceptions catched _type = None elif isinstance(exceptions_list, HySymbol): _type = self.compile(exceptions_list) else: raise HyTypeError(exceptions, "`%s' needs a valid exception list" % catch) body = self._code_branch([self.compile(x) for x in expr], expr.start_line, expr.start_column) return ast.ExceptHandler( lineno=expr.start_line, col_offset=expr.start_column, type=_type, name=name, body=body)
def compile_catch_expression(self, expr): catch = expr.pop(0) # catch try: exceptions = expr.pop(0) except IndexError: exceptions = HyList() # exceptions catch should be either: # [[list of exceptions]] # or # [variable [list of exceptions]] # or # [variable exception] # or # [exception] # or # [] if not isinstance(exceptions, HyList): raise HyTypeError(exceptions, "`%s' exceptions list is not a list" % catch) if len(exceptions) > 2: raise HyTypeError(exceptions, "`%s' exceptions list is too long" % catch) # [variable [list of exceptions]] # let's pop variable and use it as name if len(exceptions) == 2: name = exceptions.pop(0) if sys.version_info[0] >= 3: # Python3 features a change where the Exception handler # moved the name from a Name() to a pure Python String type. # # We'll just make sure it's a pure "string", and let it work # it's magic. name = ast_str(name) else: # Python2 requires an ast.Name, set to ctx Store. name = self._storeize(self.compile(name)) else: name = None try: exceptions_list = exceptions.pop(0) except IndexError: exceptions_list = [] if isinstance(exceptions_list, list): if len(exceptions_list): # [FooBar BarFoo] → catch Foobar and BarFoo exceptions _type = ast.Tuple( elts=[self.compile(x) for x in exceptions_list], lineno=expr.start_line, col_offset=expr.start_column, ctx=ast.Load()) else: # [] → all exceptions catched _type = None elif isinstance(exceptions_list, HySymbol): _type = self.compile(exceptions_list) else: raise HyTypeError(exceptions, "`%s' needs a valid exception list" % catch) body = self._code_branch([self.compile(x) for x in expr], expr.start_line, expr.start_column) return ast.ExceptHandler(lineno=expr.start_line, col_offset=expr.start_column, type=_type, name=name, body=body)