Coverage for src/pyrsslocal/helper/python_run.py: 90%

10 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2023-02-02 02:59 +0100

1""" 

2@file 

3@brief contains functionalities to run a python script 

4""" 

5 

6 

7def run_python_script(script, params=None): 

8 """ 

9 Executes a script python as a string. 

10 

11 @param script python script 

12 @param params params to add before the execution 

13 

14 .. exref:: 

15 :title: compile and run a custom script 

16 

17 :: 

18 

19 fpr = lambda v : self.outStream.write(str(v) + "\n") 

20 pars = {"print": fpr, "another_variable": 3 } 

21 run_python_script(script, pars) 

22 """ 

23 if params is None: 

24 params = {} 

25 obj = compile(script, "", "exec") 

26 

27 loc = locals() 

28 for k, v in params.items(): 

29 loc[k] = v 

30 loc["__dict__"] = params 

31 

32 try: 

33 exec(obj, globals(), loc) 

34 except Exception as e: # pragma: no cover 

35 raise e