Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Allow decoders to decode Python types derived from primitives#1986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
filmor merged 2 commits intopythonnet:masterfromlosttech:FloatDerivedCodec
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletionssrc/embed_tests/Codecs.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -355,6 +355,19 @@ from datetime import datetime
scope.Exec("Codecs.AcceptsDateTime(datetime(2021, 1, 22))");
}

[Test]
public void FloatDerivedDecoded()
{
using var scope = Py.CreateScope();
scope.Exec(@"class FloatDerived(float): pass");
using var floatDerived = scope.Eval("FloatDerived");
var decoder = new DecoderReturningPredefinedValue<object>(floatDerived, 42);
PyObjectConversions.RegisterDecoder(decoder);
using var result = scope.Eval("FloatDerived()");
object decoded = result.As<object>();
Assert.AreEqual(42, decoded);
}

[Test]
public void ExceptionDecodedNoInstance()
{
Expand Down
24 changes: 20 additions & 4 deletionssrc/runtime/Converter.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -361,28 +361,44 @@ 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);
}

// give custom codecs a chance to take over conversion of ints and sequences
// give custom codecs a chance to take over conversion
// of ints, sequences, and types derived from primitives
BorrowedReference pyType = Runtime.PyObject_TYPE(value);
if (PyObjectConversions.TryDecode(value, pyType, obType, out result))
{
return true;
}

if (Runtime.PyString_Check(value))
{
return ToPrimitive(value, stringType, out result, setError);
}

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

if (Runtime.PyFloat_Check(value))
{
return ToPrimitive(value, doubleType, out result, setError);
}

if (Runtime.PyInt_Check(value))
{
result = new PyInt(value);
Expand Down
15 changes: 12 additions & 3 deletionssrc/runtime/Runtime.cs
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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(BorrowedReferenceob)
=> PyObject_TypeCheckExact(ob, PyStringType);

internal static NewReference PyString_FromString(string value)
{
Expand DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp