27 lines
1.7 KiB
Python
27 lines
1.7 KiB
Python
"""Independent structural + behavior QA. Never executes arbitrary model Python."""
|
|
import ast, hashlib, json, pathlib, sys
|
|
|
|
def verify(root):
|
|
p=pathlib.Path(root)/'quantity.py'
|
|
if p.is_symlink() or not p.is_file() or p.stat().st_size>2048: raise ValueError('Invalid file')
|
|
raw=p.read_bytes(); tree=ast.parse(raw)
|
|
if len(tree.body)!=1 or not isinstance(tree.body[0],ast.FunctionDef): raise ValueError('Single function required')
|
|
fn=tree.body[0]
|
|
if fn.name!='clamp_quantity' or fn.decorator_list or fn.returns or fn.args.defaults or fn.args.kw_defaults or fn.args.kwonlyargs or fn.args.posonlyargs or fn.args.vararg or fn.args.kwarg or len(fn.args.args)!=1 or fn.args.args[0].arg!='quantity' or fn.args.args[0].annotation or len(fn.body)!=1 or not isinstance(fn.body[0],ast.Return): raise ValueError('Unexpected function structure')
|
|
def expr(n):
|
|
if isinstance(n,ast.Constant) and type(n.value) is int and 0<=n.value<=99: return
|
|
if isinstance(n,ast.Name) and n.id=='quantity': return
|
|
if isinstance(n,ast.Call) and isinstance(n.func,ast.Name) and n.func.id in ('min','max') and len(n.args)==2 and not n.keywords:
|
|
for a in n.args: expr(a)
|
|
return
|
|
raise ValueError('Nonallowlisted syntax')
|
|
expr(fn.body[0].value)
|
|
scope={'__builtins__':{},'min':min,'max':max}
|
|
exec(compile(tree,'generated-quantity','exec'),scope)
|
|
cases=[-1000,-1,0,1,50,98,99,100,10**30]
|
|
for q in cases:
|
|
actual=scope['clamp_quantity'](q)
|
|
if type(actual) is not int or actual!=max(0,min(q,99)): raise ValueError('Behavior QA failed')
|
|
return {'passed':True,'cases':len(cases),'sha256':hashlib.sha256(raw).hexdigest(),'scope':'quantity clamp fixture only'}
|
|
if __name__=='__main__': print(json.dumps(verify(sys.argv[1])))
|