473,765 Members | 2,021 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 4914
"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
6287
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
3110
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
2025
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...
0
2029
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. ActivePerl-5.8 completes with 'script syntax OK' while the standard redhat perl completes with 'No...
4
2225
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 into converting my weather graph generator (at http://harvest.com/w.cgi), which now uses the Win32 API...
0
1203
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
3253
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
3298
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
2676
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:"
0
9404
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,...
0
10164
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9959
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,...
0
9835
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7379
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5277
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...
1
3926
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
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.