<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Good day!<div><br></div><div>Refs:</div><div> <a href="http://pypi.python.org/pypi/RestrictedPython">http://pypi.python.org/pypi/RestrictedPython</a></div><div><br></div><div>I want to pass existing bound variables into the environment of some restricted code before running it. </div><div><br></div><div>This is similar to when globals are passed to compile_restricted_function.</div><div><br></div><div>The idea... </div><div><br></div><div><div><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Verdana; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Verdana; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Verdana; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Verdana; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div><font class="Apple-style-span" face="'Courier New'"> x = 42</font></div><div><font class="Apple-style-span" face="'Courier New'"> y = 'foo'</font></div><div><font class="Apple-style-span" face="'Courier New'"> src="""print x*2; print y; return printed"""</font></div><div><font class="Apple-style-span" face="'Courier New'"> code=compile_restricted(src, '<string>', 'exec')</font></div><div><font class="Apple-style-span" face="'Courier New'"> code.global['x'] = x # <- just dreaming, here.</font></div><div><font class="Apple-style-span" face="'Courier New'"> code.global['y'] = y # <- just dreaming, here.</font></div><div><font class="Apple-style-span" face="'Courier New'"> ans = exec(code) in restricted_globals</font></div><div><font class="Apple-style-span" face="'Courier New'"> print ans # prints "84\n'foo'\n"</font></div><div><br></div><div>It's clear that passing in arbitrary objects is not feasible. But what if I'm prepared to constrain input to built-in numeric and string types, possibly in or with nested dicts?</div><div><br></div><div>In many cases the input will be large integer or float arrays, and I want to avoid any nasty</div><div>kludge such as printing those inputs into the top lines of the src ;). The src in the demo is a constant,</div><div>but in the application, it's provided by users via a list of pre-defined and user-created 'macros'.</div><div><br></div><div>If I'm missing something obvious, flames welcome!</div><div><br></div><div>--r.</div><div><br></div>Russ Ferriday -- </div><div>CEO Topia Systems Ltd.</div><div><a href="mailto:russf@topia.com">russf@topia.com</a> -- +1 (805) 910 7877 -- <a href="http://www.topia.com">www.topia.com</a></div><div><br></div></div></span><br class="Apple-interchange-newline"></div></span><br class="Apple-interchange-newline"></div></span><br class="Apple-interchange-newline"></div></span><br class="Apple-interchange-newline"></div><br class="Apple-interchange-newline"><br class="Apple-interchange-newline">
</div>
<br></div></body></html>