Create a restricted __import__ that only allows whitelisted modules.
()
| 153 | |
| 154 | |
| 155 | def _make_safe_import(): |
| 156 | """Create a restricted __import__ that only allows whitelisted modules.""" |
| 157 | def safe_import(name, *args, **kwargs): |
| 158 | ok, err = _is_safe_import_name(name) |
| 159 | if ok: |
| 160 | return _builtins_mod.__import__(name, *args, **kwargs) |
| 161 | raise ImportError(err or f"Import not allowed: {name}") |
| 162 | return safe_import |
| 163 | |
| 164 | |
| 165 | def build_safe_builtins(extra_allowed: Optional[Set[str]] = None) -> Dict[str, Any]: |
no outgoing calls
no test coverage detected