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

Skip to content

Commit c6cc3da

Browse files
committed
fixed bug of Exec method pythonnet#591
1 parent 46d56af commit c6cc3da

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/embed_tests/pyrunstring.cs

+15
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,20 @@ public void TestExec()
5757
object c = locals.GetItem("c").AsManagedObject(typeof(int));
5858
Assert.AreEqual(111, c);
5959
}
60+
61+
[Test]
62+
public void TestExec2()
63+
{
64+
string code = @"
65+
class Test1():
66+
pass
67+
68+
class Test2():
69+
def __init__(self):
70+
Test1()
71+
72+
Test2()";
73+
PythonEngine.Exec(code);
74+
}
6075
}
6176
}

src/runtime/pythonengine.cs

+2-8
Original file line numberDiff line numberDiff line change
@@ -496,12 +496,10 @@ internal static PyObject RunString(string code, IntPtr? globals, IntPtr? locals,
496496
borrowedGlobals = false;
497497
}
498498
}
499-
500-
var borrowedLocals = true;
499+
501500
if (locals == null)
502501
{
503-
locals = Runtime.PyDict_New();
504-
borrowedLocals = false;
502+
locals = globals;
505503
}
506504

507505
try
@@ -516,10 +514,6 @@ internal static PyObject RunString(string code, IntPtr? globals, IntPtr? locals,
516514
}
517515
finally
518516
{
519-
if (!borrowedLocals)
520-
{
521-
Runtime.XDecref(locals.Value);
522-
}
523517
if (!borrowedGlobals)
524518
{
525519
Runtime.XDecref(globals.Value);

0 commit comments

Comments
 (0)