[Tutor] RE: reset and IDE

Christopher Smith csmith@blakeschool.org
Sat, 02 Feb 2002 16:45:39 -0600


just@letterror.com writes:
>Christopher Smith wrote:
>
>> 1) If I wanted to build a copy of the IDE with this as the default
>> behavior, where do I have to insert this code?  (I was unable to find
>> where a press on the "Run All" button takes you in terms of program
>flow.)
>
>Have a look at PyEdit.py, the run() method of the Editor class.

Thanks for the pointer, Just.

Here's what I've come up with so far.  In the _run function I
have inserted a modification of Jesse W's reset code (as
advertised on tutor). It implements a total reload of modules,
whether they have been changed or not.  I am not yet sure how to
find out which ones have been changed and actually need
reloading.  In addition, I have preserved Jesse's user_safe_items
option which allows you to specify things that shouldn't be
reloaded. I'm not sure at this point why that might be needed so
maybe the best bet is to leave it out.

One of the problems it has right now is that the user_safe_items
are only paid attention to the *2nd* time that the script is run.
 For example, let's say you ran the script with imported module A
and you didn't have any user_safe_items defined.  Then you make
modifications to A but don't want those changes read so you add
the line

user_safe_items=['A']

to your script.  When you run it, the present modifications to
the _run definition don't honor that change to the script so
module A is re-read anyway.  It won't be re-read the next time
you run the script, though. This might be because until the code
is executed, this new variable doesn't yet exist in the
namespace.  I think this is why the reverse happens, too.  If you
now remove the user_safe_items line from your script, module A
won't be read the first time, but it will be read the next time
the script is run.

Does anyone have some thoughts on this?  What might someone *not*
want wiped out between runs?

Is it worth figuring out which modules actually need reloading or
not? Perhaps one could fund out which modules actually need
reloading by checking the modification dates on the .py and the
.pyc files and see if the .py is newer and only then reload the
module.  

HERE'S WHAT IT CATCHES NOW
--------------------------
#x=1 was changed to y=1 but print x was not changed;
#x is flagged as not being defined
#y=1
#print x

#print 'f' in function f was changed to print 'g';
#it does so correctly
#def f():
#	print 'g'
#f()

#a module was imported and then changed; the modified
#module is re-loaded and shows the changed behavior
import ext
ext.ex()

HERE'S THE MODIFICATION TO THE _run DEFINITION
----------------------------------------------

	def _run(self):
		if self.run_with_interpreter:
			if self.editgroup.editor.changed:
				import EasyDialogs
				import Qd; Qd.InitCursor()
				save = EasyDialogs.AskYesNoCancel('Save "%s" before running?' %
self.title, 1)
				if save > 0:
					if self.domenu_save():
						return
				elif save < 0:
					return
			if not self.path:
				raise W.AlertError, "Can't run unsaved file"
			self._run_with_interpreter()
		else:
			pytext = self.editgroup.editor.get()
			globals, file, modname = self.getenvironment()
			#-------cps addition (note: the namespace is called "globals")
			safe_items=['__builtins__', '__doc__', '__file__', '__name__']
			if 'user_safe_items' in globals.keys():
				safe_items+=globals['user_safe_items']

			for item in globals.keys():
				if item not in safe_items:
					if str(type(globals[item]))=="<type 'module'>":
						exec "reload("+item+")" in globals
					else:
						exec ("del "+item) in globals 
			#-------end cps addition
			self.execstring(pytext, globals, globals, file, modname)


--------------

/c