Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2dd3f8f

Browse files
committed
switched pythonengine.cs to the new style references
1 parent 7adf98a commit 2dd3f8f

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

src/runtime/pythonengine.cs

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static ShutdownMode ShutdownMode
2424

2525
public static ShutdownMode DefaultShutdownMode => Runtime.GetDefaultShutdownMode();
2626

27-
private static DelegateManager delegateManager;
27+
private static DelegateManager? delegateManager;
2828
private static bool initialized;
2929
private static IntPtr _pythonHome = IntPtr.Zero;
3030
private static IntPtr _programName = IntPtr.Zero;
@@ -223,7 +223,7 @@ public static void Initialize(IEnumerable<string> args, bool setSysArgv = true,
223223

224224
// Load the clr.py resource into the clr module
225225
NewReference clr = Python.Runtime.ImportHook.GetCLRModule();
226-
BorrowedReference clr_dict = Runtime.PyModule_GetDict(clr);
226+
BorrowedReference clr_dict = Runtime.PyModule_GetDict(clr.Borrow());
227227

228228
var locals = new PyDict();
229229
try
@@ -246,7 +246,7 @@ public static void Initialize(IEnumerable<string> args, bool setSysArgv = true,
246246
using (var keys = locals.Keys())
247247
foreach (PyObject key in keys)
248248
{
249-
if (!key.ToString().StartsWith("_") || key.ToString().Equals("__version__"))
249+
if (!key.ToString()!.StartsWith("_") || key.ToString()!.Equals("__version__"))
250250
{
251251
PyObject value = locals[key];
252252
Runtime.PyDict_SetItem(clr_dict, key.Reference, value.Reference);
@@ -272,7 +272,7 @@ static BorrowedReference DefineModule(string name)
272272

273273
static void LoadSubmodule(BorrowedReference targetModuleDict, string fullName, string resourceName)
274274
{
275-
string memberName = fullName.AfterLast('.');
275+
string? memberName = fullName.AfterLast('.');
276276
Debug.Assert(memberName != null);
277277

278278
var module = DefineModule(fullName);
@@ -282,7 +282,7 @@ static void LoadSubmodule(BorrowedReference targetModuleDict, string fullName, s
282282
string pyCode = assembly.ReadStringResource(resourceName);
283283
Exec(pyCode, module_globals.DangerousGetAddress(), module_globals.DangerousGetAddress());
284284

285-
Runtime.PyDict_SetItemString(targetModuleDict, memberName, module);
285+
Runtime.PyDict_SetItemString(targetModuleDict, memberName!, module);
286286
}
287287

288288
static void LoadMixins(BorrowedReference targetModuleDict)
@@ -511,9 +511,9 @@ internal static void ReleaseLock(PyGILState gs)
511511
/// For more information, see the "Extending and Embedding" section
512512
/// of the Python documentation on www.python.org.
513513
/// </remarks>
514-
public static IntPtr BeginAllowThreads()
514+
public static unsafe IntPtr BeginAllowThreads()
515515
{
516-
return Runtime.PyEval_SaveThread();
516+
return (IntPtr)Runtime.PyEval_SaveThread();
517517
}
518518

519519

@@ -527,9 +527,9 @@ public static IntPtr BeginAllowThreads()
527527
/// For more information, see the "Extending and Embedding" section
528528
/// of the Python documentation on www.python.org.
529529
/// </remarks>
530-
public static void EndAllowThreads(IntPtr ts)
530+
public static unsafe void EndAllowThreads(IntPtr ts)
531531
{
532-
Runtime.PyEval_RestoreThread(ts);
532+
Runtime.PyEval_RestoreThread((PyThreadState*)ts);
533533
}
534534

535535
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
644644
globals = Runtime.PyEval_GetGlobals();
645645
if (globals.IsNull)
646646
{
647-
globals = tempGlobals = NewReference.DangerousFromPointer(Runtime.PyDict_New());
647+
tempGlobals = Runtime.PyDict_New();
648+
globals = tempGlobals.BorrowOrThrow();
648649
Runtime.PyDict_SetItem(
649650
globals, PyIdentifier.__builtins__,
650651
Runtime.PyEval_GetBuiltins()
@@ -698,7 +699,7 @@ public static PyModule CreateScope(string name)
698699

699700
public class GILState : IDisposable
700701
{
701-
private readonly IntPtr state;
702+
private readonly PyGILState state;
702703
private bool isDisposed;
703704

704705
internal GILState()
@@ -750,22 +751,29 @@ public static KeywordArguments kw(params object?[] kv)
750751
}
751752
for (var i = 0; i < kv.Length; i += 2)
752753
{
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)
755761
{
756-
value = ((PyObject)kv[i + 1]).Handle;
762+
value = pyObj;
757763
}
758764
else
759765
{
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();
765768
}
766-
if (!(kv[i + 1] is PyObject))
769+
using (temp)
767770
{
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+
}
769777
}
770778
}
771779
return dict;
@@ -821,8 +829,8 @@ public static void With(PyObject obj, Action<dynamic> Body)
821829
// Behavior described here:
822830
// https://docs.python.org/2/reference/datamodel.html#with-statement-context-managers
823831

824-
Exception ex = null;
825-
PythonException pyError = null;
832+
Exception? ex = null;
833+
PythonException? pyError = null;
826834

827835
try
828836
{

0 commit comments

Comments
 (0)