Found a solution:
I must change the strings to contains body of functions:
str = "(lambda x: __import__('pwd').getpwuid(x))(1000)"
eval() on this is ok!
(lambda or def obviously are the same)
题
I have a string like:
str = "pwd.getpwuid(1000)"
Now, if I try to eval() that it may raise an exception because I have not import pwd yet (or maybe not if I have).
So I decided to write a parser that: splits that string on "." and get a list:
lis = ["pwd", "getpwuid(1000)"]
then take lis[0]
, if it does not contain "("
or ")"
I call
importlib.import_module(lis[0])
and then again the eval.
Can I make the same thing better?
解决方案 2
Found a solution:
I must change the strings to contains body of functions:
str = "(lambda x: __import__('pwd').getpwuid(x))(1000)"
eval() on this is ok!
(lambda or def obviously are the same)
其他提示
how about splitting the positionnal args of the function in lis[2] and the named args in lis[3] with some regex and doing
lib = importlib.import_module(lis[0])
lib.__dict__[lis[1]](*list[2],**list[3])