halting execution of all modules

piet at cs.uu.nl piet at cs.uu.nl
Fri Sep 15 07:18:20 EDT 2000


>>>>> "Andy Beall" <beall at psych.ucsb.edu> (AB) writes:

AB> I'm trying to figure out how to halt a module during its execution so that
AB> under certain conditions all lines of code after a certain point do not get
AB> run.  Right now I'm doing this by introducing an unhandled NameError; this
AB> causes the interpreter to stop wherever it was and return but it's an ugly
AB> way to do this.  I'm trying to make a module for others to "embed" in their
AB> own modules.  I need my module to be able to halt the "embedding" module
AB> right at the point they called a certain function from within my module.
AB> The "foo" example below is the only way I've figured out to do this:


AB> #Embedding module
AB> import foo

AB> foo.go(1)
AB> print 'Hello...'

AB> foo.go(0)
AB> print 'Execution halted above: This line should not get printed'


AB> # Embedded module
AB> #foo.py--------------------------------------------------

AB> def go(continue):

AB>     if continue:
AB>         return
AB>     else:
AB>         unknown_symbol_to_cause_an_error

continue isn't an identifier!!

AB> Due to backward compatibility needs of a large project, I can't simply have
AB> foo.go() return a flag for deciding whether to continue or not.

IMHO the best to do would be to raise your own exception.
Otherwise I would use an assert 0:

# Embedded module
#foo.py--------------------------------------------------

def go(cont):

    if cont:
        return
    else:
        assert 0, "Sorry"

-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.van.Oostrum at hccnet.nl



More information about the Python-list mailing list