Ticket #29 (closed defect: invalid)

Opened 4 years ago

Last modified 5 months ago

Global variable mutation in functions

Reported by: jwienke@techfak.uni-bielefeld.de Assigned to:
Priority: moderate Keywords: global variables, evaluation
Cc:

Description

I've noticed a problem concerning the (re-)evaluation of global variables. Reinteract seems to be unaware of the global variables. For example begin with the following code:

globals = {'count': 0}
def doIt():
    global globals
    globals['count'] = globals['count'] + 1

doIt()
globals['count']

The output of the last line is 1, which is correct.

Now you erase the line with the call to doIt(). Reinteract only marks the last line with pink. If you reevaluate the code the output from the last line then is still 1, which is obviously wrong.

Change History

08/10/08 21:59:49 changed by otaylor

  • priority changed from high to moderate.
  • summary changed from Wrong evaluation with global variables to Global variable mutation in functions.

This is quite hard to handle well. A full solution would involve:

  • Tagging functions with the globals they mutate
  • Scanning through each line executed for the functions it calls (directly or indirectly)
  • Treating those mutations as we do direct variable mutations (make a copy of the mutated variable)

For now, what I've done is made the use of the global statement an error.

http://git.fishsoup.net/cgit/reinteract/commit/?id=a6f3b60aea87ec28104397c1afa6c74be344aa20

This obviously does cause issues for harmless uses of global like, say;

four_pi = 4 * pi
def mutiply_by_four_pi(x):
    global four_pi
    return x * four_pi

But using 'global' in these cases is not necessary. More sophisticated analysis to distinguish uses of 'global' that modify global variables should be possible, if maybe a little tricky.

09/17/11 14:26:45 changed by otaylor

  • status changed from new to closed.
  • resolution set to invalid.

Moved to https://bugzilla.gnome.org/show_bug.cgi?id=659334 - closing as "invalid"