[Python-ideas] 80 character line width vs. something wider
Cesare Di Mauro
cesare.dimauro at a-tono.com
Wed May 20 10:08:15 CEST 2009
Ben Finney <ben+python at benfinney.id.au> wrote:
> Aaron Rubin <aaron.rubin at 4dtechnology.com>
> writes:
>
>> Regarding this and the other notion of 5 or 6 being the proper level of
>> indentation:
>>
>> class a(object):
>> def method1(simulation=False):
>> try:
>> if simulation:
>> for x in range(10):
>> if x>5:
>> try:
>> # here might begin some actual math, with two or
>> three more levels of logic, interfacing with other libraries such as NumPy,
>> etc. where you might need specific error handling
>> except CustomError:
>> # customer error handling
>
> This is fairly clearly a contrived example. Can you give an example of
> actual in-use code which suffers from such deep indentation?
>
>From threading.py:
class Thread(_Verbose):
def __bootstrap_inner(self):
try:
self.__ident = _get_ident()
self.__started.set()
_active_limbo_lock.acquire()
_active[self.__ident] = self
del _limbo[self]
_active_limbo_lock.release()
if __debug__:
self._note("%s.__bootstrap(): thread started", self)
if _trace_hook:
self._note("%s.__bootstrap(): registering trace hook", self)
_sys.settrace(_trace_hook)
if _profile_hook:
self._note("%s.__bootstrap(): registering profile hook", self)
_sys.setprofile(_profile_hook)
try:
self.run()
except SystemExit:
if __debug__:
self._note("%s.__bootstrap(): raised SystemExit", self)
except:
if __debug__:
self._note("%s.__bootstrap(): unhandled exception", self)
# If sys.stderr is no more (most likely from interpreter
# shutdown) use self.__stderr. Otherwise still use sys (as in
# _sys) in case sys.stderr was redefined since the creation of
# self.
if _sys:
_sys.stderr.write("Exception in thread %s:\n%s\n" %
(self.name, _format_exc()))
else:
# Do the best job possible w/o a huge amt. of code to
# approximate a traceback (code ideas from
# Lib/traceback.py)
exc_type, exc_value, exc_tb = self.__exc_info()
try:
print>>self.__stderr, (
"Exception in thread " + self.name +
" (most likely raised during interpreter shutdown):")
print>>self.__stderr, (
"Traceback (most recent call last):")
while exc_tb:
print>>self.__stderr, (
' File "%s", line %s, in %s' %
(exc_tb.tb_frame.f_code.co_filename,
exc_tb.tb_lineno,
exc_tb.tb_frame.f_code.co_name))
exc_tb = exc_tb.tb_next
print>>self.__stderr, ("%s: %s" % (exc_type, exc_value))
# Make sure that exc_tb gets deleted since it is a memory
# hog; deleting everything else is just for thoroughness
finally:
del exc_type, exc_value, exc_tb
else:
if __debug__:
self._note("%s.__bootstrap(): normal return", self)
finally:
# Prevent a race in
# test_threading.test_no_refcycle_through_target when
# the exception keeps the target alive past when we
# assert that it's dead.
self.__exc_clear()
finally:
with _active_limbo_lock:
self.__stop()
try:
# We don't call self.__delete() because it also
# grabs _active_limbo_lock.
del _active[_get_ident()]
except:
pass
Cheers,
Cesare
More information about the Python-ideas
mailing list