@dog_stats_api.timed('capa.safe_exec.time')
def safe_exec(code, globals_dict, random_seed=None, python_path=None, extra_files=None, cache=None, slug=None, unsafely=False):
 if cache:
  safe_globals = json_safe(globals_dict)
  md5er = hashlib.md5()
  md5er.update(repr(code))
  update_hash(md5er, safe_globals)
  key = ('safe_exec.%r.%s' % (random_seed, md5er.hexdigest()))
  cached = cache.get(key)
  if (cached is not None):
   (emsg, cleaned_results) = cached
   globals_dict.update(cleaned_results)
   if emsg:
    raise SafeExecException(emsg)
   return
 code_prolog = (CODE_PROLOG % random_seed)
 if unsafely:
  exec_fn = codejail_not_safe_exec
 else:
  exec_fn = codejail_safe_exec
 try:
  exec_fn(((code_prolog + LAZY_IMPORTS) + code), globals_dict, python_path=python_path, extra_files=extra_files, slug=slug)
 except SafeExecException as e:
  emsg = e.message
 else:
  emsg = None
 if cache:
  cleaned_results = json_safe(globals_dict)
  cache.set(key, (emsg, cleaned_results))
 if emsg:
  raise e

@ dog_stats_api . timed ( 'capa.safe_exec.time' ) def safe_exec ( code , globals_dict , random_seed = None , python_path = None , extra_files = None , cache = None , slug = None , unsafely = False ) : if cache : safe_globals = json_safe ( globals_dict ) md5er = hashlib . md5 ( ) md5er . update ( repr ( code ) ) update_hash ( md5er , safe_globals ) key = ( 'safe_exec.%r.%s' % ( random_seed , md5er . hexdigest ( ) ) ) cached = cache . get ( key ) if ( cached is not None ) : ( emsg , cleaned_results ) = cached globals_dict . update ( cleaned_results ) if emsg : raise SafeExecException ( emsg ) return code_prolog = ( CODE_PROLOG % random_seed ) if unsafely : exec_fn = codejail_not_safe_exec else : exec_fn = codejail_safe_exec try : exec_fn ( ( ( code_prolog + LAZY_IMPORTS ) + code ) , globals_dict , python_path = python_path , extra_files = extra_files , slug = slug ) except SafeExecException as e : emsg = e . message else : emsg = None if cache : cleaned_results = json_safe ( globals_dict ) cache . set ( key , ( emsg , cleaned_results ) ) if emsg : raise e
safely execute python code .