@@ -386,14 +386,24 @@ impl VirtualMachine {
386386 self . initialized = true ;
387387 }
388388
389+ fn state_mut ( & mut self ) -> & mut PyGlobalState {
390+ PyRc :: get_mut ( & mut self . state )
391+ . expect ( "there should not be multiple threads while a user has a mut ref to a vm" )
392+ }
393+
389394 /// Can only be used in the initialization closure passed to [`Interpreter::new_with_init`]
390395 pub fn add_native_module < S > ( & mut self , name : S , module : stdlib:: StdlibInitFunc )
391396 where
392397 S : Into < Cow < ' static , str > > ,
393398 {
394- let state = PyRc :: get_mut ( & mut self . state )
395- . expect ( "can't add_native_module when there are multiple threads" ) ;
396- state. module_inits . insert ( name. into ( ) , module) ;
399+ self . state_mut ( ) . module_inits . insert ( name. into ( ) , module) ;
400+ }
401+
402+ pub fn add_native_modules < I > ( & mut self , iter : I )
403+ where
404+ I : IntoIterator < Item = ( Cow < ' static , str > , stdlib:: StdlibInitFunc ) > ,
405+ {
406+ self . state_mut ( ) . module_inits . extend ( iter) ;
397407 }
398408
399409 /// Can only be used in the initialization closure passed to [`Interpreter::new_with_init`]
@@ -402,9 +412,7 @@ impl VirtualMachine {
402412 I : IntoIterator < Item = ( String , bytecode:: FrozenModule ) > ,
403413 {
404414 let frozen = frozen:: map_frozen ( self , frozen) . collect :: < Vec < _ > > ( ) ;
405- let state = PyRc :: get_mut ( & mut self . state )
406- . expect ( "can't add_frozen when there are multiple threads" ) ;
407- state. frozen . extend ( frozen) ;
415+ self . state_mut ( ) . frozen . extend ( frozen) ;
408416 }
409417
410418 /// Start a new thread with access to the same interpreter.
0 commit comments