Commit 6b514aa9 authored by Benjamin Peterson's avatar Benjamin Peterson

change is_daemon, set_daemon, get_name, and set_name to properties

I'm add add warnings and backport this to 2.6 soon
parent 4c9798ba
...@@ -34,7 +34,7 @@ class TestThread(threading.Thread): ...@@ -34,7 +34,7 @@ class TestThread(threading.Thread):
delay = random.random() / 10000.0 delay = random.random() / 10000.0
if verbose: if verbose:
print('task %s will run for %.1f usec' % print('task %s will run for %.1f usec' %
(self.get_name(), delay * 1e6)) (self.name, delay * 1e6))
with self.sema: with self.sema:
with self.mutex: with self.mutex:
...@@ -45,14 +45,14 @@ class TestThread(threading.Thread): ...@@ -45,14 +45,14 @@ class TestThread(threading.Thread):
time.sleep(delay) time.sleep(delay)
if verbose: if verbose:
print('task', self.get_name(), 'done') print('task', self.name, 'done')
with self.mutex: with self.mutex:
self.nrunning.dec() self.nrunning.dec()
self.testcase.assert_(self.nrunning.get() >= 0) self.testcase.assert_(self.nrunning.get() >= 0)
if verbose: if verbose:
print('%s is finished. %d tasks are running' % print('%s is finished. %d tasks are running' %
(self.get_name(), self.nrunning.get())) (self.name, self.nrunning.get()))
class ThreadTests(unittest.TestCase): class ThreadTests(unittest.TestCase):
...@@ -173,7 +173,7 @@ class ThreadTests(unittest.TestCase): ...@@ -173,7 +173,7 @@ class ThreadTests(unittest.TestCase):
worker_saw_exception.set() worker_saw_exception.set()
t = Worker() t = Worker()
t.set_daemon(True) # so if this fails, we don't hang Python at shutdown t.daemon = True # so if this fails, we don't hang Python at shutdown
t.start() t.start()
if verbose: if verbose:
print(" started worker thread") print(" started worker thread")
...@@ -259,7 +259,7 @@ class ThreadTests(unittest.TestCase): ...@@ -259,7 +259,7 @@ class ThreadTests(unittest.TestCase):
print('program blocked; aborting') print('program blocked; aborting')
os._exit(2) os._exit(2)
t = threading.Thread(target=killer) t = threading.Thread(target=killer)
t.set_daemon(True) t.daemon = True
t.start() t.start()
# This is the trace function # This is the trace function
...@@ -437,7 +437,7 @@ class ThreadingExceptionTests(unittest.TestCase): ...@@ -437,7 +437,7 @@ class ThreadingExceptionTests(unittest.TestCase):
def test_daemonize_active_thread(self): def test_daemonize_active_thread(self):
thread = threading.Thread() thread = threading.Thread()
thread.start() thread.start()
self.assertRaises(RuntimeError, thread.set_daemon, True) self.assertRaises(RuntimeError, setattr, thread, "daemon", True)
def test_main(): def test_main():
......
...@@ -40,7 +40,7 @@ if __debug__: ...@@ -40,7 +40,7 @@ if __debug__:
if self._verbose: if self._verbose:
format = format % args format = format % args
format = "%s: %s\n" % ( format = "%s: %s\n" % (
current_thread().get_name(), format) current_thread().name, format)
_sys.stderr.write(format) _sys.stderr.write(format)
else: else:
...@@ -83,7 +83,7 @@ class _RLock(_Verbose): ...@@ -83,7 +83,7 @@ class _RLock(_Verbose):
owner = self._owner owner = self._owner
return "<%s(%s, %d)>" % ( return "<%s(%s, %d)>" % (
self.__class__.__name__, self.__class__.__name__,
owner and owner.get_name(), owner and owner.name,
self._count) self._count)
def acquire(self, blocking=1): def acquire(self, blocking=1):
...@@ -412,7 +412,7 @@ class Thread(_Verbose): ...@@ -412,7 +412,7 @@ class Thread(_Verbose):
def _set_daemon(self): def _set_daemon(self):
# Overridden in _MainThread and _DummyThread # Overridden in _MainThread and _DummyThread
return current_thread().is_daemon() return current_thread().daemon
def __repr__(self): def __repr__(self):
assert self._initialized, "Thread.__init__() was not called" assert self._initialized, "Thread.__init__() was not called"
...@@ -502,7 +502,7 @@ class Thread(_Verbose): ...@@ -502,7 +502,7 @@ class Thread(_Verbose):
# self. # self.
if _sys: if _sys:
_sys.stderr.write("Exception in thread %s:\n%s\n" % _sys.stderr.write("Exception in thread %s:\n%s\n" %
(self.get_name(), _format_exc())) (self.name, _format_exc()))
else: else:
# Do the best job possible w/o a huge amt. of code to # Do the best job possible w/o a huge amt. of code to
# approximate a traceback (code ideas from # approximate a traceback (code ideas from
...@@ -510,7 +510,7 @@ class Thread(_Verbose): ...@@ -510,7 +510,7 @@ class Thread(_Verbose):
exc_type, exc_value, exc_tb = self._exc_info() exc_type, exc_value, exc_tb = self._exc_info()
try: try:
print(( print((
"Exception in thread " + self.get_name() + "Exception in thread " + self.name +
" (most likely raised during interpreter shutdown):"), file=self._stderr) " (most likely raised during interpreter shutdown):"), file=self._stderr)
print(( print((
"Traceback (most recent call last):"), file=self._stderr) "Traceback (most recent call last):"), file=self._stderr)
...@@ -621,11 +621,13 @@ class Thread(_Verbose): ...@@ -621,11 +621,13 @@ class Thread(_Verbose):
finally: finally:
self._block.release() self._block.release()
def get_name(self): @property
def name(self):
assert self._initialized, "Thread.__init__() not called" assert self._initialized, "Thread.__init__() not called"
return self._name return self._name
def set_name(self, name): @name.setter
def name(self, name):
assert self._initialized, "Thread.__init__() not called" assert self._initialized, "Thread.__init__() not called"
self._name = str(name) self._name = str(name)
...@@ -638,11 +640,13 @@ class Thread(_Verbose): ...@@ -638,11 +640,13 @@ class Thread(_Verbose):
assert self._initialized, "Thread.__init__() not called" assert self._initialized, "Thread.__init__() not called"
return self._started.is_set() and not self._stopped return self._started.is_set() and not self._stopped
def is_daemon(self): @property
def daemon(self):
assert self._initialized, "Thread.__init__() not called" assert self._initialized, "Thread.__init__() not called"
return self._daemonic return self._daemonic
def set_daemon(self, daemonic): @daemon.setter
def daemon(self, daemonic):
if not self._initialized: if not self._initialized:
raise RuntimeError("Thread.__init__() not called") raise RuntimeError("Thread.__init__() not called")
if self._started.is_set(): if self._started.is_set():
...@@ -710,7 +714,7 @@ class _MainThread(Thread): ...@@ -710,7 +714,7 @@ class _MainThread(Thread):
def _pickSomeNonDaemonThread(): def _pickSomeNonDaemonThread():
for t in enumerate(): for t in enumerate():
if not t.is_daemon() and t.is_alive(): if not t.daemon and t.is_alive():
return t return t
return None return None
...@@ -863,7 +867,7 @@ def _test(): ...@@ -863,7 +867,7 @@ def _test():
counter = 0 counter = 0
while counter < self.quota: while counter < self.quota:
counter = counter + 1 counter = counter + 1
self.queue.put("%s.%d" % (self.get_name(), counter)) self.queue.put("%s.%d" % (self.name, counter))
_sleep(random() * 0.00001) _sleep(random() * 0.00001)
...@@ -888,7 +892,7 @@ def _test(): ...@@ -888,7 +892,7 @@ def _test():
P = [] P = []
for i in range(NP): for i in range(NP):
t = ProducerThread(Q, NI) t = ProducerThread(Q, NI)
t.set_name("Producer-%d" % (i+1)) t.name = "Producer-%d" % (i+1)
P.append(t) P.append(t)
C = ConsumerThread(Q, NI*NP) C = ConsumerThread(Q, NI*NP)
for t in P: for t in P:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment