<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Thanks for your great reply. I even augmented the reloading with the same dict by clearing all of the non-standard symbols from the dict. This effectively resets the dict:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-family: Courier; color: rgb(212, 54, 29); background-color: rgb(255, 250, 224);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #4c2f2d" class="">        </span># try to clear out the module by deleting all global refs                                                        </div><div style="margin: 0px; font-family: Courier; color: rgb(76, 47, 45); background-color: rgb(255, 250, 224);" class="">        <span style="font-variant-ligatures: no-common-ligatures; color: #ca761f" class="">d</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #cc3bff" class="">self</span>.module.__dict__</div><div style="margin: 0px; font-family: Courier; color: rgb(76, 47, 45); background-color: rgb(255, 250, 224);" class="">        <span style="font-variant-ligatures: no-common-ligatures; color: #cc3bff" class="">for</span> k <span style="font-variant-ligatures: no-common-ligatures; color: #cc3bff" class="">in</span> <span style="font-variant-ligatures: no-common-ligatures; color: #7979a3" class="">dict</span>(d).keys():</div><div style="margin: 0px; font-family: Courier; color: rgb(172, 53, 128); background-color: rgb(255, 250, 224);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #4c2f2d" class="">            </span><span style="font-variant-ligatures: no-common-ligatures; color: #cc3bff" class="">if</span><span style="font-variant-ligatures: no-common-ligatures; color: #4c2f2d" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #cc3bff" class="">not</span><span style="font-variant-ligatures: no-common-ligatures; color: #4c2f2d" class=""> k </span><span style="font-variant-ligatures: no-common-ligatures; color: #cc3bff" class="">in</span><span style="font-variant-ligatures: no-common-ligatures; color: #4c2f2d" class=""> [</span>'__spec__'<span style="font-variant-ligatures: no-common-ligatures; color: #4c2f2d" class="">, </span>'__name__'<span style="font-variant-ligatures: no-common-ligatures; color: #4c2f2d" class="">, </span>'__loader__'<span style="font-variant-ligatures: no-common-ligatures; color: #4c2f2d" class="">, </span>'__package__'<span style="font-variant-ligatures: no-common-ligatures; color: #4c2f2d" class="">, </span>'__doc__'<span style="font-variant-ligatures: no-common-ligatures; color: #4c2f2d" class="">, </span>'__builtins__'<span style="font-variant-ligatures: no-common-ligatures; color: #4c2f2d" class="">]:</span></div><div style="margin: 0px; font-family: Courier; color: rgb(76, 47, 45); background-color: rgb(255, 250, 224);" class="">                <span style="font-variant-ligatures: no-common-ligatures; color: #cc3bff" class="">del</span> d[k]</div><div style="margin: 0px; font-family: Courier; color: rgb(76, 47, 45); background-color: rgb(255, 250, 224);" class="">        <span style="font-variant-ligatures: no-common-ligatures; color: #cc3bff" class="">self</span>.module.__dict__[<span style="font-variant-ligatures: no-common-ligatures; color: #ac3580" class="">'sendMessage'</span>] = <span style="font-variant-ligatures: no-common-ligatures; color: #cc3bff" class="">self</span>.sendMessage</div><div style="margin: 0px; font-family: Courier; color: rgb(76, 47, 45); background-color: rgb(255, 250, 224);" class="">        <span style="font-variant-ligatures: no-common-ligatures; color: #cc3bff" class="">try</span>:</div><div style="margin: 0px; font-family: Courier; color: rgb(76, 47, 45); background-color: rgb(255, 250, 224);" class="">            <span style="font-variant-ligatures: no-common-ligatures; color: #cc3bff" class="">exec</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #cc3bff" class="">self</span>.source, <span style="font-variant-ligatures: no-common-ligatures; color: #cc3bff" class="">self</span>.module.__dict__)</div><div style="margin: 0px; font-family: Courier; color: rgb(76, 47, 45); background-color: rgb(255, 250, 224);" class="">        <span style="font-variant-ligatures: no-common-ligatures; color: #cc3bff" class="">except</span> <span style="font-variant-ligatures: no-common-ligatures; color: #319f23" class="">Exception</span>:</div><div style="margin: 0px; font-family: Courier; color: rgb(76, 47, 45); background-color: rgb(255, 250, 224);" class="">            <span style="font-variant-ligatures: no-common-ligatures; color: #cc3bff" class="">import</span> traceback</div><div style="margin: 0px; font-family: Courier; color: rgb(76, 47, 45); background-color: rgb(255, 250, 224);" class="">            traceback.print_exc(<span style="font-variant-ligatures: no-common-ligatures; color: #7979a3" class="">file</span>=sys.stdout)</div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Is there a better and more secure way to do the python-within-python in order allow users to automate your app?</div><div class=""><br class=""></div><div class="">Thanks!</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On Nov 23, 2014, at 12:24 AM, Chris Angelico <<a href="mailto:rosuav@gmail.com" class="">rosuav@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">On Sun, Nov 23, 2014 at 4:48 PM, Patrick Stinson <<a href="mailto:patrickkidd@gmail.com" class="">patrickkidd@gmail.com</a>> wrote:<br class=""><blockquote type="cite" class="">I am writing a python app (using PyQt, but that’s not important here), and want my users to be able to write their own scripts to automate the app’s functioning using an engine API hat I expose. I have extensive experience doing this in a C++ app with the CPython api, but have no idea how to do this outside of calling exec() from with in Python :)<br class=""><br class="">Ideally their script would compile when the source changes and retain it’s state and respond to callbacks from the api object. It appears this won’t work with exec() because the script’s definitions and state disappear as soon as the exec() call is complete, and the script doesn’t seem to be able to access it’s own defined functions and classes.<br class=""><br class="">Thoughts? Fun stuff!<br class=""></blockquote><br class="">First off, a cautionary note: Security-wise, this is absolutely<br class="">equivalent to your users editing your source code. Be aware that<br class="">you're giving them complete control.<br class=""><br class="">What you should be able to do is exec the script in a specific global<br class="">dictionary. Here's some example code (Python 3.4):<br class=""><br class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class="">script = """<br class=""></blockquote></blockquote></blockquote>def init():<br class="">    print("Initializing")<br class=""><br class="">def on_some_event(status):<br class="">    trigger_some_action("Status is now "+status)<br class="">"""<br class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class="">def trigger_some_action(msg):<br class=""></blockquote></blockquote></blockquote>    print("Action triggered.",msg)<br class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class="">globl = {"trigger_some_action":trigger_some_action}<br class="">exec(script,globl)<br class="">globl["init"]()<br class=""></blockquote></blockquote></blockquote>Initializing<br class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class="">globl["on_some_event"]("Something happened")<br class=""></blockquote></blockquote></blockquote>Action triggered. Status is now Something happened<br class=""><br class="">You can provide globals like this, or you can create an importable<br class="">module for the scripts to call on. (Or both. Create a module, and<br class="">pre-import it automatically.) The script defines functions with<br class="">specific names and/or calls your functions to register hooks; you can<br class="">reach into the globals to trigger functions.<br class=""><br class="">One way to handle updates to the code would be to exec it in the same<br class="">globals dictionary. That has its complexities (for instance, if you<br class="">rename a function, the old version will still exist under the old name<br class="">unless you explicitly del it), but it can be very convenient.<br class="">Alternatively, you could have the new version run in a new dictionary,<br class="">but important state can be kept in separate dictionaries. That's how I<br class="">manage things with a Pike program - all code gets reloaded cleanly,<br class="">but retained state is stored separately in a mutable object that gets<br class="">passed around.<br class=""><br class="">There are several ways this sort of thing can be done. It's reasonably<br class="">easy, as long as you take a bit of care across the reload boundaries.<br class=""><br class="">ChrisA<br class="">-- <br class=""><a href="https://mail.python.org/mailman/listinfo/python-list" class="">https://mail.python.org/mailman/listinfo/python-list</a><br class=""></div></blockquote></div><br class=""></body></html>