Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #80608
| References | <CAPTjJmpVVfPzwrbeornk9Txyfiz+H5MDfHG2nY_9va7RCUEmEw@mail.gmail.com> <CAPTjJmrhz7crC2_x+ZN9sLgPvd8B1Yn8skzMSTBXJ6gSj55TqQ@mail.gmail.com> |
|---|---|
| Date | 2014-11-12 09:56 -0500 |
| Subject | Re: __missing__ for the top-level Python script |
| From | Skip Montanaro <skip.montanaro@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.15724.1415804230.18130.python-list@python.org> (permalink) |
> No bites? I'd have thought there'd be a few crazy ideas thrown out in
> answer to this.
I was on vacation for a few days, so haven't been all that attentive
to my mail. I have an autoload module which does something similar
(note the Python 2.x syntax):
import sys, inspect, traceback, re
def autoload_exc(ty, va, tb):
mat = re.search("name '([^']*)' is not defined", va.args[0])
if mat is not None:
modulename = mat.group(1)
print >> sys.stderr, "autoloading", modulename
f_locals = tb.tb_frame.f_locals
f_globals = tb.tb_frame.f_globals
exec "import " + modulename in f_locals, f_globals
exec tb.tb_frame.f_code in f_locals, f_globals
else:
traceback.print_exception(ty, va, tb)
sys.excepthook = autoload_exc
which works about as you'd expect:
>>> sys.excepthook
<function autoload_exc at 0x1a5e140>
>>> math.sin(42)
autoloading math
-0.9165215479156338
>>> string.uppercase
autoloading string
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
I can't see a lot of people wanting this (I normally have its import
commented out in my PYTHONSTARTUP file), and I think it would probably
be bad practice for new users of the language.
Skip
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: __missing__ for the top-level Python script Skip Montanaro <skip.montanaro@gmail.com> - 2014-11-12 09:56 -0500
csiph-web