MCPcopy Index your code
hub / github.com/RustPython/RustPython / get_spec_file_origin

Function get_spec_file_origin

crates/vm/src/import.rs:264–286  ·  view source on GitHub ↗

Get origin path from a module spec, checking has_location first.

(
    spec: &Option<PyObjectRef>,
    vm: &VirtualMachine,
)

Source from the content-addressed store, hash-verified

262
263/// Get origin path from a module spec, checking has_location first.
264pub(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]).

Callers 2

import_fromMethod · 0.85
getattr_innerMethod · 0.85

Calls 8

okMethod · 0.80
try_to_boolMethod · 0.80
to_strMethod · 0.80
as_refMethod · 0.45
get_attrMethod · 0.45
is_noneMethod · 0.45
mapMethod · 0.45
to_ownedMethod · 0.45

Tested by

no test coverage detected