FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Stricter coversion of primitive Python types to `System.Object` by lostmsu · Pull Request #1958 · pythonnet/pythonnet · GitHub

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cs  (3) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
20 changes: 20 additions & 0 deletions src/embed_tests/TestConverter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,26 @@ public void ToNullable()
Assert.AreEqual(Const, ni);
}

[Test]
public void DerivedFloatIsNotFloat()
{
using var scope = Py.CreateScope();
scope.Exec(@"class DerivedFloat(float): pass");
using var derivedFloat = scope.Eval("DerivedFloat(42.0)");
object dotnetValue = derivedFloat.As<object>();
Assert.IsInstanceOf<PyObject>(dotnetValue);
}

[Test]
public void DerivedIntIsNotInt()
{
using var scope = Py.CreateScope();
scope.Exec(@"class DerivedInt(int): pass");
using var derivedInt = scope.Eval("DerivedInt(42)");
object dotnetValue = derivedInt.As<object>();
Assert.IsInstanceOf<PyObject>(dotnetValue);
}

[Test]
public void BigIntExplicit()
{
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/Converter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -361,17 +361,17 @@ internal static bool ToManagedValue(BorrowedReference value, Type obType,
// conversions (Python string -> managed string).
if (obType == objectType)
{
if (Runtime.PyString_Check(value))
if (Runtime.PyString_CheckExact(value))
{
return ToPrimitive(value, stringType, out result, setError);
}

if (Runtime.PyBool_Check(value))
if (Runtime.PyBool_CheckExact(value))
{
return ToPrimitive(value, boolType, out result, setError);
}

if (Runtime.PyFloat_Check(value))
if (Runtime.PyFloat_CheckExact(value))
{
return ToPrimitive(value, doubleType, out result, setError);
}
Expand Down
15 changes: 12 additions & 3 deletions src/runtime/Runtime.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1094,8 +1094,13 @@ internal static nint PyBuffer_SizeFromFormat(string format)
internal static bool PyInt_Check(BorrowedReference ob)
=> PyObject_TypeCheck(ob, PyLongType);

internal static bool PyInt_CheckExact(BorrowedReference ob)
=> PyObject_TypeCheckExact(ob, PyLongType);

internal static bool PyBool_Check(BorrowedReference ob)
=> PyObject_TypeCheck(ob, PyBoolType);
internal static bool PyBool_CheckExact(BorrowedReference ob)
=> PyObject_TypeCheckExact(ob, PyBoolType);

internal static NewReference PyInt_FromInt32(int value) => PyLong_FromLongLong(value);

Expand Down Expand Up @@ -1141,6 +1146,8 @@ internal static NewReference PyLong_FromString(string value, int radix)

internal static bool PyFloat_Check(BorrowedReference ob)
=> PyObject_TypeCheck(ob, PyFloatType);
internal static bool PyFloat_CheckExact(BorrowedReference ob)
=> PyObject_TypeCheckExact(ob, PyFloatType);

/// <summary>
/// Return value: New reference.
Expand Down Expand Up @@ -1282,9 +1289,9 @@ internal static bool PyFloat_Check(BorrowedReference ob)
// Python string API
//====================================================================
internal static bool PyString_Check(BorrowedReference ob)
{
return PyObject_TYPE(ob) == PyStringType;
}
=> PyObject_TypeCheck(ob, PyStringType);
internal static bool PyString_CheckExact(BorrowedReference ob)
=> PyObject_TypeCheckExact(ob, PyStringType);

internal static NewReference PyString_FromString(string value)
{
Expand Down Expand Up @@ -1643,6 +1650,8 @@ internal static bool PyType_IsSubtype(BorrowedReference t1, BorrowedReference t2
return Delegates.PyType_IsSubtype(t1, t2);
}

internal static bool PyObject_TypeCheckExact(BorrowedReference ob, BorrowedReference tp)
=> PyObject_TYPE(ob) == tp;
internal static bool PyObject_TypeCheck(BorrowedReference ob, BorrowedReference tp)
{
BorrowedReference t = PyObject_TYPE(ob);
Expand Down

Back | FazBrowse Home | New Git URL