Executing bytecode from a string.

Peter Hansen peter at engcorp.com
Wed Aug 11 14:04:23 CEST 2004

Michael Hudson wrote:
> "Benjamin Scherrey" <scherrey at proteus-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

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


More information about the Python-list mailing list