def put_output(self, data, uid): """put some output into the pipe""" #apply before_output hook first data = interface.plugin['services'].apply_io_hook('ANY', 'before_output', data) data = interface.plugin['services'].apply_io_hook(uid, 'before_output', data) if data == "": return data = data.replace('"', '"') pdata = data.replace("\\", "\\\\") pdata = data.replace("\n", "\\n") pdata = pdata.replace("\r", "\\r") debug_msg("pdata = "+ pdata, 4) if python_version < 3: try: pdata = pdata.decode('utf-8') except: debug_msg(' Crunchy Error in trying to decode inside cometIO.py') debug_msg(' The likely cause is trying to print a unicode string prefixed by u') debug_msg(' as in u"...". If not, please file a bug report.') self.lock.acquire() pageid = uid.split("_")[0] username = names[pageid] debug_msg("username = %s in CrunchyIOBuffer.put_output"%username, 5) if self.data.endswith('";//output\n'): self.data = self.data[:-11] + '%s";//output\n' % (pdata) # Saving session; appending from below if uid in config[username]['logging_uids']: log_id = config[username]['logging_uids'][uid][0] config[username]['log'][log_id].append(data) utilities.log_session(username) self.event.set() elif self.help_flag == True: self.put(show_help_js) pdata = pdata.replace("class='%s'"%interface.generic_output, "class='help_menu'") # use jQuery: self.put("""$("#help_menu").html("%s");\n""" % (pdata)) self.help_flag = False else: #use jQuery: self.put("""$("#out_%s").append("%s");//output\n""" % (uid, pdata)) # Saving session; first line... if uid in config[username]['logging_uids']: log_id = config[username]['logging_uids'][uid][0] config[username]['log'][log_id].append(data) utilities.log_session(username) self.lock.release()
def run(self): """run the code, redirecting stdout, stderr, stdin and returning the string representing the output """ sys.stdin.register_thread(self.channel) sys.stdout.register_thread(self.channel) sys.stderr.register_thread(self.channel) try: try: self.ccode = compile(self.code, "User's code", 'exec') except: try: if self.friendly: sys.stderr.write(errors.simplify_traceback(self.code, self.username)) else: traceback.print_exc() return except: sys.stderr.write("Recovering from internal error in Interpreter.run()") sys.stderr.write("self.channel =%s"%self.channel) return if not self.ccode: #code does nothing return try: # logging the user input first, if required if self.username and self.channel in config[self.username]['logging_uids']: vlam_type = config[self.username]['logging_uids'][self.channel][1] if vlam_type == 'editor': user_code = self.code.split("\n") log_id = config[self.username]['logging_uids'][self.channel][0] if user_code: user_code = '\n'.join(user_code) if not user_code.endswith('\n'): user_code += '\n' else: user_code = _("# no code entered by user\n") data = "<span class='stdin'>" + user_code + "</span>" config[self.username]['log'][log_id].append(data) log_session(username) exec_code(self.ccode, self.symbols, source=None, username=self.username) #exec self.ccode in self.symbols#, {} # note: previously, the "local" directory used for exec # was simply an empty directory. However, this meant that # module names imported outside a function definition # were not available inside that function. This is why # we have commented out the {} as a reminder; self.symbols # will be used for holding both global and local variables. except: try: if self.friendly: sys.stderr.write(errors.simplify_traceback(self.code, self.username)) else: traceback.print_exc() except: sys.stderr.write("Recovering from internal error in Interpreter.run()") sys.stderr.write(".. after trying to call exec_code.") sys.stderr.write("self.channel = %s"%self.channel) finally: if self.doctest: # attempting to log if self.username and self.channel in config[self.username]['logging_uids']: code_lines = self.code.split("\n") user_code = [] for line in code_lines: # __teststring identifies the beginning of the code # that is passed to a doctest (see vlam_doctest.py) # This will have been appended to the user's code. if line.startswith("__teststring"): break user_code.append(line) log_id = config[self.username]['logging_uids'][self.channel][0] if user_code: user_code = '\n' + '\n'.join(user_code) if not user_code.endswith('\n'): user_code += '\n' else: user_code = _("# no code entered by user\n") # separating each attempts user_code = "\n" + "- "*25 + "\n" + user_code data = "<span class='stdin'>" + user_code + "</span>" config[self.username]['log'][log_id].append(data) log_session(self.username) # proceed with regular output if self.friendly: message, success = errors.simplify_doctest_error_message( self.doctest_out.getvalue()) if success: sys.stdout.write(message) else: sys.stderr.write(message) else: sys.stdout.write(self.doctest_out.getvalue()) sys.stdin.unregister_thread() sys.stdout.unregister_thread() sys.stderr.unregister_thread()
def run(self): """run the code, redirecting stdout, stderr, stdin and returning the string representing the output """ sys.stdin.register_thread(self.channel) sys.stdout.register_thread(self.channel) sys.stderr.register_thread(self.channel) try: try: self.ccode = compile(self.code, "User's code", "exec") except: try: if self.friendly: sys.stderr.write(errors.simplify_traceback(self.code, self.username)) else: traceback.print_exc() return except: sys.stderr.write("Recovering from internal error in Interpreter.run()") sys.stderr.write("self.channel =%s" % self.channel) return if not self.ccode: # code does nothing return try: # logging the user input first, if required if self.username and self.channel in config[self.username]["logging_uids"]: vlam_type = config[self.username]["logging_uids"][self.channel][1] if vlam_type == "editor": user_code = self.code.split("\n") log_id = config[self.username]["logging_uids"][self.channel][0] if user_code: user_code = "\n".join(user_code) if not user_code.endswith("\n"): user_code += "\n" else: user_code = _("# no code entered by user\n") data = "<span class='stdin'>" + user_code + "</span>" config[self.username]["log"][log_id].append(data) log_session(username) exec_code(self.ccode, self.symbols, source=None, username=self.username) # exec self.ccode in self.symbols#, {} # note: previously, the "local" directory used for exec # was simply an empty directory. However, this meant that # module names imported outside a function definition # were not available inside that function. This is why # we have commented out the {} as a reminder; self.symbols # will be used for holding both global and local variables. except: try: if self.friendly: sys.stderr.write(errors.simplify_traceback(self.code, self.username)) else: traceback.print_exc() except: sys.stderr.write("Recovering from internal error in Interpreter.run()") sys.stderr.write(".. after trying to call exec_code.") sys.stderr.write("self.channel = %s" % self.channel) finally: if self.doctest: # attempting to log if self.username and self.channel in config[self.username]["logging_uids"]: code_lines = self.code.split("\n") user_code = [] for line in code_lines: # __teststring identifies the beginning of the code # that is passed to a doctest (see vlam_doctest.py) # This will have been appended to the user's code. if line.startswith("__teststring"): break user_code.append(line) log_id = config[self.username]["logging_uids"][self.channel][0] if user_code: user_code = "\n" + "\n".join(user_code) if not user_code.endswith("\n"): user_code += "\n" else: user_code = _("# no code entered by user\n") # separating each attempts user_code = "\n" + "- " * 25 + "\n" + user_code data = "<span class='stdin'>" + user_code + "</span>" config[self.username]["log"][log_id].append(data) log_session(self.username) # proceed with regular output if self.friendly: message, success = errors.simplify_doctest_error_message(self.doctest_out.getvalue()) if success: sys.stdout.write(message) else: sys.stderr.write(message) else: sys.stdout.write(self.doctest_out.getvalue()) sys.stdin.unregister_thread() sys.stdout.unregister_thread() sys.stderr.unregister_thread()