8000 PythonEngine .Exec and .Eval no longer work with raw pointers · pythonnet/pythonnet@e003e12 · GitHub
[go: up one dir, main page]

Skip to content

Commit e003e12

Browse files
committed
PythonEngine .Exec and .Eval no longer work with raw pointers
1 parent d3e4fba commit e003e12

File tree

8 files changed

+18
-21
lines changed

8 files changed

+18
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ One must now either use enum members (e.g. `MyEnum.Option`), or use enum constru
6565
- BREAKING: Names of .NET types (e.g. `str(__class__)`) changed to better support generic types
6666
- BREAKING: overload resolution will no longer prefer basic types. Instead, first matching overload will
6767
be chosen.
68+
- BREAKING: `Exec` and `Eval` from `PythonEngine` no longer accept raw pointers.
6869
- BREAKING: .NET collections and arrays are no longer automatically converted to
6970
Python collections. Instead, they implement standard Python
7071
collection interfaces from `collections.abc`.

src/embed_tests/CallableObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void SetUp()
1414
{
1515
PythonEngine.Initialize();
1616
using var locals = new PyDict();
17-
PythonEngine.Exec(CallViaInheritance.BaseClassSource, locals: locals.Handle);
17+
PythonEngine.Exec(CallViaInheritance.BaseClassSource, locals: locals);
1818
CustomBaseTypeProvider.BaseClass = new PyType(locals[CallViaInheritance.BaseClassName]);
1919
PythonEngine.InteropConfiguration.PythonBaseTypeProviders.Add(new CustomBaseTypeProvider());
2020
}

src/embed_tests/Inheritance.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class Inheritance
1313
public void SetUp()
1414
{
1515
PythonEngine.Initialize();
16-
var locals = new PyDict();
17-
PythonEngine.Exec(InheritanceTestBaseClassWrapper.ClassSourceCode, locals: locals.Handle);
16+
using var locals = new PyDict();
17+
PythonEngine.Exec(InheritanceTestBaseClassWrapper.ClassSourceCode, locals: locals);
1818
ExtraBaseTypeProvider.ExtraBase = new PyType(locals[InheritanceTestBaseClassWrapper.ClassName]);
1919
var baseTypeProviders = PythonEngine.InteropConfiguration.PythonBaseTypeProviders;
2020
baseTypeProviders.Add(new ExtraBaseTypeProvider());

src/embed_tests/TestNamedArguments.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def Test3(self, a1 = 1, a2 = 1, a3 = 1, a4 = 1):
5555
return a1 + a2 + a3 + a4
5656
5757
a = cmTest3()
58-
", null, locals.Handle);
58+
", null, locals);
5959

6060
return locals.GetItem("a");
6161
}

src/embed_tests/TestPyObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def add(self, x, y):
4646
return x + y
4747
4848
a = MemberNamesTest()
49-
", null, locals.Handle);
49+
", null, locals);
5050

5151
PyObject a = locals.GetItem("a");
5252

src/embed_tests/TestPyWith.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def fail(self):
3737
return 5 / 0
3838
3939
a = CmTest()
40-
", null, locals.Handle);
40+
", null, locals);
4141

4242
var a = locals.GetItem("a");
4343

@@ -76,7 +76,7 @@ def fail(self):
7676
return 5 / 0
7777
7878
a = CmTest()
79-
", null, locals.Handle);
79+
", null, locals);
8080

8181
var a = locals.GetItem("a");
8282
Py.With(a, cmTest =>

src/embed_tests/pyrunstring.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void TestEval()
3737
locals.SetItem("sys", sys);
3838
locals.SetItem("a", new PyInt(10));
3939

40-
object b = PythonEngine.Eval("sys.attr1 + a + 1", null, locals.Handle)
40+
object b = PythonEngine.Eval("sys.attr1 + a + 1", null, locals)
4141
.AsManagedObject(typeof(int));
4242
Assert.AreEqual(111, b);
4343
}
@@ -51,7 +51,7 @@ public void TestExec()
5151
locals.SetItem("sys", sys);
5252
locals.SetItem("a", new PyInt(10));
5353

54-
PythonEngine.Exec("c = sys.attr1 + a + 1", null, locals.Handle);
54+
PythonEngine.Exec("c = sys.attr1 + a + 1", null, locals);
5555
object c = locals.GetItem("c").AsManagedObject(typeof(int));
5656
Assert.AreEqual(111, c);
5757
}

src/runtime/pythonengine.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public static void Initialize(IEnumerable<string> args, bool setSysArgv = true,
238238

239239
LoadSubmodule(module_globals, "clr.interop", "interop.py");
240240

241-
LoadMixins(module_globals);
241+
LoadMixins(module_globals);
242242

243243
// add the imported module to the clr module, and copy the API functions
244244
// and decorators into the main clr module.
@@ -280,7 +280,7 @@ static void LoadSubmodule(BorrowedReference targetModuleDict, string fullName, s
280280

281281
Assembly assembly = Assembly.GetExecutingAssembly();
282282
string pyCode = assembly.ReadStringResource(resourceName);
283-
Exec(pyCode, module_globals.DangerousGetAddress(), module_globals.DangerousGetAddress());
283+
Exec(pyCode, module_globals, module_globals);
284284

285285
Runtime.PyDict_SetItemString(targetModuleDict, memberName!, module);
286286
}
@@ -548,11 +548,9 @@ public static PyObject Compile(string code, string filename = "", RunFlagType mo
548548
/// Evaluate a Python expression and returns the result.
549549
/// It's a subset of Python eval function.
550550
/// </remarks>
551-
public static PyObject Eval(string code, IntPtr? globals = null, IntPtr? locals = null)
551+
public static PyObject Eval(string code, PyDict? globals = null, PyObject? locals = null)
552552
{
553-
var globalsRef = new BorrowedReference(globals.GetValueOrDefault());
554-
var localsRef = new BorrowedReference(locals.GetValueOrDefault());
555-
PyObject result = RunString(code, globalsRef, localsRef, RunFlagType.Eval);
553+
PyObject result = RunString(code, globals.BorrowNullable(), locals.BorrowNullable(), RunFlagType.Eval);
556554
return result;
557555
}
558556

@@ -564,11 +562,9 @@ public static PyObject Eval(string code, IntPtr? globals = null, IntPtr? locals
564562
/// Run a string containing Python code.
565563
/// It's a subset of Python exec function.
566564
/// </remarks>
567-
public static void Exec(string code, IntPtr? globals = null, IntPtr? locals = null)
565+
public static void Exec(string code, PyDict? globals = null, PyObject? locals = null)
568566
{
569-
var globalsRef = new BorrowedReference(globals.GetValueOrDefault());
570-
var localsRef = new BorrowedReference(locals.GetValueOrDefault());
571-
using PyObject result = RunString(code, globalsRef, localsRef, RunFlagType.File);
567+
using PyObject result = RunString(code, globals.BorrowNullable(), locals.BorrowNullable(), RunFlagType.File);
572568
if (result.obj != Runtime.PyNone)
573569
{
574570
throw PythonException.ThrowLastAsClrException();
@@ -621,9 +617,9 @@ public static int Interrupt(ulong pythonThreadID)
621617
/// Use Exec/Eval/RunSimpleString instead.
622618
/// </summary>
623619
[Obsolete("RunString is deprecated and will be removed. Use Exec/Eval/RunSimpleString instead.")]
624-
public static PyObject RunString(string code, IntPtr? globals = null, IntPtr? locals = null)
620+
public static PyObject RunString(string code, PyDict? globals = null, PyObject? locals = null)
625621
{
626-
return RunString(code, new BorrowedReference(globals.GetValueOrDefault()), new BorrowedReference(locals.GetValueOrDefault()), RunFlagType.File);
622+
return RunString(code, globals.BorrowNullable(), locals.BorrowNullable(), RunFlagType.File);
627623
}
628624

629625
/// <summary>

0 commit comments

Comments
 (0)
0