How to safely run unreliable piece of code?

前端 未结 3 1883
遇见更好的自我
遇见更好的自我 2020-12-31 16:54

Suppose you are working with some bodgy piece of code which you can\'t trust, is there a way to run it safely without losing control of your script?

An example mig

3条回答
  •  执念已碎
    2020-12-31 17:40

    To be safe against exceptions, use try/except (but I guess you know that).

    To be safe against hanging code (endless loop) the only way I know is running the code in another process. This child process you can kill from the father process in case it does not terminate soon enough.

    To be safe against nasty code (doing things it shall not do), have a look at http://pypi.python.org/pypi/RestrictedPython .

提交回复
热议问题