Skip to content

Commit

Permalink
pyc: replace data in place
Browse files Browse the repository at this point in the history
This is in preparation for future changes. The code for 'struct Pyc'
is put together in one place, separate from the more general code for
PycParser.

parser.clear_unused_flag_refs() modifies parser in place. A scratch
copy of the data is still used, so that modifications can be discarder
on failure. Returning a new copy of data would be awkward if we want
to do further processing on that data.
  • Loading branch information
keszybz committed Jul 17, 2024
1 parent baaa0da commit adf1d61
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/handlers/pyc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,6 @@ pub fn pyc_python_version(buf: &[u8; 4]) -> Result<((u32, u32), usize)> {
}
}

pub struct Pyc {
config: Rc<options::Config>,
}

impl Pyc {
pub fn boxed(config: &Rc<options::Config>) -> Box<dyn super::Processor> {
Box::new(Self { config: config.clone() })
}
}

#[derive(Debug)]
#[allow(dead_code)] // Right now, we only use dbg! to print the object.
enum Object {
Expand Down Expand Up @@ -697,7 +687,7 @@ impl PycParser {
Ok(Object::Dict(dict))
}

fn clear_unused_flag_refs(&mut self) -> Result<(bool, Vec<u8>)> {
fn clear_unused_flag_refs(&mut self) -> Result<bool> {
// Sequence of flag_refs and irefs ordered by number of byte in a file
let final_list =
iter::zip(self.flag_refs.iter(), iter::repeat(true))
Expand Down Expand Up @@ -743,7 +733,22 @@ impl PycParser {

debug!("{}: removed {} unused FLAG_REFs", self.input_path.display(), removed_count);
assert_eq!(data == self.data, removed_count == 0);
Ok((removed_count > 0, data))
if removed_count > 0 {
self.data = data;
}

Ok(removed_count > 0)
}
}


pub struct Pyc {
config: Rc<options::Config>,
}

impl Pyc {
pub fn boxed(config: &Rc<options::Config>) -> Box<dyn super::Processor> {
Box::new(Self { config: config.clone() })
}
}

Expand All @@ -766,10 +771,10 @@ impl super::Processor for Pyc {

parser.read_object()?;

let (have_mod, data) = parser.clear_unused_flag_refs()?;
let have_mod = parser.clear_unused_flag_refs()?;
if have_mod {
io.open_output()?;
io.output.as_mut().unwrap().write_all(&data)?;
io.output.as_mut().unwrap().write_all(&parser.data)?;
}

io.finalize(have_mod)
Expand Down

0 comments on commit adf1d61

Please sign in to comment.