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

Skip to content

Commit ec65efe

Browse files
authored
fixed dynamic unary and binary operations not raising Python exception on failure (#1508)
Instead, they crashed with ArgumentNullException due to null pointer passed to PyObject constructor. Fixed by adding a check for None.
1 parent 4529fde commit ec65efe

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/embed_tests/TestPyObject.cs

+8
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,13 @@ public void AsManagedObjectInvalidCast()
7171
var list = PythonEngine.Eval("list");
7272
Assert.Throws<InvalidCastException>(() => list.AsManagedObject(typeof(int)));
7373
}
74+
75+
[Test]
76+
public void UnaryMinus_ThrowsOnBadType()
77+
{
78+
dynamic list = new PyList();
79+
var error = Assert.Throws<PythonException>(() => list = -list);
80+
Assert.AreEqual("TypeError", error.Type.Name);
81+
}
7482
}
7583
}

src/runtime/pyobject.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1398,6 +1398,7 @@ public override bool TryBinaryOperation(BinaryOperationBinder binder, object arg
13981398
result = null;
13991399
return false;
14001400
}
1401+
Exceptions.ErrorCheck(res);
14011402
result = CheckNone(new PyObject(res));
14021403
return true;
14031404
}
@@ -1450,6 +1451,7 @@ public override bool TryUnaryOperation(UnaryOperationBinder binder, out object r
14501451
result = null;
14511452
return false;
14521453
}
1454+
Exceptions.ErrorCheck(res);
14531455
result = CheckNone(new PyObject(res));
14541456
return true;
14551457
}

0 commit comments

Comments
 (0)