THE answer?

Cedric Adjih adjih at crepuscule.com
Thu May 4 02:29:01 EDT 2000


mdefreitas at sikorsky.com wrote:

>> I think we need to send PyRun_File a COPY of the global
>> dictionary, no? If so, how do we create a copy of a dictionary.
>> I couldn't find anything like that in C/API document..

> OK, forget that... I think I have the real answer here... rather than
> copy the global dictionary, I'll just create one with the necessary
> elements for each recursive call. This is the final answer which seems
> to work:
> [...solution...]

Ok, it looks like __builtins__ is the only thing required (?).
Thanks for the info.

Note that you could have written part of this in Python (same for handling
arguments), with something like this (not checked):

def runScript(commandAsString):
	argList=string.split(commandAsString)
	fileName=argList[0]
	newLocalDict={ "argList": argList[1:] }
	newGlobalDict={ "__builtins__": __builtins__ }
	try:
		result=execfile(fileName, newGlobalDict, newLocalDict)
	except:
		#XXX:TODO: handle/print traceback here.
		return None
	return result

It's easier in Python :-)

> This works, and each recursive call to the interpreter has a fresh set
> of global vars. You will note the lack of error checking and memory
> cleanup. Here is my more complicated version... Is my error checking OK?

Probably...

-- Cedric



More information about the Python-list mailing list