14
$\begingroup$

Sometimes it works ok

Interpreter["Expression"]["1"]

1

sometimes not:

Interpreter["Expression"]["Graphics[{}]"]

enter image description here

Why doesn't it work for the second case? What is the rule of thumb here?

$\endgroup$
  • $\begingroup$ Works on "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$ – Michael E2 Feb 12 '18 at 12:11
  • $\begingroup$ @MichaelE2 yes, makes sense but it seems to be be quite sensitive which makes it pretty useless for a general scenario. Unless well documented. $\endgroup$ – Kuba Feb 12 '18 at 12:15
13
$\begingroup$

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], {}, {}}]

Mathematica graphics

|improve this answer|||||
$\endgroup$
  • 1
    $\begingroup$ +1 Recommend that you use Sort to make the list easier to use. $\endgroup$ – Bob Hanlon Feb 12 '18 at 13:12
  • $\begingroup$ @BobHanlon Thanks, Bob. I might use MemberQ, too. $\endgroup$ – Michael E2 Feb 12 '18 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.