def Statement(self,c): n = self.t.checkNext() if (n == ";"): self.t.getNext() return elif (n == "{"): self.t.getNext() self.ParseBlock(c) return elif (n == "identifier"): self.t.getNext() if self.t.data == "if": self.ParseIf(c) return elif self.t.data == "for": self.ParseFor(c) return else: name = self.t.data if self.t.checkNext() == "(": self.t.getNext() arg = variable.variable() if self.t.checkNext() == ")": self.t.getNext() arg.pushcode(code.push_null()) else: self.Expression(arg, ")") if self.t.checkNext() == "{": self.t.getNext() arg.pushcode(code.argument()) self.ParseBlock(arg) c.index(name).assignment(arg) return c.pushcode(code.pop()) c.pushcode(code.variable(name)) c.x.code.c += arg.x.code.c c.pushcode(code.call()) else: c.pushcode(code.pop()) c.pushcode(code.variable(name)) self.getSuff(c) self.Expression(c, ";", True) return c.pushcode(code.pop()) self.Expression(c, ";")
def _executeList(code_list, args, local_ns): ''' Given a list where each element consists of a stringified Python statement, each line will be executed except for the very last line. If the final line contains the string "raise", this line will also be executed directly. Otherwise it is assumed the line should be executed by the Python eval function and the result of this will be returned. Parameters: - codeList is a Python List of strings which are executed one after another without doing any sanity checks. The last string of this list should either contain a "raise ..." statement where the exception being thrown is derived from an IDL exception OR the last string should represent some return value to be evaluated by the Python "eval" function. Additionally, the last "string" can actually be a Python object which will simply be returned instead of being evaluated by the "eval" function. A sample value for code could be [ "import CORBA", "CORBA.TRUE" ] or [ "import ACSExceptionCommon", "raise ACSExceptionCommon.CommonExImpl()" ] - local_ns is the local namespace Returns: the last string in code evaluated by the Python eval statement if it does not contain the substring "raise " within it. If the last "string" is not really a string but instead some other Python object, this is returned instead. Raises: the last string in code if it does contain the substring "raise " within it. ''' #force a copy of it _locals = copy(local_ns) #_locals = local_ns #extend it _locals['parameters'] = args #well...through the API the developer could have just specified a function... if isfunction(code_list): #great, this makes things much easier for us try: #assume they want to see the arguments return code_list(args) except: print_exc() #fine...it's a parameterless function return code_list() elif type(code_list)==types.CodeType: exec code_list in globals(), _locals exec "joe = stringFunction(parameters)" in globals(), _locals return _locals['joe'] else: #to make sure the last value isn't really popped off code = copy(code_list) #pop off the final return value final_val = code.pop() # execute all lines left in the list code_str = "" for line in code: code_str += line + '\n' try: exec code_str in globals(), _locals except: LOGGER.logWarning("Failed to execute the simulation code!:") print_exc() # (RH) I don't know why code was executed line by line here. This prevent to # execute multiline statements. # #execute each line still left in the list # for line in code: # try: # exec line in globals(), _locals # except: # LOGGER.logWarning("Failed to execute the '" + line + "' statement!") #if there's a raise in the last line, obviously the developer is trying #to simulate an exception. in this case the line should be exec'ed instead #of eval'ed if type(final_val)==str and final_val.count("raise ") == 1: #it's OK to do this without a try/except because the end-user is #almost certainly TRYING to throw an exception. exec final_val in globals(), _locals #there was a complaint about not seeing output from commandcenter so we do this:( stdout.flush() if type(final_val)==str and final_val.count("return ") == 1: return eval(final_val.replace("return ", ""), globals(), _locals) if type(final_val)==str and final_val == "return": return None #the final line is the actual return value which must be evaluated try: return eval(final_val, globals(), _locals) except: #this is only a debug message because the finalVal may already be a Python object LOGGER.logDebug("Failed to evaluate the '" + str(final_val) + "' statement!") return final_val
def _executeList(code_list, args, local_ns): ''' Given a list where each element consists of a stringified Python statement, each line will be executed except for the very last line. If the final line contains the string "raise", this line will also be executed directly. Otherwise it is assumed the line should be executed by the Python eval function and the result of this will be returned. Parameters: - codeList is a Python List of strings which are executed one after another without doing any sanity checks. The last string of this list should either contain a "raise ..." statement where the exception being thrown is derived from an IDL exception OR the last string should represent some return value to be evaluated by the Python "eval" function. Additionally, the last "string" can actually be a Python object which will simply be returned instead of being evaluated by the "eval" function. A sample value for code could be [ "import CORBA", "CORBA.TRUE" ] or [ "import ACSExceptionCommon", "raise ACSExceptionCommon.CommonExImpl()" ] - local_ns is the local namespace Returns: the last string in code evaluated by the Python eval statement if it does not contain the substring "raise " within it. If the last "string" is not really a string but instead some other Python object, this is returned instead. Raises: the last string in code if it does contain the substring "raise " within it. ''' #force a copy of it _locals = copy(local_ns) #_locals = local_ns #extend it _locals['parameters'] = args #well...through the API the developer could have just specified a function... if isfunction(code_list): #great, this makes things much easier for us try: #assume they want to see the arguments return code_list(args) except: print_exc() #fine...it's a parameterless function return code_list() elif type(code_list) == types.CodeType: exec code_list in globals(), _locals exec "joe = stringFunction(parameters)" in globals(), _locals return _locals['joe'] else: #to make sure the last value isn't really popped off code = copy(code_list) #pop off the final return value final_val = code.pop() # execute all lines left in the list code_str = "" for line in code: code_str += line + '\n' try: exec code_str in globals(), _locals except: LOGGER.logWarning("Failed to execute the simulation code!:") print_exc() # (RH) I don't know why code was executed line by line here. This prevent to # execute multiline statements. # #execute each line still left in the list # for line in code: # try: # exec line in globals(), _locals # except: # LOGGER.logWarning("Failed to execute the '" + line + "' statement!") #if there's a raise in the last line, obviously the developer is trying #to simulate an exception. in this case the line should be exec'ed instead #of eval'ed if type(final_val) == str and final_val.count("raise ") == 1: #it's OK to do this without a try/except because the end-user is #almost certainly TRYING to throw an exception. exec final_val in globals(), _locals #there was a complaint about not seeing output from commandcenter so we do this:( stdout.flush() if type(final_val) == str and final_val.count("return ") == 1: return eval(final_val.replace("return ", ""), globals(), _locals) if type(final_val) == str and final_val == "return": return None #the final line is the actual return value which must be evaluated try: return eval(final_val, globals(), _locals) except: #this is only a debug message because the finalVal may already be a Python object LOGGER.logDebug("Failed to evaluate the '" + str(final_val) + "' statement!") return final_val