CoolFace
Modelpublic

Accio-Lab/occamy-1.0-FP8

sourceHugging Faceapache-2.0updated 11d agoView on Hugging Face
4likes209downloads
code_check.py16 linesDownload Raw Back to evaluation
1import ast,json,re,sys,resource2resource.setrlimit(resource.RLIMIT_CPU,(3,3));resource.setrlimit(resource.RLIMIT_AS,(512*1024**2,512*1024**2))3x=json.load(sys.stdin);s=x['code'].strip();s=re.sub(r'^```(?:python)?\s*|\s*```$','',s);t=ast.parse(s)4allowed_methods={'append','extend','pop','insert','remove','sort','reverse','copy','count','index','lower','upper','isalnum','isalpha','isdigit','strip','split','join','replace','get','items','keys','values','add','discard','update'}5for n in ast.walk(t):6 if isinstance(n,(ast.Import,ast.ImportFrom,ast.ClassDef,ast.Global,ast.Nonlocal)):raise ValueError('Disallowed node')7 if isinstance(n,ast.Name) and n.id.startswith('__'):raise ValueError('Disallowed name')8 if isinstance(n,ast.Attribute) and n.attr not in allowed_methods:raise ValueError('Disallowed attribute')9 if isinstance(n,ast.FunctionDef) and n.decorator_list:raise ValueError('Disallowed decorator')10for n in t.body:11 if not isinstance(n,(ast.FunctionDef,ast.Assign,ast.AnnAssign,ast.Expr)):raise ValueError('Disallowed top level')12builtins={k:__builtins__.__dict__[k] for k in ['int','float','str','bool','list','dict','tuple','set','len','range','enumerate','zip','sorted','reversed','min','max','sum','abs','all','any','isinstance','ValueError','TypeError']}13ns={'__builtins__':builtins};exec(compile(t,'generated','exec'),ns);f=ns['solve'];results=[]14for args,expected in x['tests']:results.append(f(*args)==expected)15print(json.dumps({'pass':all(results),'tests':results}))16