Sometimes it works ok
Interpreter["Expression"]["1"]
1
sometimes not:
Interpreter["Expression"]["Graphics[{}]"]
Why doesn't it work for the second case? What is the rule of thumb here?
In interpreting an expression, only some symbols are allowed. A list of allowed symbols may be found (V11.2) with:
Interpreter["Expression"]["1"]; (* to autoload code *)
Internal`CheckCache[
Interpreter`Transform`PackagePrivate`iInterpreterObject[
"Expression", <|"Parser" ->
Interpreter`PackageScope`expressionParser,
"iRestrictionFunction" ->
Interpreter`PackageScope`expressionRestriction|>][
"SymbolRestriction"]] // Sort
System`
symbols not on this list seem to be disallowed. Also other symbols not in the Global`
context seem to be disallowed.
Interesting inclusions/omissions:
Interpreter["Expression"]["AiryAiZero"]
Interpreter["Expression"]["AiryAi"]
Interpreter["Expression"]["\[FormalX]"]
Interpreter["Expression"]["foo`x"]
(*
Failure[..]
AiryAi
Failure[..]
Failure[..]
*)
It may be there are other rules in play, but this is my first approximation.
Update: Workarounds. If you wish to throw caution to the wind, you can override the security check at your own risk:
Block[{Security`InsecureExprQ = False &},
Interpreter["Expression"]["Graphics[{}]"]
]
Targeting exceptions. Security`InsecureExprQ
does have hooks to allow/disallow specific symbols and contexts:
Security`$AllowedContexts
Security`$AllowedSymbols
Security`$DisallowedContexts
Security`$DisallowedSymbols
However Interpreter
overrides these. So we can override Interpreter
with the Villegas-Gayley trick. Since Interpreter
passes the second element allowedSymbols
with a head of HoldComplete
and the other three as Lists
, you have to do the same.
interp[s_String,
{allowedContexts_, allowedSymbols_, disallowedContexts_, disallowedSymbols_}] :=
Internal`InheritedBlock[{Security`InsecureExprQ},
Security`InsecureExprQ[e_HoldComplete, data_] /; ! TrueQ[$in] :=
Block[{$in = True},
Security`InsecureExprQ[e,
MapThread[Join,
{data,
{allowedContexts, allowedSymbols, disallowedContexts, disallowedSymbols}}]]
];
Interpreter["Expression"][s]
];
None of the graphics primitives and directive are on the default allowed list, so we have to add all we use:
interp["Graphics[{Text[Style[foo`x,Large],{0,0}]},Frame->True]",
{{"foo`"}, HoldComplete[Graphics, Text, Style, Large, Frame], {}, {}}]
Sort
to make the list easier to use.
$\endgroup$
Feb 12, 2018 at 13:12
MemberQ
, too.
$\endgroup$
Feb 12, 2018 at 13:25
Interpreter[Restricted["Expression", All]]["Print[1]"]
. It is undocumented ofc :) but you can add it for completeness.
$\endgroup$
"plot[Sin[x],{x,0,10}]"
, fails on"Plot[Sin[x],{x,0,10}]"
. Work on"plot"
, fails on"Plot"
. Maybe certain symbols are disallowed for security or something like that. $\endgroup$