Q: How to generate code object from bytecode?

K

kwatch

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']})
 
F

Fredrik Lundh

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>
 
C

Carsten Haese

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
 
C

Carsten Haese

* 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:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
print 'Hi'
^
SyntaxError: invalid syntaxHi

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
 
K

kwatch

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''');
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top