Get origin path from a module spec, checking has_location first.
(
spec: &Option<PyObjectRef>,
vm: &VirtualMachine,
)
| 262 | |
| 263 | /// Get origin path from a module spec, checking has_location first. |
| 264 | pub(crate) fn get_spec_file_origin( |
| 265 | spec: &Option<PyObjectRef>, |
| 266 | vm: &VirtualMachine, |
| 267 | ) -> Option<String> { |
| 268 | let spec = spec.as_ref()?; |
| 269 | let has_location = spec |
| 270 | .get_attr("has_location", vm) |
| 271 | .ok() |
| 272 | .and_then(|v| v.try_to_bool(vm).ok()) |
| 273 | .unwrap_or(false); |
| 274 | if !has_location { |
| 275 | return None; |
| 276 | } |
| 277 | spec.get_attr("origin", vm).ok().and_then(|origin| { |
| 278 | if vm.is_none(&origin) { |
| 279 | None |
| 280 | } else { |
| 281 | origin |
| 282 | .downcast_ref::<PyStr>() |
| 283 | .and_then(|s| s.to_str().map(|s| s.to_owned())) |
| 284 | } |
| 285 | }) |
| 286 | } |
| 287 | |
| 288 | /// Check if a module file possibly shadows another module of the same name. |
| 289 | /// Compares the module's directory with the original sys.path[0] (derived from sys.argv[0]). |