Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #31628
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2012-10-18 04:41 -0700 |
| Message-ID | <2f12fa83-54cc-4fc2-85e4-b8aebebf4242@googlegroups.com> (permalink) |
| Subject | use of exec() |
| From | lars van gemerden <lars@rational-it.com> |
I am trying to implement a way to let users give a limited possibility to define functions in text, that wille be stored and executed at a later time. I use exec() to transform the text to a function. The code is as follows:
class code(str):
def __call__(self, *args):
try:
return self._func_(*args)
except AttributeError:
self._func_ = self._creat_func_()
return self._func_(*args)
def __getstate__(self):
return {}
class _lambdacode(code):
def _creat_func_(self):
return eval("lambda %s: %s" % (", ".join(type(self).args), self),{},{})
class _functioncode(code):
def _creat_func_(self):
exec("def function(%s):\n\t%s" % (", ".join(type(self).args),
"\n\t".join(self.split('\n'))))
return function
def lambdatype(*argnames):
return type('lambdacode', (_lambdacode,),{'args': argnames})
def functiontype(*argnames):
return type('functioncode', (_functioncode,),{'args': argnames})
if __name__ == '__main__':
f = lambdatype('a', 'b')('a ** b')
print f
print f(3, 4)
print f(4, 3)
g = functiontype('a', 'b')('a, b = a/b, a*b\nreturn a ** b')
print g
print g(3.0, 4.0)
print g(4.0, 3.0)
This code works. But if I replace _functioncode with:
class _functioncode(code):
def _creat_func_(self):
exec("def function(%s):\n\t%s" % (", ".join(type(self).args),
"\n\t".join(self.split('\n'))),{})
return function
or
class _functioncode(code):
def _creat_func_(self):
exec("def function(%s):\n\t%s" % (", ".join(type(self).args),
"\n\t".join(self.split('\n'))),{},{})
return function
to restrict access to global variables, similar to the lambda version, i get the error:
Traceback (most recent call last):
File "D:\Documents\Code\Eclipse\workspace\FlowWare_1\toolshed\tests\mini_test.py", line 41, in <module>
print g(3.0, 4.0)
File "D:\Documents\Code\Eclipse\workspace\FlowWare_1\toolshed\tests\mini_test.py", line 13, in __call__
self._func_ = self._creat_func_()
File "D:\Documents\Code\Eclipse\workspace\FlowWare_1\toolshed\tests\mini_test.py", line 25, in _creat_func_
return function
NameError: name 'function' is not defined
which seems an odd error, but i think some global variable is necessary for this to work (if i put in globals() instead of {}, it works).
My question is which variable or if that is not the problem, what is and how can i restrict access the user code has.
Cheers, Lars
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
use of exec() lars van gemerden <lars@rational-it.com> - 2012-10-18 04:41 -0700
Re: use of exec() Chris Angelico <rosuav@gmail.com> - 2012-10-18 22:49 +1100
Re: use of exec() lars van gemerden <lars@rational-it.com> - 2012-10-18 07:07 -0700
Re: use of exec() Chris Angelico <rosuav@gmail.com> - 2012-10-19 01:29 +1100
Re: use of exec() lars van gemerden <lars@rational-it.com> - 2012-10-18 08:00 -0700
Re: use of exec() Chris Angelico <rosuav@gmail.com> - 2012-10-19 02:16 +1100
Re: use of exec() lars van gemerden <lars@rational-it.com> - 2012-10-19 16:43 -0700
Re: use of exec() Chris Angelico <rosuav@gmail.com> - 2012-10-20 13:00 +1100
Re: use of exec() lars van gemerden <lars@rational-it.com> - 2012-10-20 03:41 -0700
Re: use of exec() lars van gemerden <lars@rational-it.com> - 2012-10-20 03:41 -0700
Re: use of exec() lars van gemerden <lars@rational-it.com> - 2012-10-19 16:43 -0700
Re: use of exec() lars van gemerden <lars@rational-it.com> - 2012-10-18 08:00 -0700
Re: use of exec() lars van gemerden <lars@rational-it.com> - 2012-10-18 07:07 -0700
csiph-web