nac3artiq: raise specific exception on error

This commit is contained in:
Sebastien Bourdeauducq 2022-02-26 17:17:06 +08:00
parent 846d1726ef
commit cbc77dddb0

View File

@ -17,6 +17,7 @@ use nac3parser::{
}; };
use pyo3::prelude::*; use pyo3::prelude::*;
use pyo3::{exceptions, types::PyBytes, types::PyDict, types::PySet}; use pyo3::{exceptions, types::PyBytes, types::PyDict, types::PySet};
use pyo3::create_exception;
use parking_lot::{Mutex, RwLock}; use parking_lot::{Mutex, RwLock};
@ -88,6 +89,8 @@ struct Nac3 {
string_store: Arc<RwLock<HashMap<String, i32>>>, string_store: Arc<RwLock<HashMap<String, i32>>>,
} }
create_exception!(nac3artiq, CompileError, exceptions::PyException);
impl Nac3 { impl Nac3 {
fn register_module( fn register_module(
&mut self, &mut self,
@ -496,7 +499,7 @@ impl Nac3 {
let (name, def_id, ty) = composer let (name, def_id, ty) = composer
.register_top_level(stmt.clone(), Some(resolver.clone()), path.clone()) .register_top_level(stmt.clone(), Some(resolver.clone()), path.clone())
.map_err(|e| { .map_err(|e| {
exceptions::PyRuntimeError::new_err(format!( CompileError::new_err(format!(
"nac3 compilation failure\n----------\n{}", "nac3 compilation failure\n----------\n{}",
e e
)) ))
@ -582,7 +585,7 @@ impl Nac3 {
if let Err(e) = composer.start_analysis(true) { if let Err(e) = composer.start_analysis(true) {
// report error of __modinit__ separately // report error of __modinit__ separately
if !e.contains("__nac3_synthesized_modinit__") { if !e.contains("__nac3_synthesized_modinit__") {
return Err(exceptions::PyRuntimeError::new_err(format!( return Err(CompileError::new_err(format!(
"nac3 compilation failure: \n----------\n{}", "nac3 compilation failure: \n----------\n{}",
e e
))); )));
@ -595,7 +598,7 @@ impl Nac3 {
&mut composer.unifier, &mut composer.unifier,
&self.primitive, &self.primitive,
); );
return Err(exceptions::PyRuntimeError::new_err(msg.unwrap())); return Err(CompileError::new_err(msg.unwrap()));
} }
} }
let top_level = Arc::new(composer.make_top_level_context()); let top_level = Arc::new(composer.make_top_level_context());
@ -695,10 +698,10 @@ impl Nac3 {
.unwrap(); .unwrap();
main.link_in_module(other) main.link_in_module(other)
.map_err(|err| exceptions::PyRuntimeError::new_err(err.to_string()))?; .map_err(|err| CompileError::new_err(err.to_string()))?;
} }
main.link_in_module(load_irrt(&context)) main.link_in_module(load_irrt(&context))
.map_err(|err| exceptions::PyRuntimeError::new_err(err.to_string()))?; .map_err(|err| CompileError::new_err(err.to_string()))?;
let mut function_iter = main.get_first_function(); let mut function_iter = main.get_first_function();
while let Some(func) = function_iter { while let Some(func) = function_iter {
@ -762,10 +765,10 @@ impl Nac3 {
if let Ok(linker_status) = Command::new("ld.lld").args(linker_args).status() { if let Ok(linker_status) = Command::new("ld.lld").args(linker_args).status() {
if !linker_status.success() { if !linker_status.success() {
return Err(exceptions::PyRuntimeError::new_err("failed to start linker")); return Err(CompileError::new_err("failed to start linker"));
} }
} else { } else {
return Err(exceptions::PyRuntimeError::new_err( return Err(CompileError::new_err(
"linker returned non-zero status code", "linker returned non-zero status code",
)); ));
} }