@@ -52,28 +52,34 @@ impl PyPostProcessor {
52
52
53
53
pub ( crate ) fn get_as_subtype ( & self , py : Python < ' _ > ) -> PyResult < PyObject > {
54
54
let base = self . clone ( ) ;
55
- Ok ( match self . processor . read ( ) . unwrap ( ) . clone ( ) {
56
- PostProcessorWrapper :: ByteLevel ( _) => Py :: new ( py, ( PyByteLevel { } , base) ) ?
57
- . into_pyobject ( py) ?
58
- . into_any ( )
59
- . into ( ) ,
60
- PostProcessorWrapper :: Bert ( _) => Py :: new ( py, ( PyBertProcessing { } , base) ) ?
61
- . into_pyobject ( py) ?
62
- . into_any ( )
63
- . into ( ) ,
64
- PostProcessorWrapper :: Roberta ( _) => Py :: new ( py, ( PyRobertaProcessing { } , base) ) ?
65
- . into_pyobject ( py) ?
66
- . into_any ( )
67
- . into ( ) ,
68
- PostProcessorWrapper :: Template ( _) => Py :: new ( py, ( PyTemplateProcessing { } , base) ) ?
69
- . into_pyobject ( py) ?
70
- . into_any ( )
71
- . into ( ) ,
72
- PostProcessorWrapper :: Sequence ( _) => Py :: new ( py, ( PySequence { } , base) ) ?
73
- . into_pyobject ( py) ?
74
- . into_any ( )
75
- . into ( ) ,
76
- } )
55
+ Ok (
56
+ match & * self
57
+ . processor
58
+ . read ( )
59
+ . map_err ( |_| PyException :: new_err ( "pre tokenizer rwlock is poisoned" ) ) ?
60
+ {
61
+ PostProcessorWrapper :: ByteLevel ( _) => Py :: new ( py, ( PyByteLevel { } , base) ) ?
62
+ . into_pyobject ( py) ?
63
+ . into_any ( )
64
+ . into ( ) ,
65
+ PostProcessorWrapper :: Bert ( _) => Py :: new ( py, ( PyBertProcessing { } , base) ) ?
66
+ . into_pyobject ( py) ?
67
+ . into_any ( )
68
+ . into ( ) ,
69
+ PostProcessorWrapper :: Roberta ( _) => Py :: new ( py, ( PyRobertaProcessing { } , base) ) ?
70
+ . into_pyobject ( py) ?
71
+ . into_any ( )
72
+ . into ( ) ,
73
+ PostProcessorWrapper :: Template ( _) => Py :: new ( py, ( PyTemplateProcessing { } , base) ) ?
74
+ . into_pyobject ( py) ?
75
+ . into_any ( )
76
+ . into ( ) ,
77
+ PostProcessorWrapper :: Sequence ( _) => Py :: new ( py, ( PySequence { } , base) ) ?
78
+ . into_pyobject ( py) ?
79
+ . into_any ( )
80
+ . into ( ) ,
81
+ } ,
82
+ )
77
83
}
78
84
}
79
85
@@ -538,19 +544,14 @@ impl PySequence {
538
544
}
539
545
540
546
fn __getitem__ ( self_ : PyRef < ' _ , Self > , py : Python < ' _ > , index : usize ) -> PyResult < Py < PyAny > > {
541
- let super_ = self_. as_ref ( ) ;
542
- let mut wrapper = super_. processor . write ( ) . unwrap ( ) ;
543
- // if let PostProcessorWrapper::Sequence(ref mut post) = *wrapper {
544
- // match post.get(index) {
545
- // Some(item) => PyPostProcessor::new(Arc::clone(item)).get_as_subtype(py),
546
- // _ => Err(PyErr::new::<pyo3::exceptions::PyIndexError, _>(
547
- // "Index not found",
548
- // )),
549
- // }
550
- // }
547
+ let wrapper = self_
548
+ . as_ref ( )
549
+ . processor
550
+ . read ( )
551
+ . map_err ( |_| PyException :: new_err ( "post processor rwlock is poisoned" ) ) ?;
551
552
552
553
match * wrapper {
553
- PostProcessorWrapper :: Sequence ( ref mut inner) => match inner. get_mut ( index) {
554
+ PostProcessorWrapper :: Sequence ( ref inner) => match inner. get ( index) {
554
555
Some ( item) => {
555
556
PyPostProcessor :: new ( Arc :: new ( RwLock :: new ( item. to_owned ( ) ) ) ) . get_as_subtype ( py)
556
557
}
@@ -564,32 +565,31 @@ impl PySequence {
564
565
}
565
566
}
566
567
567
- fn __setitem__ (
568
- self_ : PyRefMut < ' _ , Self > ,
569
- index : usize ,
570
- value : PyRef < ' _ , PyPostProcessor > ,
571
- ) -> PyResult < ( ) > {
572
- let super_ = self_. as_ref ( ) ;
573
- let mut wrapper = super_. processor . write ( ) . unwrap ( ) ;
574
- let value = value. processor . read ( ) . unwrap ( ) . clone ( ) ;
568
+ fn __setitem__ ( self_ : PyRef < ' _ , Self > , index : usize , value : Bound < ' _ , PyAny > ) -> PyResult < ( ) > {
569
+ let processor: PyPostProcessor = value. extract ( ) ?;
570
+ let mut wrapper = self_
571
+ . as_ref ( )
572
+ . processor
573
+ . write ( )
574
+ . map_err ( |_| PyException :: new_err ( "post processor rwlock is poisoned" ) ) ?;
575
575
match * wrapper {
576
- PostProcessorWrapper :: Sequence ( ref mut inner) => {
577
- // Convert the Py<PyAny> into the appropriate Rust type
578
- // Ensure we can set an item at the given index
579
- if index < inner. get_processors ( ) . len ( ) {
580
- inner. set_mut ( index, value) ; // Assuming you want to wrap the new item in Arc<RwLock>
581
-
582
- Ok ( ( ) )
583
- } else {
584
- Err ( PyErr :: new :: < pyo3:: exceptions:: PyIndexError , _ > (
585
- "Index out of bounds" ,
576
+ PostProcessorWrapper :: Sequence ( ref mut inner) => match inner. get_mut ( index) {
577
+ Some ( item) => {
578
+ * item = processor. processor . read ( ) . unwrap ( ) . clone ( ) ;
579
+ }
580
+ _ => {
581
+ return Err ( PyErr :: new :: < pyo3:: exceptions:: PyIndexError , _ > (
582
+ "Index not found" ,
586
583
) )
587
584
}
585
+ } ,
586
+ _ => {
587
+ return Err ( PyException :: new_err (
588
+ "This processor is not a Sequence, it does not support __setitem__" ,
589
+ ) )
588
590
}
589
- _ => Err ( PyErr :: new :: < pyo3:: exceptions:: PyIndexError , _ > (
590
- "This processor is not a Sequence, it does not support __setitem__" ,
591
- ) ) ,
592
- }
591
+ } ;
592
+ Ok ( ( ) )
593
593
}
594
594
}
595
595
0 commit comments