@@ -24,7 +24,7 @@ public static ShutdownMode ShutdownMode
24
24
25
25
public static ShutdownMode DefaultShutdownMode => Runtime . GetDefaultShutdownMode ( ) ;
26
26
27
- private static DelegateManager delegateManager ;
27
+ private static DelegateManager ? delegateManager ;
28
28
private static bool initialized ;
29
29
private static IntPtr _pythonHome = IntPtr . Zero ;
30
30
private static IntPtr _programName = IntPtr . Zero ;
@@ -223,7 +223,7 @@ public static void Initialize(IEnumerable<string> args, bool setSysArgv = true,
223
223
224
224
// Load the clr.py resource into the clr module
225
225
NewReference clr = Python . Runtime . ImportHook . GetCLRModule ( ) ;
226
- BorrowedReference clr_dict = Runtime . PyModule_GetDict ( clr ) ;
226
+ BorrowedReference clr_dict = Runtime . PyModule_GetDict ( clr . Borrow ( ) ) ;
227
227
228
228
var locals = new PyDict ( ) ;
229
229
try
@@ -246,7 +246,7 @@ public static void Initialize(IEnumerable<string> args, bool setSysArgv = true,
246
246
using ( var keys = locals . Keys ( ) )
247
247
foreach ( PyObject key in keys )
248
248
{
249
- if ( ! key . ToString ( ) . StartsWith ( "_" ) || key . ToString ( ) . Equals ( "__version__" ) )
249
+ if ( ! key . ToString ( ) ! . StartsWith ( "_" ) || key . ToString ( ) ! . Equals ( "__version__" ) )
250
250
{
251
251
PyObject value = locals [ key ] ;
252
252
Runtime . PyDict_SetItem ( clr_dict , key . Reference , value . Reference ) ;
@@ -272,7 +272,7 @@ static BorrowedReference DefineModule(string name)
272
272
273
273
static void LoadSubmodule ( BorrowedReference targetModuleDict , string fullName , string resourceName )
274
274
{
275
- string memberName = fullName . AfterLast ( '.' ) ;
275
+ string ? memberName = fullName . AfterLast ( '.' ) ;
276
276
Debug . Assert ( memberName != null ) ;
277
277
278
278
var module = DefineModule ( fullName ) ;
@@ -282,7 +282,7 @@ static void LoadSubmodule(BorrowedReference targetModuleDict, string fullName, s
282
282
string pyCode = assembly . ReadStringResource ( resourceName ) ;
283
283
Exec ( pyCode , module_globals . DangerousGetAddress ( ) , module_globals . DangerousGetAddress ( ) ) ;
284
284
285
- Runtime . PyDict_SetItemString ( targetModuleDict , memberName , module ) ;
285
+ Runtime . PyDict_SetItemString ( targetModuleDict , memberName ! , module ) ;
286
286
}
287
287
288
288
static void LoadMixins ( BorrowedReference targetModuleDict )
@@ -511,9 +511,9 @@ internal static void ReleaseLock(PyGILState gs)
511
511
/// For more information, see the "Extending and Embedding" section
512
512
/// of the Python documentation on www.python.org.
513
513
/// </remarks>
514
- public static IntPtr BeginAllowThreads ( )
514
+ public static unsafe IntPtr BeginAllowThreads ( )
515
515
{
516
- return Runtime . PyEval_SaveThread ( ) ;
516
+ return ( IntPtr ) Runtime . PyEval_SaveThread ( ) ;
517
517
}
518
518
519
519
@@ -527,9 +527,9 @@ public static IntPtr BeginAllowThreads()
527
527
/// For more information, see the "Extending and Embedding" section
528
528
/// of the Python documentation on www.python.org.
529
529
/// </remarks>
530
- public static void EndAllowThreads ( IntPtr ts )
530
+ public static unsafe void EndAllowThreads ( IntPtr ts )
531
531
{
532
- Runtime . PyEval_RestoreThread ( ts ) ;
532
+ Runtime . PyEval_RestoreThread ( ( PyThreadState * ) ts ) ;
533
533
}
534
534
535
535
public static PyObject Compile ( string code , string filename = "" , RunFlagType mode = RunFlagType . File )
@@ -644,7 +644,8 @@ internal static PyObject RunString(string code, BorrowedReference globals, Borro
644
644
globals = Runtime . PyEval_GetGlobals ( ) ;
645
645
if ( globals . IsNull )
646
646
{
647
- globals = tempGlobals = NewReference . DangerousFromPointer ( Runtime . PyDict_New ( ) ) ;
647
+ tempGlobals = Runtime . PyDict_New ( ) ;
648
+ globals = tempGlobals . BorrowOrThrow ( ) ;
648
649
Runtime . PyDict_SetItem (
649
650
globals , PyIdentifier . __builtins__ ,
650
651
Runtime . PyEval_GetBuiltins ( )
@@ -698,7 +699,7 @@ public static PyModule CreateScope(string name)
698
699
699
700
public class GILState : IDisposable
700
701
{
701
- private readonly IntPtr state ;
702
+ private readonly PyGILState state ;
702
703
private bool isDisposed ;
703
704
704
705
internal GILState ( )
@@ -750,22 +751,29 @@ public static KeywordArguments kw(params object?[] kv)
750
751
}
751
752
for ( var i = 0 ; i < kv . Length ; i += 2 )
752
753
{
753
- IntPtr value ;
754
- if ( kv [ i + 1 ] is PyObject )
754
+ var key = kv [ i ] as string ;
755
+ if ( key is null )
756
+ throw new ArgumentException ( "Keys must be non-null strings" ) ;
757
+
758
+ BorrowedReference value ;
759
+ NewReference temp = default ;
760
+ if ( kv [ i + 1 ] is PyObject pyObj )
755
761
{
756
- value = ( ( PyObject ) kv [ i + 1 ] ) . Handle ;
762
+ value = pyObj ;
757
763
}
758
764
else
759
765
{
760
- value = Converter . ToPython ( kv [ i + 1 ] , kv [ i + 1 ] ? . GetType ( ) ) ;
761
- }
762
- if ( Runtime . PyDict_SetItemString ( dict . Handle , ( string ) kv [ i ] , value ) != 0 )
763
- {
764
- throw new ArgumentException ( string . Format ( "Cannot add key '{0}' to dictionary." , ( string ) kv [ i ] ) ) ;
766
+ temp = Converter . ToPythonDetectType ( kv [ i + 1 ] ) ;
767
+ value = temp . Borrow ( ) ;
765
768
}
766
- if ( ! ( kv [ i + 1 ] is PyObject ) )
769
+ using ( temp )
767
770
{
768
- Runtime . XDecref ( value ) ;
771
+ if ( Runtime . PyDict_SetItemString ( dict , key , value ) != 0 )
772
+ {
773
+ throw new ArgumentException (
774
+ string . Format ( "Cannot add key '{0}' to dictionary." , key ) ,
775
+ innerException : PythonException . FetchCurrent ( ) ) ;
776
+ }
769
777
}
770
778
}
771
779
return dict ;
@@ -821,8 +829,8 @@ public static void With(PyObject obj, Action<dynamic> Body)
821
829
// Behavior described here:
822
830
// https://docs.python.org/2/reference/datamodel.html#with-statement-context-managers
823
831
824
- Exception ex = null ;
825
- PythonException pyError = null ;
832
+ Exception ? ex = null ;
833
+ PythonException ? pyError = null ;
826
834
827
835
try
828
836
{
0 commit comments