bug/poor tracebacks while importing code

Mark McEahern marklists at mceahern.com
Wed Oct 2 20:06:41 CEST 2002


> call me stupid if you wish, but it was a genuine query. And thanks
> print_stack does better at locating things when -O is off.

Not stupid at all and you're quite welcome.  Maybe I over-reacted to the
word "poor"--it was my sense that you were criticizing Python unfairly for
something that was fairly esoteric without really explaining why you weren't
using the more obvious idiom:

# a.py

# I won't mention that it seems unwise to have a library (b)
# do stuff.  Oops, I guess I did.  <wink>
import b

# b.py

def raise_an_unexpected_error():
  try:
    x = 1/0
  # I think your original code had an unspecified except,
  # generally eschewed in favor of being explicit.
  except ValueError, e:
    pass

# Why are you doing stuff in a library?
foo()

##

Output:

$ python a.py
Traceback (most recent call last):
  File "a.py", line 7, in ?
    foo()
  File "a.py", line 3, in foo
    x = 1/0
ZeroDivisionError: integer division or modulo by zero

##

In other words, why jump through all the hoops with importing sys and
traceback only in an effort to mimic what Python would have done if you
followed more common idioms?  And then criticize this as a bug in Python
and/or the documentation?

What's that saying about bears, honey, and fruit?  I can't remember.

Of course, maybe your question is not so much how to print the traceback to
stderr as it is how to stuff it into a file-like object?

Cheers,

// mark





More information about the Python-list mailing list