def set_symbol(self,name,value): """ Set """ #self.data[name]= value self.data['__tmp__'] = value s = "%s = __tmp__" % name eval.do_exec(s,self.data) del self.data['__tmp__']
def add_symbol(self,name,value): """ Add """ #self.data.update({name:value}) self.data['__tmp__'] = value s = "%s = __tmp__" % name eval.do_exec(s,self.data) del self.data['__tmp__']
def del_symbol(self,name): """ Delete """ #return self.data.pop(name) #del self.data[name] if self.has_symbol(name): s = "del %s" % name eval.do_exec(s,self.data) return
def get_symbol(self,name): """ Get """ s = "try:\n" s = s+ " __tmp__ = %s\n" % name s = s+ "except:\n" s = s +" __tmp__ = None\n" eval.do_exec(s,self.data) tmp = self.data.get('__tmp__') del self.data['__tmp__'] return tmp
def execute(self,arg,**kws): """ Execute an argument return 0 if command is completed return 1 if need more input Note if USE_CODE = True its difficult to catch exceptions. Here we are letting the caller handle them. Note that the caller must clear the buffer on an exception! """ ########### # This bit wont raise an exception #if USE_CODE: # ret = self.console.push(arg) # if ret == True: # return 1 # else: # return 0 ########### # Use below which follows from python/lib/code.py # Note the caller must clear the buffer # when there is an exception! if USE_CODE: # append to buffer self.console.buffer.append(arg) source = "\n".join(self.console.buffer) #print source # Try to compile the code # if its incomplete return 1 cd = self.console.compile(source, filename="<shell input>", symbol="single") if cd is None: return 1 # If its complete run it #exec cd in self.console.locals ret = eval.do_exec(cd,self.symbol_table.data) self.console.resetbuffer() return 0 else: try: ret = eval.do_exec(arg,self.symbol_table.data) return 0 except: return 2