I would infer that it's insecure, since if it were that easy there wouldn't be various abandoned projects trying to sandbox Python.
It's the curse of any sufficiently useful language. Well, maybe not Lua, but that was specifically designed for embedding. Java also began with that intention back when applets were ahead of their time, though IIRC secure sandboxing is no longer really a feature.
Don't do it. Really, really don't do it. People have tried for decades to develop such a sandbox, and it does not work.
I'm interested in an answer. Is there a way, by design, to run code from an untrusted source in a restricted manner? So the worse the code could do is call me rude names.
I agree with eesmith. DONT DO IT. I guess you got your answer restrictedPy
Relevant:
https://discuss.python.org/t/extending-subinterpreters-with-...
https://stackoverflow.com/questions/3068139
https://wiki.python.org/moin/SandboxedPython
https://github.com/jailctf/pyjailbreaker
https://healeycodes.com/running-untrusted-python-code
https://lwn.net/Articles/574215/