def test_validate_split_lines_with_comma(self): python_code = """ print 'Hello, world!' import \\ math, os print os.listdir("/") """ self.assertListEqual(['Import from module "os" is not allowed. Check Python page for explanation.'], validate_python_code(python_code))
def test_validate_ignore_comments_and_strings(self): python_code = """ print 'import os' print 'execfile(mkmkmm)' #execfile("mkkmk.py") #eval("bla bla bla") #import os print os.listdir("/") """ self.assertEquals([], validate_python_code(python_code))
def test_validate_keywords(self): python_code = """ print 'Hello, world!' execfile("mkkmk.py") eval("bla bla bla") print os.listdir("/") """ self.assertSetEqual({'Forbidden keyword "execfile". Check Python page for explanation.', 'Forbidden keyword "eval". Check Python page for explanation.'}, set(validate_python_code(python_code)))
def test_validate_strings(self): python_code = """print 'Hello, world!' import math from os.blabla import koko print math.exp(1) print os.listdir("/") print "bla bla bla '''' \" " """ self.assertEquals(['Import from module "os" is not allowed. Check Python page for explanation.'], validate_python_code(python_code))
def runpython(request): python_code = request.POST["python_code"] print "Python code=", python_code validation_errors = validate_python_code(python_code) if validation_errors: formatted_errors = format_validation_errors(validation_errors) return HttpResponse(simplejson.dumps({ "output": '', "error": True, "error_message": "Errors:\n%s" % formatted_errors })) python_process = subprocess.Popen(["python"], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE) # Timer timer_error = list() def check_and_kill(): print "Before polling" process_poll = python_process.poll() print "Process poll= %s" % process_poll if process_poll is None: logger.info("Process terminated by timer") python_process.terminate() timer_error.append("Process was termitated by timeout because it took more than %d second" % TIMER_WAIT) timer = Timer(TIMER_WAIT, check_and_kill) timer.start() try: (output, error) = python_process.communicate(python_code) except Exception, e: logger.warning(e) raise e