Hi,
I would like to compile an AST to bytecode, so I can eval it later. I
tried using parse.compileas t, but it fails:
>>import compiler, parser ast = compiler.parse( "42") parser.compil east(ast)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: compilest() argument 1 must be parser.st, not instance
Any hints?
TIA,
--Rob 3 3253
"Rob De Almeida" <ra******@gmail .comwrote:
I would like to compile an AST to bytecode, so I can eval it later. I
tried using parse.compileas t, but it fails:
>>>import compiler, parser ast = compiler.parse( "42") parser.compi least(ast)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: compilest() argument 1 must be parser.st, not instance
Any hints?
There are two distinct varieties of AST in the Python standard library.
compiler and parser ast objects are not compatible.
I'm not sure there are any properly documented functions for converting an
AST to a code object, so your best bet may be to examine what a
pycodegen class like Expression or Module actually does.
>>import compiler def ast_to_evalcode (ast):
return compiler.pycode gen.ExpressionC odeGenerator(as t).getCode()
>>def parse_to_ast(sr c):
return compiler.pycode gen.Expression( src, "<string>")._ge t_tree()
>>ast = parse_to_ast("4 0+2") ast
Expression(Add( (Const(40), Const(2))))
>>ast_to_evalco de(ast)
<code object <expressionat 0122D848, file "<string>", line -1>
>>eval(_)
42
Duncan Booth wrote:
I would like to compile an AST to bytecode, so I can eval it later.
I'm not sure there are any properly documented functions for converting an
AST to a code object, so your best bet may be to examine what a
pycodegen class like Expression or Module actually does.
Thanks, Duncan. It worked perfectly. :-)
For arbitrary nodes I just had to wrap them inside an Expression node:
>>ast = compiler.ast.Ex pression(node) ast.filenam e = 'dummy' c = compiler.pycode gen.ExpressionC odeGenerator(as t) obj = eval(c.getCode( ), scope)
--Rob
Rob De Almeida wrote:
Duncan Booth wrote:
I would like to compile an AST to bytecode, so I can eval it later.
I'm not sure there are any properly documented functions for converting an
AST to a code object, so your best bet may be to examine what a
pycodegen class like Expression or Module actually does.
Thanks, Duncan. It worked perfectly. :-)
For arbitrary nodes I just had to wrap them inside an Expression node:
>ast = compiler.ast.Ex pression(node) ast.filename = 'dummy' c = compiler.pycode gen.ExpressionC odeGenerator(as t) obj = eval(c.getCode( ), scope)
If you're only worried about expressions, then you can have CPython do
the compilation for you much faster by wrapping the expression in a
lambda:
>>f = lambda: 42 f.func_code.c o_code
'd\x01\x00S'
>>map(ord, _)
[100, 1, 0, 83]
>>g = lambda x, y: (x * 3) + len(y) map(ord, g.func_code.co_ code)
[124, 0, 0, 100, 1, 0, 20, 116, 1, 0, 124, 1, 0, 131, 1, 0, 23, 83]
For a complete round-trip Expression library, see http://projects.amor.org/dejavu/browser/trunk/logic.py
Robert Brewer
System Architect
Amor Ministries fu******@amor.o rg This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: John Doe |
last post by:
Hello,
I'm currently working on a little tool that would benefit from having
a little embedded script language. Although I have C++ source code
available for such a script language, I have 2 concerns:
- The capabilities of the C++ script interpreter that I've written are
rather limited (of course I can solve this)
- My little script interpreter was written for readability rather than
|
by: Vio |
last post by:
I would like to know if executing:
c = compile('a=5\nprint a','<string>','exec')
on a Linux box, then pickling+beaming the result on a Windows box, will give me the expecting result:
>>> exec(c)
5
|
by: Benjamin Scherrey |
last post by:
I'm curious as to how difficult it would be to take a string that contains
compiled bytecode, load it into memory, give it a function name then
execute that function. I'm thinking of a database that contains compiled
objects that I can load and execute. I'm also curious as to what level of
grainularity this would work - module, class, class method, function?
Anyone tried to do this before? Obviously dependencies are a consideration
but I'm...
|
by: Nick Coghlan |
last post by:
Time for another random syntax idea. . .
So, I was tinkering in the interactive interpreter, and came up with the
following one-size-fits-most default argument hack:
Py> x = 1
Py> def _build_used():
.... y = x + 1
.... return x, y
....
|
by: Maurice LING |
last post by:
Hi,
I've been using Python for about 2 years now, for my honours project and
now my postgrad project. I must say that I am loving it more and more
now. From my knowledge, Python bytecodes are not back-compatible. I must
say that my technical background isn't strong enough but is there any
good reason for not being back-compatible in bytecodes?
My problem is not about pure python modules or libraries but the problem
is with 3rd party...
| |
by: Charles |
last post by:
Dear sirs,
I'm doing some investigations on the Python language.
I'd like to know if it is possible to compile Python code made for the web
(an online database for instance), and to run it using mod_python. How is
it possible? I think it would be very fast to execute the code if it were
compiled.
Thanks,
--
|
by: sxanth |
last post by:
>What puzzles me, though, are bytecodes 17, 39 and 42 - surely these aren't
>reachable? Does the compiler just throw in a default 'return None'
>epilogue, with routes there from every code path, even when it's not
>needed? If so, why?
Hi.
pyc (http://freshmeat.net/projects/pyc) can already remove that
unused code since June.
|
by: Fuzzyman |
last post by:
Hello all,
The following is a copy of a blog entry. It's asking a question about
future statements and the built in compile function. I'd appreciate any
pointers or comments about possible approaches.
`Movable Python <http://www.voidspace.org.uk/python/movpy/>`_ supports
running both Python scripts and ``.pyc`` bytecode files. It does this
by compiling scripts to bytecode, or extracting the code object from
bytecode files, and then...
|
by: schwarz |
last post by:
As part of some research I am doing a Python Virtual Machine in Java,
and the exact semantics of the STORE_NAME bytecode is unclear to be,
so I was hoping somebody here could clarify it.
The STORE_NAME bytecode is supposed to set a value for a name in the
current scope. However, the following piece of code:
def hello(who):
print "Hello", who
return hello(who)
print "Say:"
|
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...
|
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,...
| |
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,...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
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...
| |