@@ -10,10 +10,10 @@ mod sys {
10
10
ascii,
11
11
hash:: { PyHash , PyUHash } ,
12
12
} ,
13
+ convert:: ToPyObject ,
13
14
frame:: FrameRef ,
14
15
function:: { FuncArgs , OptionalArg , PosArgs } ,
15
- stdlib:: builtins,
16
- stdlib:: warnings:: warn,
16
+ stdlib:: { builtins, warnings:: warn} ,
17
17
types:: PyStructSequence ,
18
18
version,
19
19
vm:: { Settings , VirtualMachine } ,
@@ -706,6 +706,68 @@ mod sys {
706
706
crate :: vm:: thread:: COROUTINE_ORIGIN_TRACKING_DEPTH . with ( |cell| cell. get ( ) ) as _
707
707
}
708
708
709
+ #[ derive( FromArgs ) ]
710
+ struct SetAsyncgenHooksArgs {
711
+ #[ pyarg( any, optional) ]
712
+ firstiter : OptionalArg < Option < PyObjectRef > > ,
713
+ #[ pyarg( any, optional) ]
714
+ finalizer : OptionalArg < Option < PyObjectRef > > ,
715
+ }
716
+
717
+ #[ pyfunction]
718
+ fn set_asyncgen_hooks ( args : SetAsyncgenHooksArgs , vm : & VirtualMachine ) -> PyResult < ( ) > {
719
+ if let Some ( Some ( finalizer) ) = args. finalizer . as_option ( ) {
720
+ if !finalizer. is_callable ( ) {
721
+ return Err ( vm. new_type_error ( format ! (
722
+ "callable finalizer expected, got {:.50}" ,
723
+ finalizer. class( ) . name( )
724
+ ) ) ) ;
725
+ }
726
+ }
727
+
728
+ if let Some ( Some ( firstiter) ) = args. firstiter . as_option ( ) {
729
+ if !firstiter. is_callable ( ) {
730
+ return Err ( vm. new_type_error ( format ! (
731
+ "callable firstiter expected, got {:.50}" ,
732
+ firstiter. class( ) . name( )
733
+ ) ) ) ;
734
+ }
735
+ }
736
+
737
+ if let Some ( finalizer) = args. finalizer . into_option ( ) {
738
+ crate :: vm:: thread:: ASYNC_GEN_FINALIZER . with ( |cell| {
739
+ cell. replace ( finalizer) ;
740
+ } ) ;
741
+ }
742
+ if let Some ( firstiter) = args. firstiter . into_option ( ) {
743
+ crate :: vm:: thread:: ASYNC_GEN_FIRSTITER . with ( |cell| {
744
+ cell. replace ( firstiter) ;
745
+ } ) ;
746
+ }
747
+
748
+ Ok ( ( ) )
749
+ }
750
+
751
+ #[ pyclass( no_attr, name = "asyncgen_hooks" ) ]
752
+ #[ derive( PyStructSequence ) ]
753
+ pub ( super ) struct PyAsyncgenHooks {
754
+ firstiter : PyObjectRef ,
755
+ finalizer : PyObjectRef ,
756
+ }
757
+
758
+ #[ pyclass( with( PyStructSequence ) ) ]
759
+ impl PyAsyncgenHooks { }
760
+
761
+ #[ pyfunction]
762
+ fn get_asyncgen_hooks ( vm : & VirtualMachine ) -> PyAsyncgenHooks {
763
+ PyAsyncgenHooks {
764
+ firstiter : crate :: vm:: thread:: ASYNC_GEN_FIRSTITER
765
+ . with ( |cell| cell. borrow ( ) . clone ( ) . to_pyobject ( vm) ) ,
766
+ finalizer : crate :: vm:: thread:: ASYNC_GEN_FINALIZER
767
+ . with ( |cell| cell. borrow ( ) . clone ( ) . to_pyobject ( vm) ) ,
768
+ }
769
+ }
770
+
709
771
/// sys.flags
710
772
///
711
773
/// Flags provided through command line arguments or environment vars.
0 commit comments