Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: build/android/pylib/utils/reraiser_thread.py

Issue 132463007: Enable presubmit pylint in build/android. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase vs tot and only disabling F0401 in specific spots Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright 2013 The Chromium Authors. All rights reserved. 1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 """Thread and ThreadGroup that reraise exceptions on the main thread.""" 5 """Thread and ThreadGroup that reraise exceptions on the main thread."""
6 # pylint: disable=W0212
6 7
7 import logging 8 import logging
8 import sys 9 import sys
9 import threading 10 import threading
10 import time
11 import traceback 11 import traceback
12 12
13 import watchdog_timer 13 from pylib.utils import watchdog_timer
14 14
15 15
16 class TimeoutError(Exception): 16 class TimeoutError(Exception):
17 """Module-specific timeout exception.""" 17 """Module-specific timeout exception."""
18 pass 18 pass
19 19
20 20
21 def LogThreadStack(thread): 21 def LogThreadStack(thread):
22 """Log the stack for the given thread. 22 """Log the stack for the given thread.
23 23
24 Args: 24 Args:
25 thread: a threading.Thread instance. 25 thread: a threading.Thread instance.
26 """ 26 """
27 stack = sys._current_frames()[thread.ident] 27 stack = sys._current_frames()[thread.ident]
28 logging.critical('*' * 80) 28 logging.critical('*' * 80)
29 logging.critical('Stack dump for thread \'%s\'', thread.name) 29 logging.critical('Stack dump for thread \'%s\'', thread.name)
30 logging.critical('*' * 80) 30 logging.critical('*' * 80)
31 for filename, lineno, name, line in traceback.extract_stack(stack): 31 for filename, lineno, name, line in traceback.extract_stack(stack):
32 logging.critical('File: "%s", line %d, in %s', filename, lineno, name) 32 logging.critical('File: "%s", line %d, in %s', filename, lineno, name)
33 if line: 33 if line:
34 logging.critical(' %s', line.strip()) 34 logging.critical(' %s', line.strip())
35 logging.critical('*' * 80) 35 logging.critical('*' * 80)
36 36
37 37
38 class ReraiserThread(threading.Thread): 38 class ReraiserThread(threading.Thread):
39 """Thread class that can reraise exceptions.""" 39 """Thread class that can reraise exceptions."""
40 40
41 def __init__(self, func, args=[], kwargs={}, name=None): 41 def __init__(self, func, args=None, kwargs=None, name=None):
42 """Initialize thread. 42 """Initialize thread.
43 43
44 Args: 44 Args:
45 func: callable to call on a new thread. 45 func: callable to call on a new thread.
46 args: list of positional arguments for callable, defaults to empty. 46 args: list of positional arguments for callable, defaults to empty.
47 kwargs: dictionary of keyword arguments for callable, defaults to empty. 47 kwargs: dictionary of keyword arguments for callable, defaults to empty.
48 name: thread name, defaults to Thread-N. 48 name: thread name, defaults to Thread-N.
49 """ 49 """
50 super(ReraiserThread, self).__init__(name=name) 50 super(ReraiserThread, self).__init__(name=name)
51 if not args:
52 args = []
53 if not kwargs:
54 kwargs = {}
51 self.daemon = True 55 self.daemon = True
52 self._func = func 56 self._func = func
53 self._args = args 57 self._args = args
54 self._kwargs = kwargs 58 self._kwargs = kwargs
55 self._exc_info = None 59 self._exc_info = None
56 60
57 def ReraiseIfException(self): 61 def ReraiseIfException(self):
58 """Reraise exception if an exception was raised in the thread.""" 62 """Reraise exception if an exception was raised in the thread."""
59 if self._exc_info: 63 if self._exc_info:
60 raise self._exc_info[0], self._exc_info[1], self._exc_info[2] 64 raise self._exc_info[0], self._exc_info[1], self._exc_info[2]
61 65
62 #override 66 #override
63 def run(self): 67 def run(self):
64 """Overrides Thread.run() to add support for reraising exceptions.""" 68 """Overrides Thread.run() to add support for reraising exceptions."""
65 try: 69 try:
66 self._func(*self._args, **self._kwargs) 70 self._func(*self._args, **self._kwargs)
67 except: 71 except:
68 self._exc_info = sys.exc_info() 72 self._exc_info = sys.exc_info()
69 raise 73 raise
70 74
71 75
72 class ReraiserThreadGroup(object): 76 class ReraiserThreadGroup(object):
73 """A group of ReraiserThread objects.""" 77 """A group of ReraiserThread objects."""
74 78
75 def __init__(self, threads=[]): 79 def __init__(self, threads=None):
76 """Initialize thread group. 80 """Initialize thread group.
77 81
78 Args: 82 Args:
79 threads: a list of ReraiserThread objects; defaults to empty. 83 threads: a list of ReraiserThread objects; defaults to empty.
80 """ 84 """
85 if not threads:
86 threads = []
81 self._threads = threads 87 self._threads = threads
82 88
83 def Add(self, thread): 89 def Add(self, thread):
84 """Add a thread to the group. 90 """Add a thread to the group.
85 91
86 Args: 92 Args:
87 thread: a ReraiserThread object. 93 thread: a ReraiserThread object.
88 """ 94 """
89 self._threads.append(thread) 95 self._threads.append(thread)
90 96
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 131
126 Args: 132 Args:
127 watcher: Watchdog object providing timeout, by default waits forever. 133 watcher: Watchdog object providing timeout, by default waits forever.
128 """ 134 """
129 try: 135 try:
130 self._JoinAll(watcher) 136 self._JoinAll(watcher)
131 except TimeoutError: 137 except TimeoutError:
132 for thread in (t for t in self._threads if t.isAlive()): 138 for thread in (t for t in self._threads if t.isAlive()):
133 LogThreadStack(thread) 139 LogThreadStack(thread)
134 raise 140 raise
OLDNEW
« no previous file with comments | « build/android/pylib/utils/report_results.py ('k') | build/android/pylib/utils/reraiser_thread_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698