Question about compiled bytecode

Mr. Neutron nicktsocanos at charter.net
Wed Aug 21 13:23:13 EDT 2002


On Wed, 21 Aug 2002 12:08:47 -0400, Skip Montanaro wrote:


>     nick> I am working on a project that needs to be able to trasfer
>     nick> compiled bytecode between interpreters. I can not transfer
>     nick> bytecode directly over a socket as Python will barf on this.
> 
> Why not?  I transfer binary data over sockets all the time.  Can you
> post some barf and the code that generated it?
> 
I am still very new to Python, but I will explain my code and
will tell you the error it generates

I have a dictionary of "name":code. Code is compiled python files
(it is really a python program, or can be a python object. It 
doesn't matter. Code is just a bunch of python statements compiled
using compile(...).

The python statement I use to compile is:

  procs[key] = compile(code_source, file, 'exec')
  ...
  later on, I do a little voodoo
  redirect stdio to connection.makefile(...)
  exec(procs[key]) and boop! it runs the program over the socket
  reset stdout again

  (there is a little more voodoo, because it is in a thread, so
   I use a cheap gimmick by using a thread.allocate_lock() object.
   This actually really sucks, because I will have many threads
   running at once and stdio gets blocked when i do this. I am
   working on some way to get around it... any ideas?)

I have a regular socket open. It is called connection. Then I try this:

connection.send(procs[key])

Then I get:
Unhandled exception in thread:
Traceback (most recent call last):
  File "/home/njt/PyAGENT/PyAGENT.py", line 190, in handleConnection
    connection.send(procs[name])
TypeError: send() argument 1 must be string or read-only buffer, not code


I have worked around the problem by just sending the source code
and let the other end compile it, and then it works just fine.
It would be really nice though if I could just send compiled objects
and that's it the remote end can exec(object) and it runs in my
interpreter.

I will look at marshall now, it may work for what I need to do. 
Pickle does not do what I need I don't think. Although I am going
to use pickle with this in the future. What I really need is a way
to compile an object AND save it's state. This would be the
most useful for my program. Right now I am doing cheap gimmicks
to get around this. Again I am really new to Python, so this is my
first real program to try to do something interesting with Python 
besides just scripting FTP sites and things. I dunno if I can
do what I want to do with it at all yet but I under the impression
I can because Grail runs python applets. I have not been able
to fully understand how Grail works though (all this RExec stuff
and Bastion, which I will be using later on too..)

Thanks



More information about the Python-list mailing list