[Python-Dev] What exception should Thread.start() raise?
Guido van Rossum
guido at python.org
Mon Jun 4 22:33:11 CEST 2007
On 6/4/07, Steven Bethard <steven.bethard at gmail.com> wrote:
> On 6/4/07, BJörn Lindqvist <bjourne at gmail.com> wrote:
> > The threading module contains buggy code:
> > class Thread(_Verbose):
> > ...
> > def start(self):
> > assert self.__initialized, "Thread.__init__() not called"
> > assert not self.__started, "thread already started"
> > ...
> > If you run such code with python -O, weird stuff may happen when you
> > call mythread.start() multiple times. -O removes assert statements so
> > the code won't fail with an AssertionError which would be expected.
> > So what real exception should Thread.start() raise? I have suggested
> > adding an IllegalStateError modelled after java's
> > IllegalStateException, but that idea was rejected. So what exception
> > should be raised here, is it a RuntimeError?
> If you want to be fully backwards compatible, you could just write this like::
> def start(self):
> if not self.__initialized:
> raise AssertionError("Thread.__init__() not called")
> if self.__started:
> raise AssertionError("thread already started")
> But I doubt anyone is actually catching the AssertionError, so
> changing the error type would probably be okay.
Anything that causes an "assert" to fail is technically using
"undefined" behavior. I am in favor of changing this case to
RuntimeError, which is the error Python usually uses for state
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-Dev