473,421 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

Q: How to generate code object from bytecode?

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(bytecode)

## load bytecode and eval it
bytecode = open('hoge.pyc', 'rb').read()
code = create_code(bytecode) ## how to ?????
output = eval(code, globals, {'items': ['A', 'B', 'C']})

--
regards,
kwatch

Dec 26 '06 #1
4 3279
kw****@gmail.com 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(bytecode)

## load bytecode and eval it
bytecode = open('hoge.pyc', 'rb').read()
code = create_code(bytecode) ## 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>

Dec 26 '06 #2
On Tue, 2006-12-26 at 11:15 -0800, kw****@gmail.com 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(bytecode)

## load bytecode and eval it
bytecode = open('hoge.pyc', 'rb').read()
code = create_code(bytecode) ## 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
Dec 26 '06 #3
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
Dec 26 '06 #4
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(escape(to_str(item)));
_buf.append('''</li>\n''');
#end
_buf.append('''</ul>\n''');

--
regards,
kwatch

Dec 26 '06 #5

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

Similar topics

46
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?
0
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...
8
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...
235
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...
1
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...
4
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...
10
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...
6
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'...
5
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
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...
0
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...
0
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...

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.