Skip to content

Commit

Permalink
global: rustfmt our Rust code
Browse files Browse the repository at this point in the history
  • Loading branch information
indygreg committed Jan 6, 2019
1 parent d376ea2 commit 7d5fdc7
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 140 deletions.
8 changes: 6 additions & 2 deletions pyembed/src/pyalloc.rs
Expand Up @@ -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);

Expand All @@ -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);
Expand Down
64 changes: 34 additions & 30 deletions pyembed/src/pyinterp.rs
Expand Up @@ -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::*;
Expand Down Expand Up @@ -113,16 +115,12 @@ fn stdin_to_file() -> *mut libc::FILE {
// (_open_osfhandle() + _fdopen() might work), using the same function
// that <stdio.h> 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.
Expand Down Expand Up @@ -161,15 +159,18 @@ 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;

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();
Expand All @@ -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),
);
}
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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();
Expand All @@ -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<PyObject> {
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);
Expand Down Expand Up @@ -363,9 +373,7 @@ impl MainPythonInterpreter {
///
/// This emulates what CPython's main.c does.
pub fn run_repl(&mut self) -> PyResult<PyObject> {
let py = unsafe {
Python::assume_gil_acquired()
};
let py = unsafe { Python::assume_gil_acquired() };

self.init(py);

Expand All @@ -383,15 +391,13 @@ impl MainPythonInterpreter {
match sys.get(py, "__interactivehook__") {
Ok(hook) => {
hook.call(py, NoArgs, None)?;
},
}
Err(_) => (),
};

let stdin_filename = "<stdin>";
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 {
Expand All @@ -405,8 +411,6 @@ impl MainPythonInterpreter {

impl Drop for MainPythonInterpreter {
fn drop(&mut self) {
let _ = unsafe {
pyffi::Py_FinalizeEx()
};
let _ = unsafe { pyffi::Py_FinalizeEx() };
}
}
21 changes: 9 additions & 12 deletions pyembed/src/pymodules_module.rs
Expand Up @@ -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<HashMap<&str, &[u8]>, &'static str> {
fn parse_modules_blob(data: &'static [u8]) -> Result<HashMap<&str, &[u8]>, &'static str> {
if data.len() < 4 {
return Err("modules data too small");
}
Expand Down Expand Up @@ -44,9 +43,7 @@ fn parse_modules_blob(data: &'static[u8]) -> Result<HashMap<&str, &[u8]>, &'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];
Expand Down Expand Up @@ -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,
};
}
Expand All @@ -145,12 +142,12 @@ fn populate_packages(packages: &mut HashSet<&'static str>, name: &'static str) {
fn make_modules(py: Python) -> PyResult<ModulesType> {
let py_modules = match parse_modules_blob(PY_MODULES_DATA) {
Ok(value) => value,
Err(msg) => return Err(PyErr::new::<ValueError, _>(py, msg))
Err(msg) => return Err(PyErr::new::<ValueError, _>(py, msg)),
};

let pyc_modules = match parse_modules_blob(PYC_MODULES_DATA) {
Ok(value) => value,
Err(msg) => return Err(PyErr::new::<ValueError, _>(py, msg))
Err(msg) => return Err(PyErr::new::<ValueError, _>(py, msg)),
};

// TODO consider baking set of packages into embedded data.
Expand Down
16 changes: 5 additions & 11 deletions pyembed/src/pystr.rs
Expand Up @@ -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)]
Expand All @@ -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 }
}
}

Expand Down
37 changes: 23 additions & 14 deletions pyrepackager/src/bytecode.rs
Expand Up @@ -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<u8>, filename: &str, optimize: i32) -> Result<Vec<u8>, String> {
pub fn compile_bytecode(
source: &Vec<u8>,
filename: &str,
optimize: i32,
) -> Result<Vec<u8>, 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();
Expand All @@ -27,16 +33,23 @@ pub fn compile_bytecode(source: &Vec<u8>, 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) {
Expand All @@ -49,13 +62,9 @@ pub fn compile_bytecode(source: &Vec<u8>, 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::<PyBytes>(py).unwrap().data(py);

Expand Down

0 comments on commit 7d5fdc7

Please sign in to comment.