2
2
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
3
3
// See the LICENSE file in the project root for more information.
4
4
5
+ #nullable enable
6
+
5
7
using System ;
6
8
using System . Collections . Generic ;
7
- using System . Diagnostics ;
8
9
using System . Threading ;
9
10
10
11
using IronPython . Runtime ;
14
15
15
16
using Microsoft . Scripting ;
16
17
using Microsoft . Scripting . Runtime ;
17
- using Microsoft . Scripting . Utils ;
18
18
19
19
using SpecialName = System . Runtime . CompilerServices . SpecialNameAttribute ;
20
20
@@ -25,7 +25,7 @@ public static class PythonThread {
25
25
26
26
private static readonly object _stackSizeKey = new object ( ) ;
27
27
private static object _threadCountKey = new object ( ) ;
28
- [ ThreadStatic ] private static List < @lock > _sentinelLocks ;
28
+ [ ThreadStatic ] private static List < @lock > ? _sentinelLocks ;
29
29
30
30
[ SpecialName ]
31
31
public static void PerformModuleReload ( PythonContext /*!*/ context , PythonDictionary /*!*/ dict ) {
@@ -40,21 +40,20 @@ public static void PerformModuleReload(PythonContext/*!*/ context, PythonDiction
40
40
[ System . Diagnostics . CodeAnalysis . SuppressMessage ( "Microsoft.Security" , "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes" ) ]
41
41
public static readonly PythonType LockType = DynamicHelpers . GetPythonTypeFromType ( typeof ( @lock ) ) ;
42
42
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 " ) ;
47
47
48
- Thread t = CreateThread ( context , new ThreadObj ( context , function , tupArgs , kwDict ) . Start ) ;
48
+ Thread t = CreateThread ( context , new ThreadObj ( context , function , tupArgs , dict ) . Start ) ;
49
49
t . Start ( ) ;
50
50
51
51
return t . ManagedThreadId ;
52
52
}
53
53
54
54
[ 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" ) ;
58
57
59
58
Thread t = CreateThread ( context , new ThreadObj ( context , function , tupArgs , null ) . Start ) ;
60
59
t . IsBackground = true ;
@@ -109,8 +108,13 @@ public static int stack_size(CodeContext/*!*/ context, int size) {
109
108
}
110
109
111
110
// 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 ) {
114
118
return start_new_thread ( context , function , args ) ;
115
119
}
116
120
@@ -138,8 +142,6 @@ public static object _set_sentinel(CodeContext context) {
138
142
139
143
#endregion
140
144
141
- #nullable enable
142
-
143
145
[ PythonType , PythonHidden ]
144
146
public sealed class @lock {
145
147
private AutoResetEvent ? blockEvent ;
@@ -321,8 +323,6 @@ private void CreateBlockEvent() {
321
323
}
322
324
}
323
325
324
- #nullable restore
325
-
326
326
#region Internal Implementation details
327
327
328
328
private static Thread CreateThread ( CodeContext /*!*/ context , ThreadStart start ) {
@@ -331,12 +331,12 @@ private static Thread CreateThread(CodeContext/*!*/ context, ThreadStart start)
331
331
}
332
332
333
333
private class ThreadObj {
334
- private readonly object _func , _kwargs ;
334
+ private readonly object ? _func ;
335
+ private readonly PythonDictionary ? _kwargs ;
335
336
private readonly PythonTuple _args ;
336
337
private readonly CodeContext _context ;
337
338
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 ) {
340
340
_func = function ;
341
341
_kwargs = kwargs ;
342
342
_args = args ;
@@ -349,13 +349,11 @@ public void Start() {
349
349
_context . LanguageContext . SetModuleState ( _threadCountKey , startCount + 1 ) ;
350
350
}
351
351
try {
352
- #pragma warning disable 618 // TODO: obsolete
353
352
if ( _kwargs != null ) {
354
- PythonOps . CallWithArgsTupleAndKeywordDictAndContext ( _context , _func , [ ] , [ ] , _args , _kwargs ) ;
353
+ PythonCalls . CallWithKeywordArgs ( _context , _func , _args . ToArray ( ) , new PythonDictionary ( _kwargs ) ) ;
355
354
} else {
356
- PythonOps . CallWithArgsTuple ( _func , [ ] , _args ) ;
355
+ PythonCalls . Call ( _context , _func , _args . ToArray ( ) ) ;
357
356
}
358
- #pragma warning restore 618
359
357
} catch ( SystemExitException ) {
360
358
// ignore and quit
361
359
} catch ( Exception e ) {
@@ -397,17 +395,17 @@ public class _local {
397
395
#region Custom Attribute Access
398
396
399
397
[ SpecialName ]
400
- public object GetCustomMember ( string name ) {
398
+ public object GetCustomMember ( [ NotNone ] string name ) {
401
399
return _dict . get ( name , OperationFailed . Value ) ;
402
400
}
403
401
404
402
[ SpecialName ]
405
- public void SetMemberAfter ( string name , object value ) {
403
+ public void SetMemberAfter ( [ NotNone ] string name , object ? value ) {
406
404
_dict [ name ] = value ;
407
405
}
408
406
409
407
[ SpecialName ]
410
- public void DeleteMember ( string name ) {
408
+ public void DeleteMember ( [ NotNone ] string name ) {
411
409
_dict . __delitem__ ( name ) ;
412
410
}
413
411
@@ -428,21 +426,21 @@ public PythonDictionary/*!*/ __dict__ {
428
426
private class ThreadLocalDictionaryStorage : DictionaryStorage {
429
427
private readonly Microsoft . Scripting . Utils . ThreadLocal < CommonDictionaryStorage > _storage = new Microsoft . Scripting . Utils . ThreadLocal < CommonDictionaryStorage > ( ) ;
430
428
431
- public override void Add ( ref DictionaryStorage storage , object key , object value ) {
429
+ public override void Add ( ref DictionaryStorage storage , object ? key , object ? value ) {
432
430
GetStorage ( ) . Add ( key , value ) ;
433
431
}
434
432
435
- public override bool Contains ( object key ) {
433
+ public override bool Contains ( object ? key ) {
436
434
return GetStorage ( ) . Contains ( key ) ;
437
435
}
438
436
439
- public override bool Remove ( ref DictionaryStorage storage , object key ) {
437
+ public override bool Remove ( ref DictionaryStorage storage , object ? key ) {
440
438
return GetStorage ( ) . Remove ( ref storage , key ) ;
441
439
}
442
440
443
441
public override DictionaryStorage AsMutable ( ref DictionaryStorage storage ) => this ;
444
442
445
- public override bool TryGetValue ( object key , out object value ) {
443
+ public override bool TryGetValue ( object ? key , out object ? value ) {
446
444
return GetStorage ( ) . TryGetValue ( key , out value ) ;
447
445
}
448
446
@@ -454,7 +452,7 @@ public override void Clear(ref DictionaryStorage storage) {
454
452
GetStorage ( ) . Clear ( ref storage ) ;
455
453
}
456
454
457
- public override List < KeyValuePair < object , object > > /*!*/ GetItems ( ) {
455
+ public override List < KeyValuePair < object ? , object ? > > /*!*/ GetItems ( ) {
458
456
return GetStorage ( ) . GetItems ( ) ;
459
457
}
460
458
0 commit comments