Have you tried passing in empty dicts for globals and locals? I think
that the defaults will be the *current* globals and locals, and then
of course your namespace is broken...
On Tue, Oct 7, 2008 at 1:26 PM, Gordon Fraser <gf*******@gmail.comwrote:
Hi,
I'm trying to parse Python code to an AST, apply some changes to the AST
and then compile and run the AST, but I'm running into problems when
trying to evaluate/execute the resulting code object. It seems that the
global namespace differs depending on where I call parse and eval/exec.
The following code parses a file, compiles and then evaluates the AST.
If I call Python directly on this code, then it works:
import sys, parser
ast = parser.suite(open(sys.argv[1]).read())
code = ast.compile()
exec(code)
...and it also works this way with Python2.6:
ast = compile(open(sys.argv[1]).read(), "<AST>",
'exec',_ast.PyCF_ONLY_AST)
code = compile(ast, "<AST", "exec")
exec(code)
However, if I include that snippet in a different scope (some function
or class), then the namespace that the code object will have differs -
it seems the symbols defined in the AST are not included when executing
the code. For example:
import sys,parser
def main():
ast = parser.suite(open(sys.argv[1]).read())
code = ast.compile()
exec(code)
if __name__ == "__main__":
main()
In particular this is a problem if I'm parsing a module with several
functions - none of these functions actually ends up in the scope of the
code object (same behavior with Python2.6 and the PyCF_ONLY_AST
version).
The function "exec" takes parameters for globals and locals, but I have
no idea where to get these dictionaries from the "parser" module. My
guess is that I am misunderstanding something about how Python treats
namespaces. Can anyone help me here?
Thanks,
Gordon
-- http://mail.python.org/mailman/listinfo/python-list
-- or*****@orestis.gr http://orestis.gr 0 1314 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Mike McGavin |
last post by:
Hi everyone.
I've been trying for several hours now to get minidom to parse
namespaces properly from my stream of XML, so that I can use DOM methods
such as getElementsByTagNameNS(). For some...
|
by: poiuz24 |
last post by:
aeh, this may be stupid, but could s.b. please explain
to me why the following won't link (gcc 3.3)?
foo.cpp = """
namespace foo
{
int main ()
{
return 0;
}
|
by: s99999999s2003 |
last post by:
hi
the database "execute" function returns a list of logical results. Each
logical result is a list of row tuples, as explained in the documents.
everytime i use it to execute various...
|
by: ecomputerdelicacy |
last post by:
Compact Framework SP3 (and I think also SP2 and SP1) XmlTextReader does not
handle !DOCTYPE and (maybe?) rdf elements. How can I get the XmlTextReader to
read these without issuing a...
|
by: alainpoint |
last post by:
Hi
I wonder if Python is capable of the following: define a function which
returns its argument.
I mean:
def magic_function(arg):
...... some magic code ...
that behaves the following way:
|
by: JamesB |
last post by:
I am writing a service that monitors when a particular app is started.
Works, but I need to get the user who is currently logged in, and of course
Environment.UserName returns the service logon...
|
by: Gordon Fraser |
last post by:
Hi,
I'm trying to parse Python code to an AST, apply some changes to the AST
and then compile and run the AST, but I'm running into problems when
trying to evaluate/execute the resulting code...
|
by: Gordon Fraser |
last post by:
Hi,
Am Dienstag, den 07.10.2008, 15:30 +0100 schrieb Orestis Markou:
That seems to work, thanks. Still trying to make sense of the manual for
eval, as it says if the passed globals dict lacks...
|
by: Philip Semanchuk |
last post by:
On Oct 12, 2008, at 5:25 AM, S.Selvam Siva wrote:
Selvam,
You can try to find them yourself using string parsing, but that's
difficult. The closer you want to get to "perfect" at finding URLs...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |