def get(self): self.response.headers['Content-Type'] = 'text/plain' # extract the statement to be run statement = self.request.get('statement') if not statement: return # the python compiler doesn't like network line endings statement = statement.replace('\r\n', '\n') # add a couple newlines at the end of the statement. this makes # single-line expressions such as 'class Foo: pass' evaluate happily. statement += '\n\n' lol = self.request.get('lol') if lol == '1': statement = lolpython.to_python(statement) import sys as _lol_sys self.response.out.write(statement) # log and compile the statement up front try: logging.info('Compiling and evaluating:\n%s' % statement) compiled = compile(statement, '<string>', 'single') except: self.response.out.write(traceback.format_exc()) return # create a dedicated module to be used as this statement's __main__ statement_module = new.module('__main__') # use this request's __builtin__, since it changes on each request. # this is needed for import statements, among other things. import __builtin__ statement_module.__builtins__ = __builtin__ # load the session from the datastore session = Session.get(self.request.get('session')) # swap in our custom module for __main__. then unpickle the session # globals, run the statement, and re-pickle the session globals, all # inside it. old_main = sys.modules.get('__main__') try: sys.modules['__main__'] = statement_module statement_module.__name__ = '__main__' # re-evaluate the unpicklables for code in session.unpicklables: exec code in statement_module.__dict__ # re-initialize the globals for name, val in session.globals_dict().items(): try: statement_module.__dict__[name] = val except: msg = 'Dropping %s since it could not be unpickled.\n' % name self.response.out.write(msg) logging.warning(msg + traceback.format_exc()) session.remove_global(name) # run! old_globals = dict(statement_module.__dict__) try: old_stdout = sys.stdout old_stderr = sys.stderr try: sys.stdout = self.response.out sys.stderr = self.response.out exec compiled in statement_module.__dict__ finally: sys.stdout = old_stdout sys.stderr = old_stderr except: self.response.out.write(traceback.format_exc()) return # extract the new globals that this statement added new_globals = {} for name, val in statement_module.__dict__.items(): if name not in old_globals or val != old_globals[name]: new_globals[name] = val if True in [isinstance(val, UNPICKLABLE_TYPES) for val in new_globals.values()]: # this statement added an unpicklable global. store the statement and # the names of all of the globals it added in the unpicklables. session.add_unpicklable(statement, new_globals.keys()) logging.debug('Storing this statement as an unpicklable.') else: # this statement didn't add any unpicklables. pickle and store the # new globals back into the datastore. for name, val in new_globals.items(): if not name.startswith('__'): session.set_global(name, val) finally: sys.modules['__main__'] = old_main session.put()
def get(self): # extract the statement to be run first_time = False user=users.get_current_user() query=ShellUser.all() query.filter("account = ", user) db_user = query.get() statement = self.request.get('statement') if not statement: return # the python compiler doesn't like network line endings statement = statement.replace('\r\n', '\n') is_mobile = False uagent = self.request.user_agent.lower() # add a couple newlines at the end of the statement. this makes # single-line expressions such as 'class Foo: pass' evaluate happily. statement += '\n' if ("mobi" in uagent) or ("mini" in uagent): is_mobile = True mobile_template=[os.path.abspath('../site/mobile.part.html'),os.path.abspath('../site/mobile.part2.html')] tidbit = '</div><form name="shell" action="shell.do" method="get"><textarea id="prompt" name="statement">#Type Away!</textarea><input id="shellSessionId" type="hidden" name="session" value="' lol = self.request.get('lol') if lol == '1': statement = lolpython.to_python(statement) import sys as _lol_sys statement=statement.strip() reply='' chat=(''.join([statement,'#']).split('#')[1]) self.response.clear() if is_mobile: self.response.headers['Content-Type'] = 'text/html' self.response.out.write(open(mobile_template[0]).read()) highlighter.Parser(statement,self.response.out).format() else: self.response.headers['Content-Type'] = 'text/text' self.response.out.write(statement) statement = statement.split('#')[0] #TODO: change this. reply=responder(statement,chat,db_user) # log and compile the statement up front try: if statement.strip(): compiled = compile(statement, '<string>', 'single') except: self.response.out.write(traceback.format_exc()) self.response.out.write(responder('some stupid error','',db_user)) return # create a dedicated module to be used as this statement's __main__ statement_module = new.module('__main__') # use this request's __builtin__, since it changes on each request. # this is needed for import statements, among other things. import __builtin__ statement_module.__builtins__ = __builtin__ # load the session from the datastore session_id = self.request.get('session') session = Session.get(session_id) # swap in our custom module for __main__. then unpickle the session # globals, run the statement, and re-pickle the session globals, all # inside it. old_main = sys.modules.get('__main__') try: sys.modules['__main__'] = statement_module statement_module.__name__ = '__main__' # re-evaluate the unpicklables for code in session.unpicklables: exec code in statement_module.__dict__ # re-initialize the globals for name, val in session.globals_dict().items(): try: statement_module.__dict__[name] = val except: msg = 'Dropping %s since it could not be unpickled.\n' % name self.response.out.write(msg) logging.warning(msg + traceback.format_exc()) session.remove_global(name) # run! old_globals = dict(statement_module.__dict__) try: old_stdout = sys.stdout old_stderr = sys.stderr try: sys.stdout = self.response.out sys.stderr = self.response.out if statement.strip(): exec compiled in statement_module.__dict__ finally: sys.stdout = old_stdout sys.stderr = old_stderr except: self.response.out.write(traceback.format_exc()) self.response.out.write(responder('some stupid error','',db_user)) return # extract the new globals that this statement added new_globals = {} for name, val in statement_module.__dict__.items(): if name not in old_globals or val != old_globals[name]: new_globals[name] = val if True in [isinstance(val, UNPICKLABLE_TYPES) for val in new_globals.values()]: # this statement added an unpicklable global. store the statement and # the names of all of the globals it added in the unpicklables. session.add_unpicklable(statement, new_globals.keys()) else: # this statement didn't add any unpicklables. pickle and store the # new globals back into the datastore. session.set_global('help', 'Use the instructions link at the bottom for more info.') session.set_global('author', 'Diwank Singh') session.set_global('about', 'The friendly Python Instructor') session.set_global('inspiration', 'Ila Nitin Gokarn') for name, val in new_globals.items(): if not name.startswith('__'): session.set_global(name, val) finally: sys.modules['__main__'] = old_main self.response.out.write(reply) if is_mobile: self.response.out.write(tidbit) self.response.out.write(session_id) self.response.out.write(open(mobile_template[1]).read()) session.put()
def post(self): # extract the statement to be run. RUNNING IN HANGOUT. if not self.request.get('hangouts'): return statement = self.request.get('statement') if not statement: return # the python compiler doesn't like network line endings statement = statement.replace('\r\n', '\n') is_mobile = False # add a couple newlines at the end of the statement. this makes # single-line expressions such as 'class Foo: pass' evaluate happily. statement += '\n' lol = self.request.get('lol') if lol == '1': statement = lolpython.to_python(statement) import sys as _lol_sys statement=statement.strip() reply='' chat=(''.join([statement,'#']).split('#')[1]) self.response.clear() self.response.headers['Content-Type'] = 'text/text' self.response.out.write(statement) statement = statement.split('#')[0] #TODO: change this. reply=responder(statement,chat,db_user) # log and compile the statement up front try: if statement.strip(): compiled = compile(statement, '<string>', 'single') except: self.response.out.write(traceback.format_exc()) self.response.out.write(responder('some stupid error','',db_user)) return # create a dedicated module to be used as this statement's __main__ statement_module = new.module('__main__') # use this request's __builtin__, since it changes on each request. # this is needed for import statements, among other things. import __builtin__ statement_module.__builtins__ = __builtin__ # load the session from the datastore session_id = self.request.get('session') session = Session.get(session_id) # swap in our custom module for __main__. then unpickle the session # globals, run the statement, and re-pickle the session globals, all # inside it. old_main = sys.modules.get('__main__') try: sys.modules['__main__'] = statement_module statement_module.__name__ = '__main__' # re-evaluate the unpicklables for code in session.unpicklables: exec code in statement_module.__dict__ # re-initialize the globals for name, val in session.globals_dict().items(): try: statement_module.__dict__[name] = val except: msg = 'Dropping %s since it could not be unpickled.\n' % name self.response.out.write(msg) logging.warning(msg + traceback.format_exc()) session.remove_global(name) # run! old_globals = dict(statement_module.__dict__) try: old_stdout = sys.stdout old_stderr = sys.stderr try: sys.stdout = self.response.out sys.stderr = self.response.out if statement.strip(): exec compiled in statement_module.__dict__ finally: sys.stdout = old_stdout sys.stderr = old_stderr except: self.response.out.write(traceback.format_exc()) self.response.out.write(responder('some stupid error','',db_user)) return # extract the new globals that this statement added new_globals = {} for name, val in statement_module.__dict__.items(): if name not in old_globals or val != old_globals[name]: new_globals[name] = val if True in [isinstance(val, UNPICKLABLE_TYPES) for val in new_globals.values()]: # this statement added an unpicklable global. store the statement and # the names of all of the globals it added in the unpicklables. session.add_unpicklable(statement, new_globals.keys()) else: # this statement didn't add any unpicklables. pickle and store the # new globals back into the datastore. session.set_global('help', 'Use the instructions link at the bottom for more info.') session.set_global('author', 'Diwank Singh') session.set_global('about', 'The friendly Python Instructor') session.set_global('inspiration', 'Ila Nitin Gokarn') for name, val in new_globals.items(): if not name.startswith('__'): session.set_global(name, val) finally: sys.modules['__main__'] = old_main self.response.out.write(reply) session.put()
def get(self): _DEFAULT_ROLE = "tester" #change to student user=users.get_current_user() query=ShellUser.all() query.filter("account = ", user) db_user = query.get() if not db_user: logging.info("New user: %s registered" % user.nickname()) db_user = ShellUser(name=user.nickname(), role=_DEFAULT_ROLE, course_completed = False, account = user, email = user.email(), current_lesson = 1) history = History(account = user, number = 0) db_user.put() history.put() query=History.all() query.filter("account = ", user) history = query.get() self.response.headers['Content-Type'] = 'text/plain' # extract the statement to be run statement = self.request.get('statement') if not statement: return # the python compiler doesn't like network line endings statement = statement.replace('\r\n', '\n') # add a couple newlines at the end of the statement. this makes # single-line expressions such as 'class Foo: pass' evaluate happily. statement += '\n\n' lol = self.request.get('lol') if lol == '1': statement = lolpython.to_python(statement) import sys as _lol_sys self.response.out.write(statement) # log and compile the statement up front try: logging.info('Compiling and evaluating:\n%s' % statement) compiled = compile(statement, '<string>', 'single') except: self.response.out.write(traceback.format_exc()) #Reply here return # create a dedicated module to be used as this statement's __main__ statement_module = new.module('__main__') # use this request's __builtin__, since it changes on each request. # this is needed for import statements, among other things. import __builtin__ statement_module.__builtins__ = __builtin__ # load the session session_key = self.request.get('session') cache = Cache(session_key) # swap in our custom module for __main__. old_main = sys.modules.get('__main__') try: sys.modules['__main__'] = statement_module statement_module.__name__ = '__main__' # re-evaluate the statements history for code in cache.get_values(): try: compiled_code = compile(code, '<string>', 'single') exec compiled_code in statement_module.__dict__ self.response.clear() logging.info("Executing statements from history:"+code) except: logging.warning("Error in executing history:"+code) # statement added if cache.add_statement(statement): logging.debug('Storing statement in memcache: %s'% statement) # run! old_globals = dict(statement_module.__dict__) try: old_stdout = sys.stdout old_stderr = sys.stderr try: sys.stdout = self.response.out sys.stderr = self.response.out exec compiled in statement_module.__dict__ finally: sys.stdout = old_stdout sys.stderr = old_stderr except: self.response.out.write(traceback.format_exc()) return finally: sys.modules['__main__'] = old_main