1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2013 LEAP
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 Set of functions to help checking situations
26 logger = logging.getLogger(__name__)
29 def leap_assert(condition, message=""):
31 Asserts the condition and displays the message if that's not
32 met. It also logs the error and its backtrace.
34 @param condition: condition to check
36 @param message: message to display if the condition isn't met
40 logger.error("Bug: %s" % (message,))
42 frame = inspect.currentframe()
43 stack_trace = traceback.format_stack(frame)
44 logger.error(''.join(stack_trace))
45 except Exception as e:
46 logger.error("Bug in leap_assert: %r" % (e,))
47 assert condition, message
50 def leap_assert_type(var, expectedType):
52 Helper assert check for a variable's expected type
54 @param var: variable to check
56 @param expectedType: type to check agains
57 @type expectedType: type
59 leap_assert(isinstance(var, expectedType),
60 "Expected type %r instead of %r" %
61 (expectedType, type(var)))