Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80608 > unrolled thread

Re: __missing__ for the top-level Python script

Started bySkip Montanaro <skip.montanaro@gmail.com>
First post2014-11-12 09:56 -0500
Last post2014-11-12 09:56 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: __missing__ for the top-level Python script Skip Montanaro <skip.montanaro@gmail.com> - 2014-11-12 09:56 -0500

#80608 — Re: __missing__ for the top-level Python script

FromSkip Montanaro <skip.montanaro@gmail.com>
Date2014-11-12 09:56 -0500
SubjectRe: __missing__ for the top-level Python script
Message-ID<mailman.15724.1415804230.18130.python-list@python.org>
> 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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web