Compiling Python code within a module

M

Mitko Haralanov

For various reason, what I need to do is be able to send some Python
code (mostly entire functions in the form of a string) to a remote
server (written in Python), have that server compile the code and
insert it in the local namespace so it is available to be called at a
later time.

I have gotten the sending and receiving part already written and that
works. However, I can't get the compiling part! I have looked at the
compile module and while it is able to compile the code I am not
entirely sure what to do with the returned code object so it get's
inserted as a local function.

I would appreciate any help that you guys might be able to offer?

Thanks

--
Mitko Haralanov (e-mail address removed)
Senior Software Engineer 650.934.8064
System Interconnect Group http://www.qlogic.com

==========================================
The "cutting edge" is getting rather dull.
-- Andy Purshottam
 
I

ici

For various reason, what I need to do is be able to send some Python
code (mostly entire functions in the form of a string) to a remote
server (written in Python), have that server compile the code and
insert it in the local namespace so it is available to be called at a
later time.

I have gotten the sending and receiving part already written and that
works. However, I can't get the compiling part! I have looked at the
compile module and while it is able to compile the code I am not
entirely sure what to do with the returned code object so it get's
inserted as a local function.

I would appreciate any help that you guys might be able to offer?

Thanks

--
Mitko Haralanov (e-mail address removed)
Senior Software Engineer 650.934.8064
System Interconnect Group http://www.qlogic.com

==========================================
The "cutting edge" is getting rather dull.
-- Andy Purshottam

exec it :)

--- Example---
exec(compile("""
def test():
import os
for i in os.listdir('.'):
print i
""",'<string>', 'exec'))

test()
--- End example---
Now you have test() function available in namespace where executed
example

Po-zdravi
 
M

Mitko Haralanov


Thank you! That works when I compile/exec it in the main body of the
program. However, when I try to do that in a separate module it
doesn't. For example:

Module Foo:
import compiler

class Foo:
def __init__ (self):
return
def register (self, text):
exec (compiler.compile (text, "<string>",
"exec"))

File Bar:
import Foo

f = Foo.Foo ()
f.register ("def blah():\n\tprint 'blah'\n")

In this case, the function 'blah' is nowhere to be found. I've tried
looking in 'f', in the class 'Foo', in the module 'Foo' and it's
nowhere.

--
Mitko Haralanov (e-mail address removed)
Senior Software Engineer 650.934.8064
System Interconnect Group http://www.qlogic.com

==========================================
((lambda (foo) (bar foo)) (baz))
 
G

Gabriel Genellina

Thank you! That works when I compile/exec it in the main body of the
program. However, when I try to do that in a separate module it
doesn't. For example:

exec has a long form - see http://docs.python.org/ref/exec.html
And forget the compile pass - let Python handle it automatically. Also
note that exec is a statement, not a function, so you don't need ()

To "inject" inside a module a function defined in source_code, do:

exec source_code in module.__dict__

To "inject" a function inside a class, it's easier if you use an
intermediate dictionary:

d = globals().copy()
exec source_code in d
my_class.method = d['method']
def register (self, text):
exec (compiler.compile (text, "<string>",
"exec"))

f.register ("def blah():\n\tprint 'blah'\n")

In this case, the function 'blah' is nowhere to be found. I've tried
looking in 'f', in the class 'Foo', in the module 'Foo' and it's
nowhere.

It existed briefly in the local namespace of the register method - after
register is exited, it's gone.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top