Extract _safe_path
Use compat for Python 3 detection
Move Python 3 code to the body of the function
Also override extend to check paths for safety.