22// The .NET Foundation licenses this file to you under the Apache 2.0 License.
33// See the LICENSE file in the project root for more information.
44
5+ #nullable enable
6+
57using System ;
68using System . Collections . Generic ;
7- using System . Diagnostics ;
89using System . Threading ;
910
1011using IronPython . Runtime ;
1415
1516using Microsoft . Scripting ;
1617using Microsoft . Scripting . Runtime ;
17- using Microsoft . Scripting . Utils ;
1818
1919using SpecialName = System . Runtime . CompilerServices . SpecialNameAttribute ;
2020
@@ -25,7 +25,7 @@ public static class PythonThread {
2525
2626 private static readonly object _stackSizeKey = new object ( ) ;
2727 private static object _threadCountKey = new object ( ) ;
28- [ ThreadStatic ] private static List < @lock > _sentinelLocks ;
28+ [ ThreadStatic ] private static List < @lock > ? _sentinelLocks ;
2929
3030 [ SpecialName ]
3131 public static void PerformModuleReload ( PythonContext /*!*/ context , PythonDictionary /*!*/ dict ) {
@@ -40,21 +40,20 @@ public static void PerformModuleReload(PythonContext/*!*/ context, PythonDiction
4040 [ System . Diagnostics . CodeAnalysis . SuppressMessage ( "Microsoft.Security" , "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes" ) ]
4141 public static readonly PythonType LockType = DynamicHelpers . GetPythonTypeFromType ( typeof ( @lock ) ) ;
4242
43- [ Documentation ( "start_new_thread(function, [ args, [kwDict] ]) -> thread id\n Creates a new thread running the given function" ) ]
44- public static object start_new_thread ( CodeContext /*!*/ context , object function , object args , object kwDict ) {
45- PythonTuple tupArgs = args as PythonTuple ;
46- if ( tupArgs == null ) throw PythonOps . TypeError ( "2nd arg must be a tuple " ) ;
43+ [ Documentation ( "start_new_thread(function, args, [kwDict]) -> thread id\n Creates a new thread running the given function" ) ]
44+ public static object start_new_thread ( CodeContext /*!*/ context , object ? function , object ? args , object ? kwDict ) {
45+ if ( args is not PythonTuple tupArgs ) throw PythonOps . TypeError ( "2nd arg must be a tuple" ) ;
46+ if ( kwDict is not PythonDictionary dict ) throw PythonOps . TypeError ( "optional 3rd arg must be a dictionary " ) ;
4747
48- Thread t = CreateThread ( context , new ThreadObj ( context , function , tupArgs , kwDict ) . Start ) ;
48+ Thread t = CreateThread ( context , new ThreadObj ( context , function , tupArgs , dict ) . Start ) ;
4949 t . Start ( ) ;
5050
5151 return t . ManagedThreadId ;
5252 }
5353
5454 [ Documentation ( "start_new_thread(function, args, [kwDict]) -> thread id\n Creates a new thread running the given function" ) ]
55- public static object start_new_thread ( CodeContext /*!*/ context , object function , object args ) {
56- PythonTuple tupArgs = args as PythonTuple ;
57- if ( tupArgs == null ) throw PythonOps . TypeError ( "2nd arg must be a tuple" ) ;
55+ public static object start_new_thread ( CodeContext /*!*/ context , object ? function , object ? args ) {
56+ if ( args is not PythonTuple tupArgs ) throw PythonOps . TypeError ( "2nd arg must be a tuple" ) ;
5857
5958 Thread t = CreateThread ( context , new ThreadObj ( context , function , tupArgs , null ) . Start ) ;
6059 t . IsBackground = true ;
@@ -109,8 +108,13 @@ public static int stack_size(CodeContext/*!*/ context, int size) {
109108 }
110109
111110 // deprecated synonyms, wrappers over preferred names...
112- [ Documentation ( "start_new(function, [args, [kwDict]]) -> thread id\n Creates a new thread running the given function" ) ]
113- public static object start_new ( CodeContext context , object function , object args ) {
111+ [ Documentation ( "start_new(function, args, [kwDict]) -> thread id\n Creates a new thread running the given function" ) ]
112+ public static object start_new ( CodeContext context , object ? function , object ? args , object ? kwDict ) {
113+ return start_new_thread ( context , function , args , kwDict ) ;
114+ }
115+
116+ [ Documentation ( "start_new(function, args, [kwDict]) -> thread id\n Creates a new thread running the given function" ) ]
117+ public static object start_new ( CodeContext context , object ? function , object ? args ) {
114118 return start_new_thread ( context , function , args ) ;
115119 }
116120
@@ -138,8 +142,6 @@ public static object _set_sentinel(CodeContext context) {
138142
139143 #endregion
140144
141- #nullable enable
142-
143145 [ PythonType , PythonHidden ]
144146 public sealed class @lock {
145147 private AutoResetEvent ? blockEvent ;
@@ -321,8 +323,6 @@ private void CreateBlockEvent() {
321323 }
322324 }
323325
324- #nullable restore
325-
326326 #region Internal Implementation details
327327
328328 private static Thread CreateThread ( CodeContext /*!*/ context , ThreadStart start ) {
@@ -331,12 +331,12 @@ private static Thread CreateThread(CodeContext/*!*/ context, ThreadStart start)
331331 }
332332
333333 private class ThreadObj {
334- private readonly object _func , _kwargs ;
334+ private readonly object ? _func ;
335+ private readonly PythonDictionary ? _kwargs ;
335336 private readonly PythonTuple _args ;
336337 private readonly CodeContext _context ;
337338
338- public ThreadObj ( CodeContext context , object function , PythonTuple args , object kwargs ) {
339- Debug . Assert ( args != null ) ;
339+ public ThreadObj ( CodeContext context , object ? function , PythonTuple args , PythonDictionary ? kwargs ) {
340340 _func = function ;
341341 _kwargs = kwargs ;
342342 _args = args ;
@@ -349,13 +349,11 @@ public void Start() {
349349 _context . LanguageContext . SetModuleState ( _threadCountKey , startCount + 1 ) ;
350350 }
351351 try {
352- #pragma warning disable 618 // TODO: obsolete
353352 if ( _kwargs != null ) {
354- PythonOps . CallWithArgsTupleAndKeywordDictAndContext ( _context , _func , [ ] , [ ] , _args , _kwargs ) ;
353+ PythonCalls . CallWithKeywordArgs ( _context , _func , _args . ToArray ( ) , new PythonDictionary ( _kwargs ) ) ;
355354 } else {
356- PythonOps . CallWithArgsTuple ( _func , [ ] , _args ) ;
355+ PythonCalls . Call ( _context , _func , _args . ToArray ( ) ) ;
357356 }
358- #pragma warning restore 618
359357 } catch ( SystemExitException ) {
360358 // ignore and quit
361359 } catch ( Exception e ) {
@@ -397,17 +395,17 @@ public class _local {
397395 #region Custom Attribute Access
398396
399397 [ SpecialName ]
400- public object GetCustomMember ( string name ) {
398+ public object GetCustomMember ( [ NotNone ] string name ) {
401399 return _dict . get ( name , OperationFailed . Value ) ;
402400 }
403401
404402 [ SpecialName ]
405- public void SetMemberAfter ( string name , object value ) {
403+ public void SetMemberAfter ( [ NotNone ] string name , object ? value ) {
406404 _dict [ name ] = value ;
407405 }
408406
409407 [ SpecialName ]
410- public void DeleteMember ( string name ) {
408+ public void DeleteMember ( [ NotNone ] string name ) {
411409 _dict . __delitem__ ( name ) ;
412410 }
413411
@@ -428,21 +426,21 @@ public PythonDictionary/*!*/ __dict__ {
428426 private class ThreadLocalDictionaryStorage : DictionaryStorage {
429427 private readonly Microsoft . Scripting . Utils . ThreadLocal < CommonDictionaryStorage > _storage = new Microsoft . Scripting . Utils . ThreadLocal < CommonDictionaryStorage > ( ) ;
430428
431- public override void Add ( ref DictionaryStorage storage , object key , object value ) {
429+ public override void Add ( ref DictionaryStorage storage , object ? key , object ? value ) {
432430 GetStorage ( ) . Add ( key , value ) ;
433431 }
434432
435- public override bool Contains ( object key ) {
433+ public override bool Contains ( object ? key ) {
436434 return GetStorage ( ) . Contains ( key ) ;
437435 }
438436
439- public override bool Remove ( ref DictionaryStorage storage , object key ) {
437+ public override bool Remove ( ref DictionaryStorage storage , object ? key ) {
440438 return GetStorage ( ) . Remove ( ref storage , key ) ;
441439 }
442440
443441 public override DictionaryStorage AsMutable ( ref DictionaryStorage storage ) => this ;
444442
445- public override bool TryGetValue ( object key , out object value ) {
443+ public override bool TryGetValue ( object ? key , out object ? value ) {
446444 return GetStorage ( ) . TryGetValue ( key , out value ) ;
447445 }
448446
@@ -454,7 +452,7 @@ public override void Clear(ref DictionaryStorage storage) {
454452 GetStorage ( ) . Clear ( ref storage ) ;
455453 }
456454
457- public override List < KeyValuePair < object , object > > /*!*/ GetItems ( ) {
455+ public override List < KeyValuePair < object ? , object ? > > /*!*/ GetItems ( ) {
458456 return GetStorage ( ) . GetItems ( ) ;
459457 }
460458
0 commit comments