def test_format_list_full(self): extracted = stacktrace.extract_stack( get_frame( supplement=dict( source_url='/foo/bar.pt', line=42, column=84, expression='tal:define="foo view/foo"', warnings=('watch out', 'pass auf'), getInfo=lambda: 'read all about it'), info='I am the Walrus' ) ) formatted = stacktrace.format_list(extracted) self.assertIsInstance(formatted, list) for line in formatted: self.assertEndsWith(line, '\n') line = formatted[-1].split('\n') self.assertStartsWith( line[0], ' File "' + MY_FILE_NAME + '", line ') self.assertEndsWith(line[0], ', in get_frame') self.assertEqual(' return sys._getframe()', line[1]) self.assertEqual(' - /foo/bar.pt', line[2]) self.assertEqual(' - Line 42, Column 84', line[3]) self.assertEqual( ' - Expression: tal:define="foo view/foo"', line[4]) self.assertEqual( ' - Warning: watch out', line[5]) self.assertEqual( ' - Warning: pass auf', line[6]) self.assertEqual( 'read all about it', line[7]) self.assertEqual(' - I am the Walrus', line[8])
def test_print_list_default(self): extracted = stacktrace.extract_stack(get_frame()) original_stderr = sys.__stderr__ stderr = sys.stderr = StringIO.StringIO() try: stacktrace.print_list(extracted) finally: sys.stderr = original_stderr self.assertEndsWith(stderr.getvalue(), 'return sys._getframe()\n')
def test_format_list_simple(self): extracted = stacktrace.extract_stack(get_frame()) formatted = stacktrace.format_list(extracted) self.assertIsInstance(formatted, list) for line in formatted: self.assertEndsWith(line, '\n') line = formatted[-1].split('\n') self.assertStartsWith(line[0], ' File "' + MY_FILE_NAME + '", line ') self.assertEndsWith(line[0], ', in get_frame') self.assertEqual(' return sys._getframe()', line[1])
def test_format_list_simple(self): extracted = stacktrace.extract_stack(get_frame()) formatted = stacktrace.format_list(extracted) self.assertIsInstance(formatted, list) for line in formatted: self.assertEndsWith(line, '\n') line = formatted[-1].split('\n') self.assertStartsWith( line[0], ' File "' + MY_FILE_NAME + '", line ') self.assertEndsWith(line[0], ', in get_frame') self.assertEqual(' return sys._getframe()', line[1])
def test_extract_stack(self): extracted = stacktrace.extract_stack(get_frame()) self.assertTrue(len(extracted) > 1) filename, lineno, name, line, modname, supplement, info = ( extracted[-1]) self.assertEqual(MY_FILE_NAME, filename) self.assertIsInstance(lineno, int) self.assertEqual('get_frame', name) self.assertEqual('return sys._getframe()', line) self.assertEqual(__name__, modname) self.assertIs(None, supplement) self.assertIs(None, info)
def connection_raw_execute(self, connection, raw_cursor, statement, params): statement_to_log = statement if params: statement_to_log = raw_cursor.mogrify( statement, tuple(connection.to_database(params))) if isinstance(statement_to_log, bytes): statement_to_log = statement_to_log.decode('UTF-8', errors='replace') # Record traceback to log, if requested. print_traceback = self._debug_sql_extra log_sql = getattr(_local, 'sql_logging', None) log_traceback = False if log_sql is not None: log_sql.append(dict(stack=None, sql=None, exception=None)) conditional = getattr(_local, 'sql_logging_tracebacks_if', None) if callable(conditional): try: log_traceback = conditional( self._normalize_whitespace( statement_to_log.strip()).upper()) except (MemoryError, SystemExit, KeyboardInterrupt): raise except: exc_type, exc_value, tb = sys.exc_info() log_sql[-1]['exception'] = (exc_type, exc_value) log_sql[-1]['stack'] = extract_tb(tb) else: log_traceback = bool(conditional) if print_traceback or log_traceback: stack = extract_stack() if log_traceback: log_sql[-1]['stack'] = stack if print_traceback: print_list(stack) sys.stderr.write("." * 70 + "\n") # store the last executed statement as an attribute on the current # thread threading.currentThread().lp_last_sql_statement = statement request_starttime = getattr(_local, 'request_start_time', None) if request_starttime is None: if print_traceback or self._debug_sql or log_sql is not None: # Stash some information for logging at the end of the # SQL execution. connection._lp_statement_info = (int( time() * 1000), u'SQL-%s' % connection._database.name, statement_to_log) return action = get_request_timeline(get_current_browser_request()).start( u'SQL-%s' % connection._database.name, statement_to_log) connection._lp_statement_action = action
def test_format_list_extra_errors(self): extracted = stacktrace.extract_stack(get_frame(supplement=dict())) extracted[-1][-2]['warnings'] = object() # This should never happen. original_stderr = sys.__stderr__ stderr = sys.stderr = StringIO.StringIO() self.assertFalse(stacktrace.DEBUG_EXCEPTION_FORMATTER) stacktrace.DEBUG_EXCEPTION_FORMATTER = True try: formatted = stacktrace.format_list(extracted) finally: sys.stderr = original_stderr stacktrace.DEBUG_EXCEPTION_FORMATTER = False self.assertStartsWith(stderr.getvalue(), 'Traceback (most recent') self.assertEndsWith(formatted[-1], ' return sys._getframe()\n')
def connection_raw_execute(self, connection, raw_cursor, statement, params): statement_to_log = statement if params: statement_to_log = raw_cursor.mogrify( statement, tuple(connection.to_database(params))) # Record traceback to log, if requested. print_traceback = self._debug_sql_extra log_sql = getattr(_local, 'sql_logging', None) log_traceback = False if log_sql is not None: log_sql.append(dict(stack=None, sql=None, exception=None)) conditional = getattr(_local, 'sql_logging_tracebacks_if', None) if callable(conditional): try: log_traceback = conditional( self._normalize_whitespace( statement_to_log.strip()).upper()) except (MemoryError, SystemExit, KeyboardInterrupt): raise except: exc_type, exc_value, tb = sys.exc_info() log_sql[-1]['exception'] = (exc_type, exc_value) log_sql[-1]['stack'] = extract_tb(tb) else: log_traceback = bool(conditional) if print_traceback or log_traceback: stack = extract_stack() if log_traceback: log_sql[-1]['stack'] = stack if print_traceback: print_list(stack) sys.stderr.write("." * 70 + "\n") # store the last executed statement as an attribute on the current # thread threading.currentThread().lp_last_sql_statement = statement request_starttime = getattr(_local, 'request_start_time', None) if request_starttime is None: if print_traceback or self._debug_sql or log_sql is not None: # Stash some information for logging at the end of the # SQL execution. connection._lp_statement_info = ( int(time() * 1000), 'SQL-%s' % connection._database.name, statement_to_log) return action = get_request_timeline(get_current_browser_request()).start( 'SQL-%s' % connection._database.name, statement_to_log) connection._lp_statement_action = action
def test_format_list_full(self): extracted = stacktrace.extract_stack( get_frame(supplement=dict(source_url='/foo/bar.pt', line=42, column=84, expression='tal:define="foo view/foo"', warnings=('watch out', 'pass auf'), getInfo=lambda: 'read all about it'), info='I am the Walrus')) formatted = stacktrace.format_list(extracted) self.assertIsInstance(formatted, list) for line in formatted: self.assertEndsWith(line, '\n') line = formatted[-1].split('\n') self.assertStartsWith(line[0], ' File "' + MY_FILE_NAME + '", line ') self.assertEndsWith(line[0], ', in get_frame') self.assertEqual(' return sys._getframe()', line[1]) self.assertEqual(' - /foo/bar.pt', line[2]) self.assertEqual(' - Line 42, Column 84', line[3]) self.assertEqual(' - Expression: tal:define="foo view/foo"', line[4]) self.assertEqual(' - Warning: watch out', line[5]) self.assertEqual(' - Warning: pass auf', line[6]) self.assertEqual('read all about it', line[7]) self.assertEqual(' - I am the Walrus', line[8])
def test_print_list_file(self): extracted = stacktrace.extract_stack(get_frame()) f = StringIO.StringIO() stacktrace.print_list(extracted, file=f) self.assertEndsWith(f.getvalue(), 'return sys._getframe()\n')