Hi,
It is possible to get bytecode from code object.
Reversely, is it possible to create code object from bytecode?
ex.
## python code (not a module)
pycode = '''\
print "<ul>\n"
for item in items:
print "<li>%s</li>\n" % item
print "</ul>\n"
'''
## compile it and get bytecode
code = compile(pycode, kind='exec')
bytecode = code.co_code
open('hoge.pyc' , 'wb').write(byt ecode)
## load bytecode and eval it
bytecode = open('hoge.pyc' , 'rb').read()
code = create_code(byt ecode) ## how to ?????
output = eval(code, globals, {'items': ['A', 'B', 'C']})
--
regards,
kwatch 4 3298 kw****@gmail.co m wrote:
It is possible to get bytecode from code object.
Reversely, is it possible to create code object from bytecode?
ex.
## python code (not a module)
pycode = '''\
print "<ul>\n"
for item in items:
print "<li>%s</li>\n" % item
print "</ul>\n"
'''
## compile it and get bytecode
code = compile(pycode, kind='exec')
bytecode = code.co_code
open('hoge.pyc' , 'wb').write(byt ecode)
## load bytecode and eval it
bytecode = open('hoge.pyc' , 'rb').read()
code = create_code(byt ecode) ## how to ?????
output = eval(code, globals, {'items': ['A', 'B', 'C']})
use the marshal module; see the last script on this page for an example: http://effbot.org/librarybook/marshal
</F>
On Tue, 2006-12-26 at 11:15 -0800, kw****@gmail.co m wrote:
Hi,
It is possible to get bytecode from code object.
Reversely, is it possible to create code object from bytecode?
ex.
## python code (not a module)
pycode = '''\
print "<ul>\n"
for item in items:
print "<li>%s</li>\n" % item
print "</ul>\n"
'''
## compile it and get bytecode
code = compile(pycode, kind='exec')
bytecode = code.co_code
open('hoge.pyc' , 'wb').write(byt ecode)
## load bytecode and eval it
bytecode = open('hoge.pyc' , 'rb').read()
code = create_code(byt ecode) ## how to ?????
output = eval(code, globals, {'items': ['A', 'B', 'C']})
As Fredrik has said, you should use marshal to handle the writing and
reading of the code object. I'd like to point out the following
additional facts that may not be apparent to you:
* Code objects come in two flavors: statements and expressions.
* exec can execute a 'statement' flavored code object.
* eval can evaluate an 'expression' flavored code object.
* Your code snippet is a statement, actually, a suite of statements. You
need to exec it, not eval it.
* You seem to think that eval'ing or exec'ing a code object will
magically capture its output stream. It won't.
What do you actually want to accomplish? Instead of having us poke at
your first attempt at a solution, it might be more helpful if you told
us what problem you're actually trying to solve.
-Carsten
On Tue, 2006-12-26 at 14:48 -0500, Carsten Haese wrote:
* Code objects come in two flavors: statements and expressions.
* exec can execute a 'statement' flavored code object.
* eval can evaluate an 'expression' flavored code object.
* Your code snippet is a statement, actually, a suite of statements. You
need to exec it, not eval it.
And to reply to myself before the effbot corrects me, it turns out that
the separation between expressions and statements is not quite so
strict. For one, an expression can be used like a statement.
Also, apparently eval() can evaluate the code object of a statement,
even though it can't evaluate the source code of a statement, which I
find oddly inconsistent:
>>eval("print 'Hi'")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
print 'Hi'
^
SyntaxError: invalid syntax
>>eval(compile( "print 'Hi'", "<script>", "exec"))
Hi
There's probably a deeper reason here, but I don't often write code that
needs to rely on eval/exec, so I don't really care ;)
-Carsten
Thanks Fredrik and Carsten,
I'll try marshal module.
* Your code snippet is a statement, actually, a suite of statements. You
need to exec it, not eval it.
* You seem to think that eval'ing or exec'ing a code object will
magically capture its output stream. It won't.
Oh, it's my mistake.
What do you actually want to accomplish?
I'm now developping embedded python text converter.
ex. example.pyhtml
<h1>title</h1>
<ul>
<?py for item in items: ?>
<li>${item}</li>
<?py #end ?>
</ul>
ex. converted python code
_buf = []; _buf.append(''' <h1>title</h1>
<ul>\n''');
for item in items:
_buf.append(''' <li>'''); _buf.append(esc ape(to_str(item )));
_buf.append(''' </li>\n''');
#end
_buf.append(''' </ul>\n''');
--
regards,
kwatch This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jon Perez |
last post by:
Can one run a 1.5 .pyc file with the 2.x version
interpreters and vice versa?
How about running a 2.x .pyc using a 2.y interpreter?
|
by: Johnathan Doe |
last post by:
I've been thinking about what the issues would be in compiling
Python into native machine code, and since type information is
important in Python, it seems possible that Python code can
be compiled into native machine code (albeit with a lot of
extra effort).
For instance, type information is discovered when something is
assigned to a variable or an anonymous piece of data is used
in a program. Compiling Python bytecode into native...
|
by: Irmen de Jong |
last post by:
What would be the best way, if any, to obtain
the bytecode for a given loaded module?
I can get the source:
import inspect
import os
src = inspect.getsource(os)
but there is no ispect.getbytecode() ;-)
|
by: napi |
last post by:
I think you would agree with me that a C compiler that directly
produces Java Byte Code to be run on any JVM is something that is
missing to software programmers so far. With such a tool one could
stay with C and still be able to produce Java byte code for
platform independent apps. Also, old programs (with some tweaking)
could be re-compiled and ported to the JVM.
We have been developing such a tool over the last 2 years and currently...
|
by: praba kar |
last post by:
Dear All,
Python 2.3 creates byte code with *.pyc
extention. But Python 2.4 creates bytes code with
*.pyo. Is there any difference between *.pyc
and *.pyo?.
Actually After python compiled a program
then that program will run from the *.pyc byte
code. If I delete that byte code what will be
| |
by: Fabiano Sidler |
last post by:
Hi folks!
I'm looking for a way to compile python source to bytecode instead of
code-objects. Is there a possibility to do that? The reason is: I want
to store pure bytecode with no additional data.
The second question is, therefore: How can I get the correct values
for a given bytecode, such as the stacksize and flags attributes of
the correspondent code object?
|
by: Math |
last post by:
Hello,
I wonder if I can ask this particular question here...
I'm writing this piece of Python Software and I'm almost done...:-)
But now I want the end-user to register this software with a registration
code or perhaps something like an evaluation demo version which expires
after some period of time...
Is this the right place to ask or does anybody know where to look for more
on the subject?
|
by: Fuzzyman |
last post by:
Hello all,
I'm trying to extract the code object from a function, and exec it
without explicitly passing parameters.
The code object 'knows' it expects to receive paramaters. It's
'arg_count' attribute is readonly.
How can I set the arg_count to 0, or pass parameters to the code object
when I exec it ?
|
by: xkenneth |
last post by:
Hi All,
I'll shortly be distributing a number of python applications that
use proprietary. The software is part of a much larger system and it
will need to be distributed securely. How can i achieve this?
Regards,
Ken
|
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: 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...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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: 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...
|
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
| |