473,748 Members | 8,367 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: Getting namespaces right when parsing/executing Python ASTs

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*******@gmai l.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(op en(sys.argv[1]).read())
code = ast.compile()
exec(code)

...and it also works this way with Python2.6:

ast = compile(open(sy s.argv[1]).read(), "<AST>",
'exec',_ast.PyC F_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(op en(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 misunderstandin g 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
Oct 7 '08 #1
0 1342

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
6389
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 reason, though, it just doesn't seem to want to split the prefixes from the rest of the tags when parsing. The minidom documentation at http://docs.python.org/lib/module-xml.dom.minidom.html implies that
27
2026
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; }
4
1898
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 statements, it returns me, for example (, , ) and sometimes , (, ) or () in my call, i give eg (a,b,c) = db.execute(stmt) so that it returns me (, ,
2
3665
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 NotSupportedException? Or at least to get past these elements (and nested elements) with an Skip()? I've tried setting xtr.XmlResolver = null; but it doesn't change the functionality. I'm still getting the exception. In a recent MSDN MVP Chat, I was pointed...
20
1566
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:
33
11862
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 (NT_AUTHORITY\SYSTEM). I understand that when the service starts, no user may be logged in, but that's ok, as the app I am monitoring can only be run by a logged in user. Do I need to use WMI to get the user context of Explorer.exe or is there a...
0
1060
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 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:
0
189
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 '__builtins__' it will copy the current globals, and if no dict is passed it will use the current globals (so what's the difference?). But anyway, as long as it works I'm happy!
1
2878
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 expressed in JS, the closer you'll get to rewriting a JS interpreter. For instance, this is not so hard to understand: "http://example.com/" but this is:
0
8984
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.