diff --git a/pyembed/src/pyalloc.rs b/pyembed/src/pyalloc.rs index a2bc84eae..6aad9278c 100644 --- a/pyembed/src/pyalloc.rs +++ b/pyembed/src/pyalloc.rs @@ -88,7 +88,9 @@ extern "C" fn raw_realloc(ctx: *mut c_void, ptr: *mut c_void, new_size: size_t) let layout = alloc::Layout::from_size_align_unchecked(new_size, MIN_ALIGN); let key = ptr as *mut u8; - let old_layout = (*state).remove(&key).expect("original memory address not tracked"); + let old_layout = (*state) + .remove(&key) + .expect("original memory address not tracked"); let res = alloc::realloc(ptr as *mut u8, old_layout, new_size); @@ -108,7 +110,9 @@ extern "C" fn raw_free(ctx: *mut c_void, ptr: *mut c_void) -> () { let state = ctx as *mut RawAllocatorState; let key = ptr as *mut u8; - let layout = (*state).get(&key).expect(format!("could not find allocated memory record: {:?}", key).as_str()); + let layout = (*state) + .get(&key) + .expect(format!("could not find allocated memory record: {:?}", key).as_str()); alloc::dealloc(key, *layout); (*state).remove(&key); diff --git a/pyembed/src/pyinterp.rs b/pyembed/src/pyinterp.rs index 35904daa4..5eb7b7980 100644 --- a/pyembed/src/pyinterp.rs +++ b/pyembed/src/pyinterp.rs @@ -8,7 +8,9 @@ use std::ffi::CString; use std::path::PathBuf; use std::ptr::null; -use cpython::{NoArgs, ObjectProtocol, PyErr, PyModule, PyObject, PyResult, PythonObject, Python, ToPyObject}; +use cpython::{ + NoArgs, ObjectProtocol, PyErr, PyModule, PyObject, PyResult, Python, PythonObject, ToPyObject, +}; use pyffi; use crate::data::*; @@ -113,16 +115,12 @@ fn stdin_to_file() -> *mut libc::FILE { // (_open_osfhandle() + _fdopen() might work), using the same function // that uses to obtain a FILE* seems like the least risky thing // to do. - unsafe { - __acrt_iob_func(0) - } + unsafe { __acrt_iob_func(0) } } #[cfg(unix)] fn stdin_to_file() -> *mut libc::FILE { - unsafe { - libc::fdopen(libc::STDIN_FILENO, &('r' as libc::c_char)) - } + unsafe { libc::fdopen(libc::STDIN_FILENO, &('r' as libc::c_char)) } } /// Represents an embedded Python interpreter. @@ -161,7 +159,7 @@ impl MainPythonInterpreter { pub fn init(&mut self, py: Python) { // TODO return Result<> and don't panic. if self.init_run { - return + return; } let config = &self.config; @@ -169,7 +167,10 @@ impl MainPythonInterpreter { if let Some(raw_allocator) = &self.raw_allocator { unsafe { let ptr = &raw_allocator.allocator as *const _; - pyffi::PyMem_SetAllocator(pyffi::PyMemAllocatorDomain::PYMEM_DOMAIN_RAW, ptr as *mut _); + pyffi::PyMem_SetAllocator( + pyffi::PyMemAllocatorDomain::PYMEM_DOMAIN_RAW, + ptr as *mut _, + ); // TODO call this if memory debugging enabled. //pyffi::PyMem_SetupDebugHooks(); @@ -186,7 +187,10 @@ impl MainPythonInterpreter { // Register our _pymodules extension which exposes modules data. unsafe { // name char* needs to live as long as the interpreter is active. - pyffi::PyImport_AppendInittab(PYMODULES_NAME.as_ptr() as *const i8, Some(PyInit__pymodules)); + pyffi::PyImport_AppendInittab( + PYMODULES_NAME.as_ptr() as *const i8, + Some(PyInit__pymodules), + ); } } @@ -204,12 +208,17 @@ impl MainPythonInterpreter { pyffi::Py_SetProgramName(program_name.into()); } - if let (Some(ref encoding), Some(ref errors)) = (&config.standard_io_encoding, &config.standard_io_errors) { + if let (Some(ref encoding), Some(ref errors)) = + (&config.standard_io_encoding, &config.standard_io_errors) + { let cencoding = CString::new(encoding.clone()).unwrap(); let cerrors = CString::new(errors.clone()).unwrap(); let res = unsafe { - pyffi::Py_SetStandardStreamEncoding(cencoding.as_ptr() as *const i8, cerrors.as_ptr() as *const i8) + pyffi::Py_SetStandardStreamEncoding( + cencoding.as_ptr() as *const i8, + cerrors.as_ptr() as *const i8, + ) }; if res != 0 { @@ -300,13 +309,12 @@ impl MainPythonInterpreter { } pub fn run(&mut self) { - let py = unsafe { - Python::assume_gil_acquired() - }; + let py = unsafe { Python::assume_gil_acquired() }; self.init(py); - py.eval("import re, sys; from black import main; main()", None, None).unwrap(); + py.eval("import re, sys; from black import main; main()", None, None) + .unwrap(); //py.eval("print(\"hello, world\")", None, None).unwrap(); //py.import("__main__").unwrap(); @@ -316,15 +324,17 @@ impl MainPythonInterpreter { /// /// Returns the execution result of the module code. pub fn run_module_as_main(&mut self, name: &str) -> PyResult { - let py = unsafe { - Python::assume_gil_acquired() - }; + let py = unsafe { Python::assume_gil_acquired() }; self.init(py); // This is modeled after runpy.py:_run_module_as_main(). let main: PyModule = unsafe { - PyObject::from_owned_ptr(py, pyffi::PyImport_AddModule("__main__\0".as_ptr() as *const c_char)).cast_into(py)? + PyObject::from_owned_ptr( + py, + pyffi::PyImport_AddModule("__main__\0".as_ptr() as *const c_char), + ) + .cast_into(py)? }; let main_dict = main.dict(py); @@ -363,9 +373,7 @@ impl MainPythonInterpreter { /// /// This emulates what CPython's main.c does. pub fn run_repl(&mut self) -> PyResult { - let py = unsafe { - Python::assume_gil_acquired() - }; + let py = unsafe { Python::assume_gil_acquired() }; self.init(py); @@ -383,15 +391,13 @@ impl MainPythonInterpreter { match sys.get(py, "__interactivehook__") { Ok(hook) => { hook.call(py, NoArgs, None)?; - }, + } Err(_) => (), }; let stdin_filename = ""; let filename = CString::new(stdin_filename).expect("could not create CString"); - let mut cf = pyffi::PyCompilerFlags { - cf_flags: 0, - }; + let mut cf = pyffi::PyCompilerFlags { cf_flags: 0 }; // TODO use return value. unsafe { @@ -405,8 +411,6 @@ impl MainPythonInterpreter { impl Drop for MainPythonInterpreter { fn drop(&mut self) { - let _ = unsafe { - pyffi::Py_FinalizeEx() - }; + let _ = unsafe { pyffi::Py_FinalizeEx() }; } } diff --git a/pyembed/src/pymodules_module.rs b/pyembed/src/pymodules_module.rs index 4d86ea646..98f49cd3d 100644 --- a/pyembed/src/pymodules_module.rs +++ b/pyembed/src/pymodules_module.rs @@ -4,19 +4,18 @@ /// This module defines the _pymodules Python module, which exposes /// .py/.pyc source/code data so it can be used by an in-memory importer. - use std::collections::{HashMap, HashSet}; use std::io::Cursor; -use byteorder::{ReadBytesExt, LittleEndian}; -use pyffi::{PyBUF_READ, PyMemoryView_FromMemory}; -use cpython::{PyBool, PyErr, PyObject, PyResult, PyString, Python, ToPyObject}; +use byteorder::{LittleEndian, ReadBytesExt}; use cpython::exc::{KeyError, ValueError}; +use cpython::{PyBool, PyErr, PyObject, PyResult, PyString, Python, ToPyObject}; +use pyffi::{PyBUF_READ, PyMemoryView_FromMemory}; -use crate::data::{PY_MODULES_DATA, PYC_MODULES_DATA}; +use crate::data::{PYC_MODULES_DATA, PY_MODULES_DATA}; /// Parse modules blob data into a map of module name to module data. -fn parse_modules_blob(data: &'static[u8]) -> Result, &'static str> { +fn parse_modules_blob(data: &'static [u8]) -> Result, &'static str> { if data.len() < 4 { return Err("modules data too small"); } @@ -44,9 +43,7 @@ fn parse_modules_blob(data: &'static[u8]) -> Result, &'stat for (name_length, value_length) in index { let offset = reader.position() as usize; - let name = unsafe { - std::str::from_utf8_unchecked(&data[offset..offset + name_length]) - }; + let name = unsafe { std::str::from_utf8_unchecked(&data[offset..offset + name_length]) }; let value_offset = values_start_offset + values_current_offset; let value = &data[value_offset..value_offset + value_length]; @@ -135,7 +132,7 @@ fn populate_packages(packages: &mut HashSet<&'static str>, name: &'static str) { Some(idx) => { packages.insert(&search[0..idx]); search = &search[0..idx]; - }, + } None => break, }; } @@ -145,12 +142,12 @@ fn populate_packages(packages: &mut HashSet<&'static str>, name: &'static str) { fn make_modules(py: Python) -> PyResult { let py_modules = match parse_modules_blob(PY_MODULES_DATA) { Ok(value) => value, - Err(msg) => return Err(PyErr::new::(py, msg)) + Err(msg) => return Err(PyErr::new::(py, msg)), }; let pyc_modules = match parse_modules_blob(PYC_MODULES_DATA) { Ok(value) => value, - Err(msg) => return Err(PyErr::new::(py, msg)) + Err(msg) => return Err(PyErr::new::(py, msg)), }; // TODO consider baking set of packages into embedded data. diff --git a/pyembed/src/pystr.rs b/pyembed/src/pystr.rs index 7815cdc6a..4a7df555b 100644 --- a/pyembed/src/pystr.rs +++ b/pyembed/src/pystr.rs @@ -4,7 +4,7 @@ use libc::{c_void, size_t, wchar_t}; use pyffi; -use std::os::raw::{c_char}; +use std::os::raw::c_char; use std::ptr::null_mut; #[derive(Debug)] @@ -14,27 +14,21 @@ pub struct OwnedPyStr { impl Drop for OwnedPyStr { fn drop(&mut self) { - unsafe { - pyffi::PyMem_RawFree(self.data as *mut c_void) - } + unsafe { pyffi::PyMem_RawFree(self.data as *mut c_void) } } } -impl <'a> From<&'a str> for OwnedPyStr { +impl<'a> From<&'a str> for OwnedPyStr { fn from(s: &str) -> Self { let size: *mut size_t = null_mut(); - let ptr = unsafe { - pyffi::Py_DecodeLocale(s.as_ptr() as *const c_char, size) - }; + let ptr = unsafe { pyffi::Py_DecodeLocale(s.as_ptr() as *const c_char, size) }; if ptr.is_null() { panic!("could not convert str to Python string"); } - OwnedPyStr { - data: ptr, - } + OwnedPyStr { data: ptr } } } diff --git a/pyrepackager/src/bytecode.rs b/pyrepackager/src/bytecode.rs index b34ee7747..744901609 100644 --- a/pyrepackager/src/bytecode.rs +++ b/pyrepackager/src/bytecode.rs @@ -2,15 +2,21 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -use cpython::{Python, PyBytes, PyErr, PyObject}; +use cpython::{PyBytes, PyErr, PyObject, Python}; use libc::c_char; -use pyffi::{Py_CompileStringExFlags, Py_file_input, Py_MARSHAL_VERSION, PyMarshal_WriteObjectToString}; +use pyffi::{ + PyMarshal_WriteObjectToString, Py_CompileStringExFlags, Py_MARSHAL_VERSION, Py_file_input, +}; use std::ffi::CString; /// Compile Python source to bytecode in-process. /// /// This can be used to produce data for a frozen module. -pub fn compile_bytecode(source: &Vec, filename: &str, optimize: i32) -> Result, String> { +pub fn compile_bytecode( + source: &Vec, + filename: &str, + optimize: i32, +) -> Result, String> { // Need to convert to CString to ensure trailing NULL is present. let source = CString::new(source.clone()).unwrap(); let filename = CString::new(filename).unwrap(); @@ -27,16 +33,23 @@ pub fn compile_bytecode(source: &Vec, filename: &str, optimize: i32) -> Resu // TODO we should validate against the parsed distribution instead of // hard-coding the version number. if pyffi::Py_MARSHAL_VERSION != 4 { - panic!("unrecognized marshal version {}; did build.rs link against Python 3.7?", pyffi::Py_MARSHAL_VERSION); + panic!( + "unrecognized marshal version {}; did build.rs link against Python 3.7?", + pyffi::Py_MARSHAL_VERSION + ); } - let mut flags = pyffi::PyCompilerFlags { - cf_flags: 0, - }; + let mut flags = pyffi::PyCompilerFlags { cf_flags: 0 }; let code = unsafe { let flags_ptr = &mut flags; - Py_CompileStringExFlags(source.as_ptr() as *const c_char, filename.as_ptr() as *const c_char, Py_file_input, flags_ptr, optimize) + Py_CompileStringExFlags( + source.as_ptr() as *const c_char, + filename.as_ptr() as *const c_char, + Py_file_input, + flags_ptr, + optimize, + ) }; if PyErr::occurred(py) { @@ -49,13 +62,9 @@ pub fn compile_bytecode(source: &Vec, filename: &str, optimize: i32) -> Resu panic!("code is null without Python error. Huh?"); } - let marshalled = unsafe { - PyMarshal_WriteObjectToString(code, Py_MARSHAL_VERSION) - }; + let marshalled = unsafe { PyMarshal_WriteObjectToString(code, Py_MARSHAL_VERSION) }; - let marshalled = unsafe { - PyObject::from_owned_ptr(py, marshalled) - }; + let marshalled = unsafe { PyObject::from_owned_ptr(py, marshalled) }; let data = marshalled.cast_as::(py).unwrap().data(py); diff --git a/pyrepackager/src/config.rs b/pyrepackager/src/config.rs index d09275db2..40b878753 100644 --- a/pyrepackager/src/config.rs +++ b/pyrepackager/src/config.rs @@ -104,7 +104,7 @@ pub fn parse_config(data: &Vec) -> Config { Some(value) => { let values: Vec<&str> = value.split(":").collect(); (Some(values[0].to_string()), Some(values[1].to_string())) - }, + } None => (None, None), }; @@ -114,9 +114,10 @@ pub fn parse_config(data: &Vec) -> Config { }; let package_module_paths = match config.python_packaging.module_paths { - Some(value) => { - value.iter().map(|p| PathBuf::from(p.as_str().unwrap())).collect() - }, + Some(value) => value + .iter() + .map(|p| PathBuf::from(p.as_str().unwrap())) + .collect(), None => Vec::new(), }; @@ -162,12 +163,16 @@ pub fn resolve_python_distribution_archive(config: &Config, cache_dir: &Path) -> Some(path) => { let p = Path::new(path); p.file_name().unwrap().to_str().unwrap().to_string() - }, + } None => match &config.python_distribution_url { Some(url) => { let url = Url::parse(url).expect("failed to parse URL"); - url.path_segments().expect("cannot be base path").last().expect("could not get last element").to_string() - }, + url.path_segments() + .expect("cannot be base path") + .last() + .expect("could not get last element") + .to_string() + } None => panic!("neither local path nor URL defined for distribution"), }, }; @@ -205,13 +210,15 @@ pub fn resolve_python_distribution_archive(config: &Config, cache_dir: &Path) -> std::fs::copy(path, &cache_path).unwrap(); cache_path - }, + } None => match &config.python_distribution_url { Some(url) => { let mut data: Vec = Vec::new(); let mut response = reqwest::get(url).expect("unable to perform HTTP request"); - response.read_to_end(&mut data).expect("unable to download URL"); + response + .read_to_end(&mut data) + .expect("unable to download URL"); let mut hasher = Sha256::new(); hasher.input(&data); diff --git a/pyrepackager/src/dist.rs b/pyrepackager/src/dist.rs index 8f388fcc2..aee8f6736 100644 --- a/pyrepackager/src/dist.rs +++ b/pyrepackager/src/dist.rs @@ -6,9 +6,9 @@ use itertools::Itertools; use std::collections::BTreeMap; use std::fs; use std::io::{BufRead, BufReader, Read}; -use std::path::{PathBuf, Path}; +use std::path::{Path, PathBuf}; -use crate::fsscan::{find_python_resources, PythonResourceType, walk_tree_files}; +use crate::fsscan::{find_python_resources, walk_tree_files, PythonResourceType}; #[derive(Debug, Deserialize)] struct LinkEntry { @@ -160,12 +160,9 @@ fn parse_setup_line(modules: &mut BTreeMap, line: &str) { let p = p.with_extension("o"); let basename = p.file_name().unwrap().to_str().unwrap(); object_filenames.push(basename.to_string()); - - } - else if word.starts_with("-l") { + } else if word.starts_with("-l") { libraries.push(word[2..].to_string()); - } - else if word == "-framework" { + } else if word == "-framework" { frameworks.push(String::from(words[idx + 1])); } } @@ -209,8 +206,7 @@ fn parse_setup_local(modules: &mut BTreeMap, data: &Vec) // Everything after the *disabled* line can be ignored. if line == "*disabled*" { break; - } - else if line == "*static*" { + } else if line == "*static*" { continue; } @@ -287,7 +283,9 @@ pub struct PythonDistributionInfo { /// Passing in a data structure with raw file data within is inefficient. But /// it makes things easier to implement and allows us to do things like consume /// tarballs without filesystem I/O. -pub fn analyze_python_distribution_data(temp_dir: tempdir::TempDir) -> Result { +pub fn analyze_python_distribution_data( + temp_dir: tempdir::TempDir, +) -> Result { let mut objs_core: BTreeMap = BTreeMap::new(); let mut links_core: Vec = Vec::new(); let mut extension_modules: BTreeMap = BTreeMap::new(); @@ -359,26 +357,34 @@ pub fn analyze_python_distribution_data(temp_dir: tempdir::TempDir) -> Result Some(python_path.join(p)), - None => None, + extension_modules.insert( + module.clone(), + ExtensionModule { + module: module.clone(), + init_fn: Some(entry.init_fn.clone()), + builtin_default: entry.in_core, + disableable: !entry.in_core, + object_paths, + static_library: match &entry.static_lib { + Some(p) => Some(python_path.join(p)), + None => None, + }, + links, }, - links, - }); + ); } let include_path = python_path.join(pi.python_include); for entry in walk_tree_files(&include_path) { let full_path = entry.path(); - let rel_path = full_path.strip_prefix(&include_path).expect("unable to strip prefix"); - includes.insert(String::from(rel_path.to_str().expect("path to string")), full_path.to_path_buf()); + let rel_path = full_path + .strip_prefix(&include_path) + .expect("unable to strip prefix"); + includes.insert( + String::from(rel_path.to_str().expect("path to string")), + full_path.to_path_buf(), + ); } let stdlib_path = python_path.join(pi.python_stdlib); @@ -388,7 +394,7 @@ pub fn analyze_python_distribution_data(temp_dir: tempdir::TempDir) -> Result { resources.insert(entry.name.clone(), entry.path); () - }, + } PythonResourceType::Source => { py_modules.insert(entry.name.clone(), entry.path); () @@ -411,19 +417,25 @@ pub fn analyze_python_distribution_data(temp_dir: tempdir::TempDir) -> Result(source: R) -> Result { +pub fn analyze_python_distribution_tar( + source: R, +) -> Result { let mut tf = tar::Archive::new(source); - let temp_dir = tempdir::TempDir::new("python-distribution").expect("could not create temp directory"); + let temp_dir = + tempdir::TempDir::new("python-distribution").expect("could not create temp directory"); let temp_dir_path = temp_dir.path(); - tf.unpack(&temp_dir_path).expect("unable to extract tar archive"); + tf.unpack(&temp_dir_path) + .expect("unable to extract tar archive"); analyze_python_distribution_data(temp_dir) } /// Extract Python distribution data from a zstandard compressed tar archive. -pub fn analyze_python_distribution_tar_zst(source: R) -> Result { +pub fn analyze_python_distribution_tar_zst( + source: R, +) -> Result { let dctx = zstd::stream::Decoder::new(source).unwrap(); analyze_python_distribution_tar(dctx) diff --git a/pyrepackager/src/fsscan.rs b/pyrepackager/src/fsscan.rs index eeba197b1..db7ab7be0 100644 --- a/pyrepackager/src/fsscan.rs +++ b/pyrepackager/src/fsscan.rs @@ -6,7 +6,7 @@ use itertools::Itertools; use std::collections::BTreeMap; use std::ffi::OsStr; use std::fs; -use std::path::{PathBuf, Path}; +use std::path::{Path, PathBuf}; pub fn walk_tree_files(path: &Path) -> Box> { let res = walkdir::WalkDir::new(path); @@ -91,21 +91,30 @@ impl Iterator for PythonResourceIterator { let res = self.walkdir_result.next(); if res.is_none() { - return None + return None; } let entry = res.unwrap(); let path = entry.path(); - let rel_path = path.strip_prefix(&self.root_path).expect("unable to strip path prefix"); + let rel_path = path + .strip_prefix(&self.root_path) + .expect("unable to strip path prefix"); let rel_str = rel_path.to_str().expect("could not convert path to str"); - let components = rel_path.iter().map(|p| p.to_str().expect("unable to get path as str")).collect::>(); + let components = rel_path + .iter() + .map(|p| p.to_str().expect("unable to get path as str")) + .collect::>(); let (module_name, flavor) = match rel_path.extension().and_then(OsStr::to_str) { Some("py") => { let package_parts = &components[0..components.len() - 1]; - let module_name = rel_path.file_stem().expect("unable to get file stemp").to_str().expect("unable to convert path to str"); + let module_name = rel_path + .file_stem() + .expect("unable to get file stemp") + .to_str() + .expect("unable to convert path to str"); let mut full_module_name: Vec<&str> = package_parts.to_vec(); @@ -116,7 +125,7 @@ impl Iterator for PythonResourceIterator { let full_module_name = itertools::join(full_module_name, "."); (full_module_name, PythonResourceType::Source) - }, + } Some("pyc") => { // .pyc files should be in a __pycache__ directory. if components.len() < 2 { @@ -130,9 +139,14 @@ impl Iterator for PythonResourceIterator { let package_parts = &components[0..components.len() - 2]; // Files have format /__pycache__/.cpython-37.opt-1.pyc - let module_name = rel_path.file_stem().expect("unable to get file stem").to_str().expect("unable to convert file stem to str"); + let module_name = rel_path + .file_stem() + .expect("unable to get file stem") + .to_str() + .expect("unable to convert file stem to str"); let module_name_parts = module_name.split(".").collect_vec(); - let module_name = itertools::join(&module_name_parts[0..module_name_parts.len() - 1], "."); + let module_name = + itertools::join(&module_name_parts[0..module_name_parts.len() - 1], "."); let mut full_module_name: Vec<&str> = package_parts.to_vec(); @@ -146,16 +160,14 @@ impl Iterator for PythonResourceIterator { if rel_str.ends_with(".opt-1.pyc") { flavor = PythonResourceType::BytecodeOpt1; - } - else if rel_str.ends_with(".opt-2.pyc") { + } else if rel_str.ends_with(".opt-2.pyc") { flavor = PythonResourceType::BytecodeOpt2; - } - else { + } else { flavor = PythonResourceType::Bytecode; } (full_module_name, flavor) - }, + } _ => { // If it isn't a .py or a .pyc file, it is a resource file. let name = itertools::join(components, "."); diff --git a/pyrepackager/src/lib.rs b/pyrepackager/src/lib.rs index 1d7c39366..e2ba5ed5a 100644 --- a/pyrepackager/src/lib.rs +++ b/pyrepackager/src/lib.rs @@ -61,7 +61,10 @@ const STDLIB_IGNORE_FILES: &[&str] = &[ mod tests { #[test] fn it_works() { - let source = std::fs::File::open("/home/gps/src/python-build-standalone/build/cpython-linux64.tar.zst").unwrap(); + let source = std::fs::File::open( + "/home/gps/src/python-build-standalone/build/cpython-linux64.tar.zst", + ) + .unwrap(); super::analyze_python_distribution_tar_zst(source).unwrap(); } } diff --git a/pyrepackager/src/repackage.rs b/pyrepackager/src/repackage.rs index 628152e18..21c637a4a 100644 --- a/pyrepackager/src/repackage.rs +++ b/pyrepackager/src/repackage.rs @@ -44,8 +44,7 @@ lazy_static! { if cfg!(target_os = "linux") { v.push("dl"); v.push("m"); - } - else if cfg!(target_os = "macos") { + } else if cfg!(target_os = "macos") { v.push("dl"); v.push("m"); } @@ -97,17 +96,23 @@ pub struct ImportlibData { /// Bytecode is then derived from it. pub fn derive_importlib(dist: &PythonDistributionInfo) -> ImportlibData { let mod_bootstrap_path = dist.py_modules.get("importlib._bootstrap").unwrap(); - let mod_bootstrap_external_path = dist.py_modules.get("importlib._bootstrap_external").unwrap(); + let mod_bootstrap_external_path = dist + .py_modules + .get("importlib._bootstrap_external") + .unwrap(); let bootstrap_source = fs::read(&mod_bootstrap_path).expect("unable to read bootstrap source"); let module_name = ""; - let bootstrap_bytecode = compile_bytecode(&bootstrap_source, module_name, 0).expect("error compiling bytecode"); + let bootstrap_bytecode = + compile_bytecode(&bootstrap_source, module_name, 0).expect("error compiling bytecode"); - let mut bootstrap_external_source = fs::read(&mod_bootstrap_external_path).expect("unable to read bootstrap_external source"); + let mut bootstrap_external_source = + fs::read(&mod_bootstrap_external_path).expect("unable to read bootstrap_external source"); bootstrap_external_source.extend("\n# END OF importlib/_bootstrap_external.py\n\n".bytes()); bootstrap_external_source.extend(PYTHON_IMPORTER); let module_name = ""; - let bootstrap_external_bytecode = compile_bytecode(&bootstrap_external_source, module_name, 0).expect("error compiling bytecode"); + let bootstrap_external_bytecode = compile_bytecode(&bootstrap_external_source, module_name, 0) + .expect("error compiling bytecode"); ImportlibData { bootstrap_source: bootstrap_source, @@ -195,7 +200,10 @@ fn make_config_c(dist: &PythonDistributionInfo, extensions: &BTreeSet<&String>) continue; } - lines.push(String::from(format!("{{\"{}\", {}}},", entry.module, init_fn))); + lines.push(String::from(format!( + "{{\"{}\", {}}},", + entry.module, init_fn + ))); } } @@ -227,7 +235,8 @@ pub fn link_libpython(dist: &PythonDistributionInfo) { // Relevant extension modules are the intersection of modules that are // built/available and what's requested from the current config. - let mut extension_modules: BTreeSet<&String> = BTreeSet::from_iter(dist.extension_modules.keys()); + let mut extension_modules: BTreeSet<&String> = + BTreeSet::from_iter(dist.extension_modules.keys()); for e in OS_IGNORE_EXTENSIONS.as_slice() { extension_modules.remove(&String::from(*e)); @@ -274,8 +283,7 @@ pub fn link_libpython(dist: &PythonDistributionInfo) { if entry.framework { println!("framework {} required by core", entry.name); needed_frameworks.insert(&entry.name); - } - else if entry.system { + } else if entry.system { println!("system library {} required by core", entry.name); needed_system_libraries.insert(&entry.name); } @@ -287,7 +295,10 @@ pub fn link_libpython(dist: &PythonDistributionInfo) { let entry = dist.extension_modules.get(name).unwrap(); if entry.builtin_default { - println!("{} is built-in and doesn't need special build actions", name); + println!( + "{} is built-in and doesn't need special build actions", + name + ); continue; } @@ -300,17 +311,13 @@ pub fn link_libpython(dist: &PythonDistributionInfo) { if entry.framework { needed_frameworks.insert(&entry.name); println!("framework {} required by {}", entry.name, name); - } - else if entry.system { + } else if entry.system { println!("system library {} required by {}", entry.name, name); needed_system_libraries.insert(&entry.name); - } - - else if let Some(_lib) = &entry.static_path { + } else if let Some(_lib) = &entry.static_path { needed_libraries.insert(&entry.name); println!("static library {} required by {}", entry.name, name); - } - else if let Some(_lib) = &entry.dynamic_path { + } else if let Some(_lib) = &entry.dynamic_path { needed_libraries.insert(&entry.name); println!("dynamic library {} required by {}", entry.name, name); } @@ -323,7 +330,10 @@ pub fn link_libpython(dist: &PythonDistributionInfo) { } // Otherwise find the library in the distribution. Extract it. And statically link against it. - let fs_path = dist.libraries.get(library).expect(&format!("unable to find library {}", library)); + let fs_path = dist + .libraries + .get(library) + .expect(&format!("unable to find library {}", library)); println!("{:?}", fs_path); let library_path = out_dir.join(format!("lib{}.a", library)); @@ -369,4 +379,4 @@ pub fn is_stdlib_test_package(name: &str) -> bool { } false -} \ No newline at end of file +}