Sunday, October 23, 2016

Oh my god, learnt something about python


This works when source text is valid python
globals = dict()
exec(sourcetext,globals)

This does not:

locals = dict()
globals = dict()
exec(sourcetext,globals,locals)

When the source text contains an import statement, like the pop.py code in the previous post, the second python exec fails with undefined tkinter.

Now this sounds like either a documentation error, a lack of understanding, or a bug.  My basic problem here is that I thought eval expected two, one or zero dictionaries. It was the caller's choice.

The issue is that python expects some path initialization in the local dictionary, the debugger does not..  I need to look it  up.

In the debugger, we want the code under test yo run with its own local dictionary, but I had to disable the feature for now.  But the dictionaries give us a useful of info about the code under test, we want that isolated in its own dictionary.

So, I looked here:
https://docs.python.org/3/library/functions.html#exec

And yes indeed, exec will work just fine in a single dictionary, and will maintain locals and globals there. So, I remove local dict, we do not want it, it interferes with the code under test.

Moving on
This redneck code manages the basics, exec, evel, and the tracebacks. It will break properly.

So, it is easy for the professional to break off a brand new window that just picks apart the global/local symbols for a very close look.
The debugger will run the analysis module on a break, and pass it the instance of Snippet, which contains everything there is to know, including the original source, cause of interrupt, symbol table, frame, line number, line text, debugger root window etc. The analysis code calls getatr to find out the specifics.

No comments: