MCPcopy Index your code
hub / github.com/ipython/ipython / guarded_eval

Function guarded_eval

IPython/core/guarded_eval.py:433–466  ·  view source on GitHub ↗

Evaluate provided code in the evaluation context. If evaluation policy given by context is set to ``forbidden`` no evaluation will be performed; if it is set to ``dangerous`` standard :func:`eval` will be used; finally, for any other, policy :func:`eval_node` will be called on parse

(code: str, context: EvaluationContext)

Source from the content-addressed store, hash-verified

431
432
433def guarded_eval(code: str, context: EvaluationContext):
434 """Evaluate provided code in the evaluation context.
435
436 If evaluation policy given by context is set to ``forbidden``
437 no evaluation will be performed; if it is set to ``dangerous``
438 standard :func:`eval` will be used; finally, for any other,
439 policy :func:`eval_node` will be called on parsed AST.
440 """
441 locals_ = context.locals
442
443 if context.evaluation == "forbidden":
444 raise GuardRejection("Forbidden mode")
445
446 # note: not using `ast.literal_eval` as it does not implement
447 # getitem at all, for example it fails on simple `[0][1]`
448
449 if context.in_subscript:
450 # syntactic sugar for ellipsis (:) is only available in subscripts
451 # so we need to trick the ast parser into thinking that we have
452 # a subscript, but we need to be able to later recognise that we did
453 # it so we can ignore the actual __getitem__ operation
454 if not code:
455 return tuple()
456 locals_ = locals_.copy()
457 locals_[SUBSCRIPT_MARKER] = IDENTITY_SUBSCRIPT
458 code = SUBSCRIPT_MARKER + "[" + code + "]"
459 context = context.replace(locals=locals_)
460
461 if context.evaluation == "dangerous":
462 return eval(code, context.globals, context.locals)
463
464 node = ast.parse(code, mode="exec")
465
466 return eval_node(node, context)
467
468
469BINARY_OP_DUNDERS: dict[type[ast.operator], tuple[str]] = {

Callers 15

global_matchesMethod · 0.90
_evaluate_exprMethod · 0.90
dict_key_matchesMethod · 0.90
test_pandas_series_ilocFunction · 0.90
test_pandas_seriesFunction · 0.90
test_pandas_bad_seriesFunction · 0.90
test_named_tupleFunction · 0.90

Calls 5

GuardRejectionClass · 0.85
replaceMethod · 0.80
eval_nodeFunction · 0.70
copyMethod · 0.45
parseMethod · 0.45

Tested by 15

test_pandas_series_ilocFunction · 0.72
test_pandas_seriesFunction · 0.72
test_pandas_bad_seriesFunction · 0.72
test_named_tupleFunction · 0.72
test_dictFunction · 0.72
test_setFunction · 0.72
test_listFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…