| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -355,6 +355,19 @@ from datetime import datetime | |||
| 355 | 355 | scope.Exec("Codecs.AcceptsDateTime(datetime(2021, 1, 22))"); | |
| 356 | 356 | } | |
| 357 | 357 | ||
| 358 | + [Test] | ||
| 359 | + public void FloatDerivedDecoded() | ||
| 360 | + { | ||
| 361 | + using var scope = Py.CreateScope(); | ||
| 362 | + scope.Exec(@"class FloatDerived(float): pass"); | ||
| 363 | + using var floatDerived = scope.Eval("FloatDerived"); | ||
| 364 | + var decoder = new DecoderReturningPredefinedValue<object>(floatDerived, 42); | ||
| 365 | + PyObjectConversions.RegisterDecoder(decoder); | ||
| 366 | + using var result = scope.Eval("FloatDerived()"); | ||
| 367 | + object decoded = result.As<object>(); | ||
| 368 | + Assert.AreEqual(42, decoded); | ||
| 369 | + } | ||
| 370 | + | ||
| 358 | 371 | [Test] | |
| 359 | 372 | public void ExceptionDecodedNoInstance() | |
| 360 | 373 | { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -361,28 +361,44 @@ internal static bool ToManagedValue(BorrowedReference value, Type obType, | |||
| 361 | 361 | // conversions (Python string -> managed string). | |
| 362 | 362 | if (obType == objectType) | |
| 363 | 363 | { | |
| 364 | - if (Runtime.PyString_Check(value)) | ||
| 364 | + if (Runtime.PyString_CheckExact(value)) | ||
| 365 | 365 | { | |
| 366 | 366 | return ToPrimitive(value, stringType, out result, setError); | |
| 367 | 367 | } | |
| 368 | 368 | ||
| 369 | - if (Runtime.PyBool_Check(value)) | ||
| 369 | + if (Runtime.PyBool_CheckExact(value)) | ||
| 370 | 370 | { | |
| 371 | 371 | return ToPrimitive(value, boolType, out result, setError); | |
| 372 | 372 | } | |
| 373 | 373 | ||
| 374 | - if (Runtime.PyFloat_Check(value)) | ||
| 374 | + if (Runtime.PyFloat_CheckExact(value)) | ||
| 375 | 375 | { | |
| 376 | 376 | return ToPrimitive(value, doubleType, out result, setError); | |
| 377 | 377 | } | |
| 378 | 378 | ||
| 379 | - // give custom codecs a chance to take over conversion of ints and sequences | ||
| 379 | + // give custom codecs a chance to take over conversion | ||
| 380 | + // of ints, sequences, and types derived from primitives | ||
| 380 | 381 | BorrowedReference pyType = Runtime.PyObject_TYPE(value); | |
| 381 | 382 | if (PyObjectConversions.TryDecode(value, pyType, obType, out result)) | |
| 382 | 383 | { | |
| 383 | 384 | return true; | |
| 384 | 385 | } | |
| 385 | 386 | ||
| 387 | + if (Runtime.PyString_Check(value)) | ||
| 388 | + { | ||
| 389 | + return ToPrimitive(value, stringType, out result, setError); | ||
| 390 | + } | ||
| 391 | + | ||
| 392 | + if (Runtime.PyBool_Check(value)) | ||
| 393 | + { | ||
| 394 | + return ToPrimitive(value, boolType, out result, setError); | ||
| 395 | + } | ||
| 396 | + | ||
| 397 | + if (Runtime.PyFloat_Check(value)) | ||
| 398 | + { | ||
| 399 | + return ToPrimitive(value, doubleType, out result, setError); | ||
| 400 | + } | ||
| 401 | + | ||
| 386 | 402 | if (Runtime.PyInt_Check(value)) | |
| 387 | 403 | { | |
| 388 | 404 | result = new PyInt(value); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1094,8 +1094,13 @@ internal static nint PyBuffer_SizeFromFormat(string format) | |||
| 1094 | 1094 | internal static bool PyInt_Check(BorrowedReference ob) | |
| 1095 | 1095 | => PyObject_TypeCheck(ob, PyLongType); | |
| 1096 | 1096 | ||
| 1097 | + internal static bool PyInt_CheckExact(BorrowedReference ob) | ||
| 1098 | + => PyObject_TypeCheckExact(ob, PyLongType); | ||
| 1099 | + | ||
| 1097 | 1100 | internal static bool PyBool_Check(BorrowedReference ob) | |
| 1098 | 1101 | => PyObject_TypeCheck(ob, PyBoolType); | |
| 1102 | + internal static bool PyBool_CheckExact(BorrowedReference ob) | ||
| 1103 | + => PyObject_TypeCheckExact(ob, PyBoolType); | ||
| 1099 | 1104 | ||
| 1100 | 1105 | internal static NewReference PyInt_FromInt32(int value) => PyLong_FromLongLong(value); | |
| 1101 | 1106 | ||
@@ -1141,6 +1146,8 @@ internal static NewReference PyLong_FromString(string value, int radix) | |||
| 1141 | 1146 | ||
| 1142 | 1147 | internal static bool PyFloat_Check(BorrowedReference ob) | |
| 1143 | 1148 | => PyObject_TypeCheck(ob, PyFloatType); | |
| 1149 | + internal static bool PyFloat_CheckExact(BorrowedReference ob) | ||
| 1150 | + => PyObject_TypeCheckExact(ob, PyFloatType); | ||
| 1144 | 1151 | ||
| 1145 | 1152 | /// <summary> | |
| 1146 | 1153 | /// Return value: New reference. | |
@@ -1282,9 +1289,9 @@ internal static bool PyFloat_Check(BorrowedReference ob) | |||
| 1282 | 1289 | // Python string API | |
| 1283 | 1290 | //==================================================================== | |
| 1284 | 1291 | internal static bool PyString_Check(BorrowedReference ob) | |
| 1285 | - { | ||
| 1286 | - return PyObject_TYPE(ob) == PyStringType; | ||
| 1287 | - } | ||
| 1292 | + => PyObject_TypeCheck(ob, PyStringType); | ||
| 1293 | + internal static bool PyString_CheckExact(BorrowedReference ob) | ||
| 1294 | + => PyObject_TypeCheckExact(ob, PyStringType); | ||
| 1288 | 1295 | ||
| 1289 | 1296 | internal static NewReference PyString_FromString(string value) | |
| 1290 | 1297 | { | |
@@ -1643,6 +1650,8 @@ internal static bool PyType_IsSubtype(BorrowedReference t1, BorrowedReference t2 | |||
| 1643 | 1650 | return Delegates.PyType_IsSubtype(t1, t2); | |
| 1644 | 1651 | } | |
| 1645 | 1652 | ||
| 1653 | + internal static bool PyObject_TypeCheckExact(BorrowedReference ob, BorrowedReference tp) | ||
| 1654 | + => PyObject_TYPE(ob) == tp; | ||
| 1646 | 1655 | internal static bool PyObject_TypeCheck(BorrowedReference ob, BorrowedReference tp) | |
| 1647 | 1656 | { | |
| 1648 | 1657 | BorrowedReference t = PyObject_TYPE(ob); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments