SystemError while execing bad code

Peter Hansen peter at engcorp.com
Tue Jan 20 11:39:04 EST 2004


Gerrit Holl wrote:
> 
> I found a cool way to trigger a SystemError:
> 
> >>> exec CodeType(0,0,0,0,"",(),(),(),"","",0,"")
> XXX lineno: 0, opcode: 0
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "/usr/lib/python2.2/site-packages/", line 0, in
>     File "/usr/lib/python2.2/site-packages/", line 0, in
> SystemError: unknown opcode
> 
> The documentation says:
> You should report this to the author or maintainer of your Python
> interpreter. Be sure to report the version of the Python interpreter
> (sys.version; it is also printed at the start of an interactive Python
> session), the exact error message (the exception's associated value) and
> if possible the source of the program that triggered the error.
> 
> Although it probably shouldn't be taken literally in this case...
> ...or should it :-)?
> 
> Hmm, and this actually *hangs*:
> >>> exec CodeType(0,0,0,0,"",(),(),(),"","",2**31-1,"")
> XXX lineno: 2147483647, opcode: 0
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "/usr/lib/python2.2/site-packages/", line 2147483647, in
> 
> ...
> 
> But I guess this is a case of "so don't do it" :-)?

I once experimented, briefly, with doing "genetic programming" (evolving
programs to solve problems) using Python bytecode.  In effect, this meant
that random sequences of bytes were being executed as though they were 
valid compiled Python programs.

The interpreter crashed rapidly and frequently.  I quickly concluded
that the interpreter is designed to be safe in executing bytecode that
is generated by a proper Python compiler, and that the compiler did not
generate random bytecodes most of the time.

Had to abandon that idea, though in principle it's still very cool.

-Peter



More information about the Python-list mailing list