473,545 Members | 582 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Executing bytecode from a string.

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 more interested in the mechanics of this. Appreciate ideas &
pointers you might have...

Ben Scherrey
Jul 18 '05 #1
6 4893
"Benjamin Scherrey" <sc******@prote us-tech.com> writes:
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.
Fairly hard: you'd need to guess or work out all the other parameters
to new.code such as argcount, stacksize,...
I'm thinking of a database that contains compiled objects that I can
load and execute.
You'd likely be much better off storing marshalled code objects than
raw code strings. Whether that would suffice is hard to say from your
description, but it's probably a good start.

Or just Python source...
I'm also curious as to what level of grainularity this would work -
module, class, class method, function?
I think "it depends".
Anyone tried to do this before?


Almost certainly :-)

Cheers,
mwh

--
Ignoring the rules in the FAQ: 1" slice in spleen and prevention
of immediate medical care.
-- Mark C. Langston, asr
Jul 18 '05 #2
Michael Hudson wrote:
"Benjamin Scherrey" <sc******@prote us-tech.com> writes:

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.


Fairly hard: you'd need to guess or work out all the other parameters
to new.code such as argcount, stacksize,...


I seem to recall doing this, and it wasn't hard for my particular
case because it wasn't an arbitrary function. It took no arguments,
returned nothing, etc...

I was doing this as an experiment to see whether it was feasible
to use evolutionary programming to evolve Python bytecode that
would solve some problem.

The idea worked in principle (I was able to get the bytecode to
execute) but unfortunately there were numerous instances where the
effectively random bytecode would lead to the interpreter crashing
fatally.

I still think it's a neat idea, but it won't work well with Python
in its current form.

Might be useful for doing some kind of stress testing on the
interpreter though, if someone was interested in making it
bullet-proof even for psychotic code (as part of a new security
initiative, perhaps?).

And sorry, but this was a quickie experiment and I don't have the
code any more. Just wanted to say it was doable for the simplest
case.

-Peter
Jul 18 '05 #3

"Benjamin Scherrey" <sc******@prote us-tech.com> wrote in message
news:pa******** *************** *****@proteus-tech.com...
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 more interested in the mechanics of this. Appreciate ideas &
pointers you might have...

Ben Scherrey


Look at the 'new' module. It's in the Library Reference under
Python Runtime Services. You'll need to wrap the byte string
with 'code', and then wrap that with 'function'.

As someone else pointed out, there are a number of other
parameters you'll need to supply, and some of them aren't
at all obvious. You may find the disassembler (documented
under Python language services) to be helpful in figuring
some of them out.

John Roth
Jul 18 '05 #4
John Roth wrote:
Look at the 'new' module. It's in the Library Reference under
Python Runtime Services. You'll need to wrap the byte string
with 'code', and then wrap that with 'function'.


that's insane, though. use marshal on code objects, and keep your
sanity.

see page 4-10 in this document for a small sample:

http://www.effbot.org/zone/librarybo...esentation.pdf

to avoid chaos when you upgrade Python, it's a good idea to attach
imp.get_magic() to the marshalled string, and verify it before you un-
marshall the code; see:

http://mail.python.org/pipermail/pyt...er/196230.html

</F>

Jul 18 '05 #5
Benjamin Scherrey wrote:
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 more interested in the mechanics of this. Appreciate ideas &
pointers you might have...

Ben Scherrey


The following was written just for fun with no pretension that it will work
in the "real world". I've only manipulated co_consts and co_code of my
custom code object - you can investigate the other parameters one after
another and replace them with something at your choice.
The question that remains - how do you want to create the bytecode strings?
If from actual functions, why not store their source code, if from
codeobjects, why not marshal.dump()/load() them?

Peter

<code>
# get hold of the function and code types
def f(): pass
function = type(f)
del f
code = type(compile("d ef f(): pass", "noname", "exec"))

template = compile("def f(): pass", "noname", "exec")

def makeFunc(byteco de, consts):
""" create a new function from a bytecode string and a consts tuple """

# instead of inventing all code-attributes ourselves
# copy those we are not interested in from an existing template
co = template
codeobj = code(co.co_argc ount, co.co_nlocals, co.co_stacksize ,
co.co_flags, bytecode, consts, co.co_names,
co.co_varnames, co.co_filename, co.co_name,
co.co_firstline no, co.co_lnotab, co.co_freevars,
co.co_cellvars)

return function(codeob j, globals(), "f", None, None)

# get hold of a valid bytecode string
# (I could have pasted the string literal from the interpreter,
# but I chose not to cheat :-)
def prototype():
print "so what"
bytecode = prototype.func_ code.co_code

# test what we have so far
g = makeFunc(byteco de, consts=(None, "it takes more than the byte-code",))
h = makeFunc(byteco de, consts=(None, "to make a function"))
g()
h()
</code>
Jul 18 '05 #6
Benjamin Scherrey schrieb:
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 more interested in the mechanics of this. Appreciate ideas &
pointers you might have...

Ben Scherrey


I am just writing my Macro/Script in python sourcecode.
In the program I'm doing:

f = open('segment.p y','r')
str = f.read()
f.close()

segment_code = compile(str, '<string>', 'exec')

exec(segment_co de)

Markus
Jul 18 '05 #7

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

Similar topics

46
6205
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?
1
3098
by: Bo Jacobsen | last post by:
I have a number of files compiled to bytecode using py_compile.compile(). The .pyc files can be invoked by python directly ($python file.pyc), but "loading" them by execfile just throws an exception ? Any suggestions Bo.
33
1971
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...
0
2020
by: Andrew Lambert | last post by:
Hi, I've been trying to compile perl scripts into bytecode. Now, from what I understand I can use either perlcc -B script.pl or perl -MO=bytecode script.pl to do this (whats the difference between these two methods anyway ?). If I use perl -MO=bytecode, both versions I've tried dump seemingly random ascii characters to the screen....
4
2208
by: Ben | last post by:
Does anyone have any experience using the TrueType bytecode interpreter to display fonts in PHP under Windows? I'm running Windows XP, PHP 4.3.11, GD 2.0.28 with FreeType enabled. It all works fine, but it's using the FreeType anti-aliased hinting rather than the clear Windows non-antialiased (bytecode interpreter?) hinting. I'm looking...
0
1190
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.
3
3242
by: Rob De Almeida | last post by:
Hi, I would like to compile an AST to bytecode, so I can eval it later. I tried using parse.compileast, but it fails: Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: compilest() argument 1 must be parser.st, not instance Any hints?
4
3287
by: kwatch | last post by:
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:
3
2666
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",...
0
7457
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...
0
7651
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. ...
0
7802
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7410
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...
0
7746
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 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...
1
5320
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...
0
4941
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...
0
3438
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
693
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...

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.