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

Method try_load_from_python_environ

crates/stdlib/src/ssl.rs:1331–1360  ·  view source on GitHub ↗

Helper: Try to load certificates from Python's os.environ variables Returns true if certificates were successfully loaded. We use Python's os.environ instead of Rust's std::env because Python code can modify os.environ at runtime (e.g., `os.environ['SSL_CERT_FILE'] = '/path'`), but rustls-native-certs uses std::env which only sees the process environment at startup.

(
            &self,
            loader: &mut cert::CertLoader<'_>,
            vm: &VirtualMachine,
        )

Source from the content-addressed store, hash-verified

1329 /// `os.environ['SSL_CERT_FILE'] = '/path'`), but rustls-native-certs uses
1330 /// std::env which only sees the process environment at startup.
1331 fn try_load_from_python_environ(
1332 &self,
1333 loader: &mut cert::CertLoader<'_>,
1334 vm: &VirtualMachine,
1335 ) -> PyResult<bool> {
1336 use std::path::Path;
1337
1338 let os_module = vm.import("os", 0)?;
1339 let environ = os_module.get_attr("environ", vm)?;
1340
1341 // Try SSL_CERT_FILE first
1342 if let Ok(cert_file) = Self::get_env_path(&environ, "SSL_CERT_FILE", vm)
1343 && Path::new(&cert_file).exists()
1344 && let Ok(stats) = loader.load_from_file(&cert_file)
1345 {
1346 self.update_cert_stats(stats);
1347 return Ok(true);
1348 }
1349
1350 // Try SSL_CERT_DIR (only if SSL_CERT_FILE didn't work)
1351 if let Ok(cert_dir) = Self::get_env_path(&environ, "SSL_CERT_DIR", vm)
1352 && Path::new(&cert_dir).is_dir()
1353 && let Ok(stats) = loader.load_from_dir(&cert_dir)
1354 {
1355 self.update_cert_stats(stats);
1356 return Ok(true);
1357 }
1358
1359 Ok(false)
1360 }
1361
1362 /// Helper: Load system certificates using rustls-native-certs
1363 ///

Callers 1

load_default_certsMethod · 0.80

Calls 8

newFunction · 0.85
load_from_fileMethod · 0.80
update_cert_statsMethod · 0.80
load_from_dirMethod · 0.80
importMethod · 0.45
get_attrMethod · 0.45
existsMethod · 0.45
is_dirMethod · 0.45

Tested by

no test coverage detected