Reload after an exception, not possible ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stef Mientki

    Reload after an exception, not possible ?

    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_Exceptio n:
    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
    UnboundLocalErr or: 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 ????

    please enlighten me,
    thanks,
    Stef Mientki



  • Steve Holden

    #2
    Re: Reload after an exception, not possible ?

    Stef Mientki wrote:
    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_Exceptio n:
    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
    UnboundLocalErr or: 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 ????
    >
    You have an assignment to JAL_simulation_ file elsewhere inside the same
    function, later in the code. This means that Python will assume that it
    is local to the function, but the import statement will affect the
    module namespace rather than the function namespace.

    In other words, your code is roughly equivalent to what follows, with
    the added complexity that the illegal reference isn't triggered until
    the second iteration of a loop.
    >>def f():
    .... print os
    .... import os
    .... os = "Something"
    ....
    >>f()
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "<stdin>", line 2, in f
    UnboundLocalErr or: local variable 'os' referenced before assignment
    >>>
    The solution? Don't assign to JAL_simulation_ file, and the function will
    use the local instead.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    --------------- Asciimercial ------------------
    Get on the web: Blog, lens and tag the Internet
    Many services currently offer free registration
    ----------- Thank You for Reading -------------

    Comment

    • Steve Holden

      #3
      Re: Reload after an exception, not possible ?

      Steve Holden wrote:
      [...]
      The solution? Don't assign to JAL_simulation_ file, and the function will
      use the local instead.
      >
      Sorry, that last "local" should have been "global".

      regards
      Steve
      --
      Steve Holden +1 571 484 6266 +1 800 494 3119
      Holden Web LLC/Ltd http://www.holdenweb.com
      Skype: holdenweb http://del.icio.us/steve.holden
      --------------- Asciimercial ------------------
      Get on the web: Blog, lens and tag the Internet
      Many services currently offer free registration
      ----------- Thank You for Reading -------------

      Comment

      • Stef Mientki

        #4
        Re: Reload after an exception, not possible ?

        Steve Holden wrote:
        Stef Mientki wrote:
        >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_Exceptio n:
        >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
        > UnboundLocalErr or: 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 ????
        >>
        You have an assignment to JAL_simulation_ file elsewhere inside the same
        function,
        No these are the only 2 uses of "JAL_simulation _file" in the whole project.

        later in the code. This means that Python will assume that it
        is local to the function, but the import statement will affect the
        module namespace rather than the function namespace.
        >
        In other words, your code is roughly equivalent to what follows, with
        the added complexity that the illegal reference isn't triggered until
        the second iteration of a loop.
        >
        >>def f():
        ... print os
        ... import os
        ... os = "Something"
        ...
        >>f()
        Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        File "<stdin>", line 2, in f
        UnboundLocalErr or: local variable 'os' referenced before assignment
        >>>
        >
        The solution? Don't assign to JAL_simulation_ file, and the function will
        use the local instead.
        >
        The solution is much simpler, but I don't understand why the "import",
        which really reloads the changed code in JAL_simulation_ file,
        works the second time :-(

        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_Exceptio n:
        11 JALsPy_globals. State = SS_Halt

        cheers,
        Stef Mientki

        Comment

        Working...