I'm very new to python. Here is the problem Im having.
I have hooked the builtin._import_ with my custom hook which loads a module from a string.
def import_hook(name, globals=None, locals=None, fromlist=None):
if name in sys.modules:
obj = sys.modules[name]
return obj
#Make sure we hook only for modules which start with ajay
if name.startswith("ajay"):
statement = '''
print 'inside the ajay module'
def ajay_func():
print 'inside the func'
'''
mod = imp.new_module(name)
mod.__file__ = name
compiled = compile(statement, '<string>', 'exec')
exec compiled in mod.__dict__
sys.modules[name] = mod
return mod
return original_import(name, globals, locals, fromlist)
Then I use this hook in function which is loading a module and calling its function in exec statement.
original_import = __builtin__.__import__
def test(request):
statement = '''
import sys
import ajay
def ILessons_1(request):
ajay.ajay_func()
'''
try:
__builtin__.__import__ = import_hook
compiled = compile(statement, '<string>', 'exec')
exec (compiled, globals(), locals()) #in statement_module.__dict__
ajay.ajay_func()
return ILessons_1(request);
finally:
__builtin__.__import__ = original_import
pass
When I run this code I get error "global name 'ajay' is not defined"in line "return ILessons_1(request);". Interesting thing is python is able to resolve ajay in line just above this line. Im pretty sure Im making some mistake in exec statement but have not been able to figure out.
Can some please help me solve this problem.
Thanks
Noted few issues here:
1) globals and locals are built-in function names, You should not use them as variable names.
2) Possibly a bug? (tested with python 2.7.1 under ubuntu)
Consider following code (note the exec statement):
def outer_function():
foo = "foobar"
statement = '''
def inner_function():
print foo
'''
#exec statement in globals(), locals()
exec statement in globals().update(locals())
inner_function()
outer_function()
Here commented string (exec with 2 arguments after in) will not work as described at in the documentation and result in:
NameError: global name 'foo' is not defined
But one may manually combine globals+locals an pass them to exec (next string after comment in my example). Looks like a workaround to me.
Related
If I import a module that uses exec or eval, is it possible to give it access to the main program?
myExec.py
def myExec(code):
exec(code)
main.py
import myExec
def test():
print("ok")
myExec.myExec("test()")
Yes!
exec has a few optional parameters, globals and locals. These basically tell it what global and local variables its allowed to use, in the form of a dictionary. Calling globals() or locals() functions returns the dictionary with all the global and local variables where you are calling from, so you can use:
myExec.py:
def myExec(code, globals_=None, locals_=None): # the trailing underscore is so that there are no name conflicts
exec(code, globals_, locals_)
main.py:
import myExec
def test():
print("ok")
myExec.myExec("test()", globals())
I want to replace some builtin functions inside the code that I run with exec. It is possible by passing it as a dictionary entry in the second exec argument. But when I try to import a module inside the executed code, the functions are as in original bultins, when called inside imported module.
This is the example of what I'm trying to achieve:
from inspect import cleandoc
def new_print(val):
print('Hello', val)
code_inner = cleandoc("""
def bar():
print('Inner')
""")
with open('inner.py', 'w') as f:
f.write(code_inner)
code_outer = cleandoc("""
import inner
print('Outer')
inner.bar()
""")
exec(code_outer, {'print': new_print}, {})
This is the response that I receive:
Hello Outer
Inner
And this is what I would like to have:
Hello Outer
Hello Inner
Is there any way to pass new globals, or builtins, or maybe variable list to the module beeing imported?
I'm not sure if it's quite what you want, but passing a dictionary parameter to the function and updating its modules globals works.
code_inner = cleandoc("""
def bar(d):
globals().update(d)
print('Inner')
""")
code_outer = cleandoc("""
import inner
print('Outer')
inner.bar({'print': print})
""")
Alternatively, without modification of the bar function you can pass its module a global like so:
code_outer = cleandoc("""
import inner
inner.print = print
print('Outer')
inner.bar()
""")
package_path = '/home/foo/bar/'
module_class = 'hello.World'
method = 'something'
first, i want to import the hello module what it is inside of the package_path (/home/foo/bar/)
then, i must instantiate the class World at this module
and finally i should run the something() method on the new world_instance_object
any idea for do that?
Thank you so much!
To avoid having to manipulate sys.path and to keep all the extra modules you import from cluttering up sys.modules, I'd suggest using execfile() for this. Something like this (untested code):
import os.path
def load_and_call(package_path, module_class, method_name):
module_name, class_name = module_class.split(".")
module_globals = {}
execfile(os.path.join(package_path, module_name + ".py"), module_globals)
return getattr(module_globals[class_name](), method_name, lambda: None)()
You could add some code to cache the parsed objects using a dictionary, if a file will be used more than once (again, untested):
def load_and_call(package_path, module_class, method_name, _module_cache={}):
module_name, class_name = module_class.split(".")
py_path = os.path.join(package_path, module_name + ".py")
module_globals = _module_cache.setdefault(py_path, {})
if not module_globals:
execfile(py_path, module_globals)
return getattr(module_globals[class_name](), method_name, lambda: None)()
I've got a function here for running an external python script in another process. m is the Multiprocessing module
def run(app,WindowOffX,WindowOffY,WindowWidth,WindowHeight):
try:
exec("import Programs."+app+".main as Foo")
Foo.init()
p = m.Process(target=Foo.main(WindowOffX,WindowOffY,WindowWidth,WindowHeight))
except ImportError:
print("That app doesn't exist!!! O.O")
But this generates NameError: global name 'Foo' is not defined. Can someone help?
Ha, the problem is that python doesn't know your exec statement defines Foo, so it tries to look it up as a global. To clue it in, try this:
try:
Foo = None
exec("import Programs."+app+".main as Foo")
Foo.init()
Incidentally, here's how you can do what you're after without using exec:
Foo = __import__("Programs."+app+".main")
I decided to try to preprocess function text before it's compilation into byte-code and following execution. This is merely for training. I hardly imagine situations where it'll be a satisfactory solution to be used. I have faced one problem which I wanted to solve in this way, but eventually a better way was found. So this is just for training and to learn something new, not for real usage.
Assume we have a function, which source code we want to be modified quite a bit before compilation:
def f():
1;a()
print('Some statements 1')
1;a()
print('Some statements 2')
Let, for example, mark some lines of it with 1;, for them to be sometimes commented and sometimes not. I just take it for example, modifications of the function may be different.
To comment these lines I made a decorator. The whole code it bellow:
from __future__ import print_function
def a():
print('a()')
def comment_1(s):
lines = s.split('\n')
return '\n'.join(line.replace(';','#;',1) if line.strip().startswith('1;') else line for line in lines)
def remove_1(f):
import inspect
source = inspect.getsource(f)
new_source = comment_1(source)
with open('temp.py','w') as file:
file.write(new_source)
from temp import f as f_new
return f_new
def f():
1;a()
print('Some statements 1')
1;a()
print('Some statements 2')
f = remove_1(f) #If decorator #remove is used above f(), inspect.getsource includes #remove inside the code.
f()
I used inspect.getsourcelines to retrieve function f code. Then I made some text-processing (in this case commenting lines starting with 1;). After that I saved it to temp.py module, which is then imported. And then a function f is decorated in the main module.
The output, when decorator is applied, is this:
Some statements 1
Some statements 2
when NOT applied is this:
a()
Some statements 1
a()
Some statements 2
What I don't like is that I have to use hard drive to load compiled function. Can it be done without writing it to temporary module temp.py and importing from it?
The second question is about placing decorator above f: #replace. When I do this, inspect.getsourcelines returns f text with this decorator. I could manually be deleted from f's text. but that would be quite dangerous, as there may be more than one decorator applied. So I resorted to the old-style decoration syntax f = remove_1(f), which does the job. But still, is it possible to allow normal decoration technique with #replace?
One can avoid creating a temporary file by invoking the exec statement on the source. (You can also explicitly call compile prior to exec if you want additional control over compilation, but exec will do the compilation for you, so it's not necessary.) Correctly calling exec has the additional benefit that the function will work correctly if it accesses global variables from the namespace of its module.
The problem described in the second question can be resolved by temporarily blocking the decorator while it is running. That way the decorator remains, along all the other ones, but is a no-op.
Here is the updated source.
from __future__ import print_function
import sys
def a():
print('a()')
def comment_1(s):
lines = s.split('\n')
return '\n'.join(line.replace(';','#;',1) if line.strip().startswith('1;') else line for line in lines)
_blocked = False
def remove_1(f):
global _blocked
if _blocked:
return f
import inspect
source = inspect.getsource(f)
new_source = comment_1(source)
env = sys.modules[f.__module__].__dict__
_blocked = True
try:
exec new_source in env
finally:
_blocked = False
return env[f.__name__]
#remove_1
def f():
1;a()
print('Some statements 1')
1;a()
print('Some statements 2')
f()
def remove_1(f):
import inspect
source = inspect.getsource(f)
new_source = comment_1(source)
env = sys.modules[f.__module__].__dict__.copy()
exec new_source in env
return env[f.__name__]
I'll leave a modified version of the solution given in the answer by user4815162342. It uses ast module to delete some parts of f, as was suggested in the comment to the question. To make it I majorly relied on the information in this article.
This implementation deletes all occurrences of a as standalone expression.
from __future__ import print_function
import sys
import ast
import inspect
def a():
print('a() is called')
_blocked = False
def remove_1(f):
global _blocked
if _blocked:
return f
import inspect
source = inspect.getsource(f)
a = ast.parse(source) #get ast tree of f
class Transformer(ast.NodeTransformer):
'''Will delete all expressions containing 'a' functions at the top level'''
def visit_Expr(self, node): #visit all expressions
try:
if node.value.func.id == 'a': #if expression consists of function with name a
return None #delete it
except(ValueError):
pass
return node #return node unchanged
transformer = Transformer()
a_new = transformer.visit(a)
f_new_compiled = compile(a_new,'<string>','exec')
env = sys.modules[f.__module__].__dict__
_blocked = True
try:
exec(f_new_compiled,env)
finally:
_blocked = False
return env[f.__name__]
#remove_1
def f():
a();a()
print('Some statements 1')
a()
print('Some statements 2')
f()
The output is:
Some statements 1
Some statements 2