@@ -15,7 +15,7 @@ mod _contextvars {
1515 AsObject , Py , PyObjectRef , PyPayload , PyRef , PyResult , VirtualMachine , atomic_func,
1616 builtins:: { PyGenericAlias , PyList , PyStrRef , PyType , PyTypeRef } ,
1717 class:: StaticType ,
18- common:: { hash:: PyHash , wtf8:: Wtf8Buf } ,
18+ common:: { hash:: PyHash , lock :: LazyLock , wtf8:: Wtf8Buf } ,
1919 function:: { ArgCallable , FuncArgs , OptionalArg } ,
2020 protocol:: { PyMappingMethods , PySequenceMethods } ,
2121 types:: { AsMapping , AsSequence , Constructor , Hashable , Iterable , Representable } ,
@@ -26,7 +26,6 @@ mod _contextvars {
2626 } ;
2727 use crossbeam_utils:: atomic:: AtomicCell ;
2828 use indexmap:: IndexMap ;
29- use rustpython_common:: lock:: LazyLock ;
3029
3130 // TODO: Real hamt implementation
3231 type Hamt = IndexMap < PyRef < ContextVar > , PyObjectRef , ahash:: RandomState > ;
@@ -89,11 +88,10 @@ mod _contextvars {
8988
9089 fn enter ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < ( ) > {
9190 if zelf. inner . entered . get ( ) {
92- let msg = format ! (
91+ return Err ( vm . new_runtime_error ( format ! (
9392 "cannot enter context: {} is already entered" ,
9493 zelf. as_object( ) . repr( vm) ?
95- ) ;
96- return Err ( vm. new_runtime_error ( msg) ) ;
94+ ) ) ) ;
9795 }
9896
9997 super :: CONTEXTS . with_borrow_mut ( |ctxs| {
@@ -107,19 +105,20 @@ mod _contextvars {
107105
108106 fn exit ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < ( ) > {
109107 if !zelf. inner . entered . get ( ) {
110- let msg = format ! (
108+ return Err ( vm . new_runtime_error ( format ! (
111109 "cannot exit context: {} is not entered" ,
112110 zelf. as_object( ) . repr( vm) ?
113- ) ;
114- return Err ( vm. new_runtime_error ( msg) ) ;
111+ ) ) ) ;
115112 }
116113
117114 super :: CONTEXTS . with_borrow_mut ( |ctxs| {
118- let err_msg =
119- "cannot exit context: thread state references a different context object" ;
120115 ctxs. pop_if ( |ctx| ctx. get_id ( ) == zelf. get_id ( ) )
121116 . map ( drop)
122- . ok_or_else ( || vm. new_runtime_error ( err_msg) )
117+ . ok_or_else ( || {
118+ vm. new_runtime_error (
119+ "cannot exit context: thread state references a different context object"
120+ )
121+ } )
123122 } ) ?;
124123 zelf. inner . entered . set ( false ) ;
125124
@@ -205,12 +204,11 @@ mod _contextvars {
205204 default : OptionalArg < PyObjectRef > ,
206205 ) -> PyResult < Option < PyObjectRef > > {
207206 let found = self . get_inner ( & key) ;
208- let result = if let Some ( found) = found {
209- Some ( found)
207+ Ok ( if found. is_some ( ) {
208+ found
210209 } else {
211210 default. into_option ( )
212- } ;
213- Ok ( result)
211+ } )
214212 }
215213
216214 // TODO: wrong return type
@@ -252,12 +250,9 @@ mod _contextvars {
252250 ) ) ,
253251 subscript : atomic_func ! ( |mapping, needle, vm| {
254252 let needle = needle. try_to_value( vm) ?;
255- let found = PyContext :: mapping_downcast( mapping) . get_inner( needle) ;
256- if let Some ( found) = found {
257- Ok ( found)
258- } else {
259- Err ( vm. new_key_error( needle. to_owned( ) . into( ) ) )
260- }
253+ PyContext :: mapping_downcast( mapping)
254+ . get_inner( needle)
255+ . ok_or_else( || vm. new_key_error( needle. to_owned( ) . into( ) ) )
261256 } ) ,
262257 ass_subscript : None ,
263258 } ;
@@ -333,8 +328,7 @@ mod _contextvars {
333328 if vars. swap_remove ( zelf) . is_none ( ) {
334329 // TODO:
335330 // PyErr_SetObject(PyExc_LookupError, (PyObject *)var);
336- let msg = zelf. as_object ( ) . repr ( vm) ?. as_wtf8 ( ) . to_owned ( ) ;
337- return Err ( vm. new_lookup_error ( msg) ) ;
331+ return Err ( vm. new_lookup_error ( zelf. as_object ( ) . repr ( vm) ?. as_wtf8 ( ) . to_owned ( ) ) ) ;
338332 }
339333
340334 Ok ( ( ) )
@@ -408,8 +402,7 @@ mod _contextvars {
408402 } else if let Some ( default) = zelf. default . as_ref ( ) {
409403 default. clone ( )
410404 } else {
411- let msg = zelf. as_object ( ) . repr ( vm) ?;
412- return Err ( vm. new_lookup_error ( msg. as_wtf8 ( ) . to_owned ( ) ) ) ;
405+ return Err ( vm. new_lookup_error ( zelf. as_object ( ) . repr ( vm) ?. as_wtf8 ( ) . to_owned ( ) ) ) ;
413406 } ;
414407 Ok ( Some ( value) )
415408 }
@@ -439,25 +432,25 @@ mod _contextvars {
439432 #[ pymethod]
440433 fn reset ( zelf : & Py < Self > , token : PyRef < ContextToken > , vm : & VirtualMachine ) -> PyResult < ( ) > {
441434 if token. used . get ( ) {
442- let msg = format ! ( "{} has already been used once" , token. as_object( ) . repr( vm) ?) ;
443- return Err ( vm. new_runtime_error ( msg) ) ;
435+ return Err ( vm. new_runtime_error ( format ! (
436+ "{} has already been used once" ,
437+ token. as_object( ) . repr( vm) ?
438+ ) ) ) ;
444439 }
445440
446441 if !zelf. is ( & token. var ) {
447- let msg = format ! (
442+ return Err ( vm . new_value_error ( format ! (
448443 "{} was created by a different ContextVar" ,
449444 token. var. as_object( ) . repr( vm) ?
450- ) ;
451- return Err ( vm. new_value_error ( msg) ) ;
445+ ) ) ) ;
452446 }
453447
454448 let ctx = PyContext :: current ( vm) ;
455449 if !ctx. is ( & token. ctx ) {
456- let msg = format ! (
450+ return Err ( vm . new_value_error ( format ! (
457451 "{} was created in a different Context" ,
458452 token. var. as_object( ) . repr( vm) ?
459- ) ;
460- return Err ( vm. new_value_error ( msg) ) ;
453+ ) ) ) ;
461454 }
462455
463456 token. used . set ( true ) ;
@@ -602,6 +595,7 @@ mod _contextvars {
602595 fn slot_new ( _cls : PyTypeRef , _args : FuncArgs , vm : & VirtualMachine ) -> PyResult {
603596 Err ( vm. new_runtime_error ( "Tokens can only be created by ContextVars" ) )
604597 }
598+
605599 fn py_new ( _cls : & Py < PyType > , _args : Self :: Args , _vm : & VirtualMachine ) -> PyResult < Self > {
606600 unimplemented ! ( "use slot_new" )
607601 }
0 commit comments