@@ -65,8 +65,9 @@ impl PythonBindGenerator {
6565 let mut file_contents = vec ! [ ] ;
6666
6767 file_contents. push ( Cow :: Borrowed ( match bind_type {
68- PythonBindType :: Struct | PythonBindType :: Union => "use crate::{generated::rlbot::flat, FromGil, IntoGil};" ,
69- PythonBindType :: Enum => "use crate::generated::rlbot::flat;" ,
68+ PythonBindType :: Struct => "use crate::{flat_err_to_py, generated::rlbot::flat, FromGil, IntoGil};" ,
69+ PythonBindType :: Union => "use crate::{generated::rlbot::flat, FromGil, IntoGil};" ,
70+ PythonBindType :: Enum => "use crate::{flat_err_to_py, generated::rlbot::flat};" ,
7071 } ) ) ;
7172
7273 if bind_type != PythonBindType :: Union {
@@ -82,8 +83,10 @@ impl PythonBindGenerator {
8283 }
8384
8485 file_contents. push ( Cow :: Borrowed ( match bind_type {
85- PythonBindType :: Struct => "use pyo3::{pyclass, pymethods, types::PyBytes, Bound, Py, Python};" ,
86- PythonBindType :: Enum => "use pyo3::{pyclass, pymethods, types::PyBytes, Bound, Python};" ,
86+ PythonBindType :: Struct => "use pyo3::{pyclass, pymethods, types::PyBytes, Bound, Py, PyResult, Python};" ,
87+ PythonBindType :: Enum => {
88+ "use pyo3::{exceptions::PyValueError, pyclass, pymethods, types::PyBytes, Bound, PyResult, Python};"
89+ }
8790 PythonBindType :: Union => "use pyo3::{pyclass, pymethods, Py, PyObject, Python, ToPyObject};" ,
8891 } ) ) ;
8992
@@ -809,19 +812,20 @@ impl PythonBindGenerator {
809812 assert ! ( u8 :: try_from( self . types. len( ) ) . is_ok( ) ) ;
810813
811814 self . write_str ( " #[pyo3(signature = (value=Default::default()))]" ) ;
812- self . write_str ( " pub fn new(value: u8) -> Self {" ) ;
815+ self . write_str ( " pub fn new(value: u8) -> PyResult< Self> {" ) ;
813816 self . write_str ( " match value {" ) ;
814817
815818 for variable_info in & self . types {
816819 let variable_name = & variable_info[ 0 ] ;
817820 let variable_value = & variable_info[ 1 ] ;
818821
819- self . file_contents
820- . push ( Cow :: Owned ( format ! ( " {variable_value} => Self::{variable_name}," ) ) ) ;
822+ self . file_contents . push ( Cow :: Owned ( format ! (
823+ " {variable_value} => Ok(Self::{variable_name}),"
824+ ) ) ) ;
821825 }
822826
823827 if self . types . len ( ) != usize:: from ( u8:: MAX ) {
824- self . write_str ( " v => panic !(\" Unknown value: {v}\" )," ) ;
828+ self . write_str ( " v => Err(PyValueError::new_err(format !(\" Unknown value of {v}\" )) )," ) ;
825829 }
826830
827831 self . write_str ( " }" ) ;
@@ -1118,14 +1122,17 @@ impl PythonBindGenerator {
11181122 self . write_str ( " #[staticmethod]" ) ;
11191123
11201124 if self . bind_type == PythonBindType :: Enum {
1121- self . write_str ( " fn unpack(data: &[u8]) -> Self {" ) ;
1122- self . write_string ( format ! ( " root::<flat::{}>(data).unwrap().into()" , self . struct_name) ) ;
1125+ self . write_str ( " fn unpack(data: &[u8]) -> PyResult<Self> {" ) ;
1126+ self . write_string ( format ! ( " match root::<flat::{}>(data) {{" , self . struct_name) ) ;
1127+ self . write_str ( " Ok(flat_t) => Ok(flat_t.into())," ) ;
1128+ self . write_str ( " Err(e) => Err(flat_err_to_py(e))," ) ;
1129+ self . write_str ( " }" ) ;
11231130 } else {
1124- self . write_str ( " fn unpack(py: Python, data: &[u8]) -> Py<Self> {" ) ;
1125- self . write_string ( format ! (
1126- " root::<flat::{}>(data).unwrap() .unpack().into_gil(py)" ,
1127- self . struct_name
1128- ) ) ;
1131+ self . write_str ( " fn unpack(py: Python, data: &[u8]) -> PyResult< Py<Self> > {" ) ;
1132+ self . write_string ( format ! ( " match root::<flat::{}>(data) {{" , self . struct_name ) ) ;
1133+ self . write_str ( " Ok(flat_t) => Ok(flat_t .unpack().into_gil(py))," ) ;
1134+ self . write_str ( " Err(e) => Err(flat_err_to_py(e))," ) ;
1135+ self . write_str ( " }" ) ;
11291136 }
11301137
11311138 self . write_str ( " }" ) ;
@@ -1226,6 +1233,8 @@ fn pyi_generator(type_data: &[(String, String, Vec<Vec<String>>)]) -> io::Result
12261233 Cow :: Borrowed ( "__doc__: str" ) ,
12271234 Cow :: Borrowed ( "__version__: str" ) ,
12281235 Cow :: Borrowed ( "" ) ,
1236+ Cow :: Borrowed ( "class InvalidFlatbuffer(ValueError): ..." ) ,
1237+ Cow :: Borrowed ( "" ) ,
12291238 ] ;
12301239
12311240 let primitive_map = [
@@ -1347,7 +1356,10 @@ fn pyi_generator(type_data: &[(String, String, Vec<Vec<String>>)]) -> io::Result
13471356 file_contents. push ( Cow :: Borrowed ( "" ) ) ;
13481357
13491358 if is_enum {
1350- file_contents. push ( Cow :: Borrowed ( " def __init__(self, value: int = 0): ..." ) ) ;
1359+ file_contents. push ( Cow :: Borrowed ( " def __init__(self, value: int = 0):" ) ) ;
1360+ file_contents. push ( Cow :: Borrowed ( " \" \" \" " ) ) ;
1361+ file_contents. push ( Cow :: Borrowed ( " :raises ValueError: If the `value` is not a valid enum value" ) ) ;
1362+ file_contents. push ( Cow :: Borrowed ( " \" \" \" " ) ) ;
13511363 } else {
13521364 file_contents. push ( Cow :: Borrowed ( " def __init__(" ) ) ;
13531365 file_contents. push ( Cow :: Borrowed ( " self," ) ) ;
@@ -1404,7 +1416,12 @@ fn pyi_generator(type_data: &[(String, String, Vec<Vec<String>>)]) -> io::Result
14041416 if !is_union {
14051417 file_contents. push ( Cow :: Borrowed ( " def pack(self) -> bytes: ..." ) ) ;
14061418 file_contents. push ( Cow :: Borrowed ( " @staticmethod" ) ) ;
1407- file_contents. push ( Cow :: Owned ( format ! ( " def unpack(data: bytes) -> {type_name}: ..." ) ) ) ;
1419+ file_contents. push ( Cow :: Owned ( format ! ( " def unpack(data: bytes) -> {type_name}:" ) ) ) ;
1420+ file_contents. push ( Cow :: Borrowed ( " \" \" \" " ) ) ;
1421+ file_contents. push ( Cow :: Borrowed (
1422+ " :raises InvalidFlatbuffer: If the `data` is invalid for this type" ,
1423+ ) ) ;
1424+ file_contents. push ( Cow :: Borrowed ( " \" \" \" " ) ) ;
14081425 }
14091426
14101427 file_contents. push ( Cow :: Borrowed ( "" ) ) ;
0 commit comments