Reload after an exception not possible ?

Tal Einat wrote:

  

hello,

I posted this question in the normal Python newsgroup,
because I think it's a normal Python question,
but I'm afraid that these kinds of constructs are only used in GUI.
So maybe here someone can give the answer.
    

hello,

I've a graphical application (wxPython),
where the code in the main GUI loop is given below.

1 JAL_Loaded = False
2 while len(App_Running) > 0:
3 if JALsPy_globals.State == SS_Run:
4 try:
5 if JAL_Loaded:
6 reload ( JAL_simulation_file )
7 else:
8 JAL_Loaded = True
9 import JAL_simulation_file
10 except JALsPy_globals.Reload_Exception:
11 JALsPy_globals.State = SS_Halt

The first time the while loop is entered,
JAL_Loaded is False (line 1),
and when I press some button,
the code will enter at line 8,
importing a module, that has executable code with an infinite loop.

To exit this infinite loop, an exception is generated (by a button
press),
and program comes back in the above while-loop line (10,11,2).

So far so good.

Then I change the code in the imported file,
and when I start the engine again,
the flag JAL_Loaded is True, so I don't import,
but I reload the file.

Now I get the next exception
   UnboundLocalError: local variable 'JAL_simulation_file' referenced
before assignment

So obviously I can't reload but have to do an import again
(which makes the code much simpler
but I don't understand why the reload raises an exception ???

thanks,
Stef Mientki
    
Have you tried declaring JAL_simulation_file as global? I wonder if
that would work...
  

No,
but the solution is much simpler

2 while len(App_Running) > 0:
3 if JALsPy_globals.State == SS_Run:
4 try:
9 import JAL_simulation_file
10 except JALsPy_globals.Reload_Exception:
11 JALsPy_globals.State = SS_Halt

The second (and next) time I do the import at line 9,
it reloads the changed file perfectly.
But I don't understand it :wink:

cheers,
Stef

···

On 8/1/07, Stef Mientki <s.mientki@ru.nl> wrote: