GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — pythonnet/src/runtime/Converter.cs at master · pythonnet/pythonnet Expand file tree/ Copy pathConverter.csMore file actions 991 lines (857 loc) · 34.3 KB / Copy pathConverter.csFile metadata and controls991 lines (857 loc) · 34.3 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using System.Runtime.InteropServices; using System.Security; namespace Python.Runtime { /// <summary> /// Performs data conversions between managed types and Python types. /// </summary> [SuppressUnmanagedCodeSecurity] internal class Converter { private Converter() { } private static readonly Type objectType; private static readonly Type stringType; private static readonly Type singleType; private static readonly Type doubleType; private static readonly Type int16Type; private static readonly Type int32Type; private static readonly Type int64Type; private static readonly Type boolType; private static readonly Type typeType; static Converter() { objectType = typeof(Object); stringType = typeof(String); int16Type = typeof(Int16); int32Type = typeof(Int32); int64Type = typeof(Int64); singleType = typeof(Single); doubleType = typeof(Double); boolType = typeof(Boolean); typeType = typeof(Type); } /// <summary> /// Given a builtin Python type, return the corresponding CLR type. /// </summary> internal static Type? GetTypeByAlias(BorrowedReference op) { if (op == Runtime.PyStringType) return stringType; if (op == Runtime.PyUnicodeType) return stringType; if (op == Runtime.PyLongType) return int32Type; if (op == Runtime.PyLongType) return int64Type; if (op == Runtime.PyFloatType) return doubleType; if (op == Runtime.PyBoolType) return boolType; return null; } internal static BorrowedReference GetPythonTypeByAlias(Type op) { if (op == stringType) return Runtime.PyUnicodeType.Reference; if (op == int16Type) return Runtime.PyLongType.Reference; if (op == int32Type) return Runtime.PyLongType.Reference; if (op == int64Type) return Runtime.PyLongType.Reference; if (op == doubleType) return Runtime.PyFloatType.Reference; if (op == singleType) return Runtime.PyFloatType.Reference; if (op == boolType) return Runtime.PyBoolType.Reference; return BorrowedReference.Null; } internal static NewReference ToPython<T>(T value) => ToPython(value, typeof(T)); private static readonly Func<object, bool> IsTransparentProxy = GetIsTransparentProxy(); private static bool Never(object _) => false; private static Func<object, bool> GetIsTransparentProxy() { var remoting = typeof(int).Assembly.GetType("System.Runtime.Remoting.RemotingServices"); if (remoting is null) return Never; var isProxy = remoting.GetMethod("IsTransparentProxy", new[] { typeof(object) }); if (isProxy is null) return Never; return (Func<object, bool>)Delegate.CreateDelegate( typeof(Func<object, bool>), isProxy, throwOnBindFailure: true); } internal static NewReference ToPythonDetectType(object? value) => value is null ? new NewReference(Runtime.PyNone) : ToPython(value, value.GetType()); internal static NewReference ToPython(object? value, Type type) { if (value is PyObject pyObj) { return new NewReference(pyObj); } // Null always converts to None in Python. if (value == null) { return new NewReference(Runtime.PyNone); } if (EncodableByUser(type, value)) { var encoded = PyObjectConversions.TryEncode(value, type); if (encoded != null) { return new NewReference(encoded); } } if (type.IsInterface) { var ifaceObj = (InterfaceObject)ClassManager.GetClassImpl(type); return ifaceObj.TryWrapObject(value); } if (type.IsArray || type.IsEnum) { return CLRObject.GetReference(value, type); } // it the type is a python subclass of a managed type then return the // underlying python object rather than construct a new wrapper object. if (value is IPythonDerivedType pyderived) { if (!IsTransparentProxy(pyderived)) return ClassDerivedObject.ToPython(pyderived); } // ModuleObjects are created in a way that their wrapping them as // a CLRObject fails, the ClassObject has no tpHandle. Return the // pyHandle as is, do not convert. if (value is ModuleObject) { throw new NotImplementedException(); } // hmm - from Python, we almost never care what the declared // type is. we'd rather have the object bound to the actual // implementing class. type = value.GetType(); if (type.IsEnum) { return CLRObject.GetReference(value, type); } TypeCode tc = Type.GetTypeCode(type); switch (tc) { case TypeCode.Object: return CLRObject.GetReference(value, type); case TypeCode.String: return Runtime.PyString_FromString((string)value); case TypeCode.Int32: return Runtime.PyInt_FromInt32((int)value); case TypeCode.Boolean: if ((bool)value) { return new NewReference(Runtime.PyTrue); } return new NewReference(Runtime.PyFalse); case TypeCode.Byte: return Runtime.PyInt_FromInt32((byte)value); case TypeCode.Char: return Runtime.PyUnicode_FromOrdinal((int)((char)value)); case TypeCode.Int16: return Runtime.PyInt_FromInt32((short)value); case TypeCode.Int64: return Runtime.PyLong_FromLongLong((long)value); case TypeCode.Single: return Runtime.PyFloat_FromDouble((float)value); case TypeCode.Double: return Runtime.PyFloat_FromDouble((double)value); case TypeCode.SByte: return Runtime.PyInt_FromInt32((sbyte)value); case TypeCode.UInt16: return Runtime.PyInt_FromInt32((ushort)value); case TypeCode.UInt32: return Runtime.PyLong_FromUnsignedLongLong((uint)value); case TypeCode.UInt64: return Runtime.PyLong_FromUnsignedLongLong((ulong)value); default: return CLRObject.GetReference(value, type); } } static bool EncodableByUser(Type type, object value) { TypeCode typeCode = Type.GetTypeCode(type); return type.IsEnum || typeCode is TypeCode.DateTime or TypeCode.Decimal || typeCode == TypeCode.Object && value.GetType() != typeof(object) && value is not Type; } /// <summary> /// In a few situations, we don't have any advisory type information /// when we want to convert an object to Python. /// </summary> internal static NewReference ToPythonImplicit(object? value) { if (value == null) { return new NewReference(Runtime.PyNone); } return ToPython(value, objectType); } /// <summary> /// Return a managed object for the given Python object, taking funny /// byref types into account. /// </summary> /// <param name="value">A Python object</param> /// <param name="type">The desired managed type</param> /// <param name="result">Receives the managed object</param> /// <param name="setError">If true, call <c>Exceptions.SetError</c> with the reason for failure.</param> /// <returns>True on success</returns> internal static bool ToManaged(BorrowedReference value, Type type, out object? result, bool setError) { if (type.IsByRef) { type = type.GetElementType(); } return Converter.ToManagedValue(value, type, out result, setError); } internal static bool ToManagedValue(BorrowedReference value, Type obType, out object? result, bool setError) { if (obType == typeof(PyObject)) { result = new PyObject(value); return true; } if (obType.IsSubclassOf(typeof(PyObject)) && !obType.IsAbstract && obType.GetConstructor(new[] { typeof(PyObject) }) is { } ctor) { var untyped = new PyObject(value); result = ToPyObjectSubclass(ctor, untyped, setError); return result is not null; } // Common case: if the Python value is a wrapped managed object // instance, just return the wrapped object. result = null; switch (ManagedType.GetManagedObject(value)) { case CLRObject co: object tmp = co.inst; if (obType.IsInstanceOfType(tmp)) { result = tmp; return true; } if (setError) { string typeString = tmp is null ? "null" : tmp.GetType().ToString(); Exceptions.SetError(Exceptions.TypeError, $"{typeString} value cannot be converted to {obType}"); } return false; case ClassBase cb: if (!cb.type.Valid) { Exceptions.SetError(Exceptions.TypeError, cb.type.DeletedMessage); return false; } result = cb.type.Value; return true; case null: break; default: throw new ArgumentException("We should never receive instances of other managed types"); } if (value == Runtime.PyNone && !obType.IsValueType) { result = null; return true; } if (obType.IsGenericType && obType.GetGenericTypeDefinition() == typeof(Nullable<>)) { if (value == Runtime.PyNone) { result = null; return true; } // Set type to underlying type obType = obType.GetGenericArguments()[0]; } if (obType.ContainsGenericParameters) { if (setError) { Exceptions.SetError(Exceptions.TypeError, $"Cannot create an instance of the open generic type {obType}"); } return false; } if (obType.IsArray) { return ToArray(value, obType, out result, setError); } // Conversion to 'Object' is done based on some reasonable default // conversions (Python string -> managed string). if (obType == objectType) { if (Runtime.PyString_CheckExact(value)) { return ToPrimitive(value, stringType, out result, setError); } if (Runtime.PyBool_CheckExact(value)) { return ToPrimitive(value, boolType, out result, setError); } if (Runtime.PyFloat_CheckExact(value)) { return ToPrimitive(value, doubleType, out result, setError); } // 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); return true; } result = new PyObject(value); return true; } // Conversion to 'Type' is done using the same mappings as above for objects. if (obType == typeType) { if (value == Runtime.PyStringType) { result = stringType; return true; } if (value == Runtime.PyBoolType) { result = boolType; return true; } if (value == Runtime.PyLongType) { result = typeof(PyInt); return true; } if (value == Runtime.PyFloatType) { result = doubleType; return true; } if (value == Runtime.PyListType) { result = typeof(PyList); return true; } if (value == Runtime.PyTupleType) { result = typeof(PyTuple); return true; } if (setError) { Exceptions.SetError(Exceptions.TypeError, "value cannot be converted to Type"); } return false; } if (DecodableByUser(obType)) { BorrowedReference pyType = Runtime.PyObject_TYPE(value); if (PyObjectConversions.TryDecode(value, pyType, obType, out result)) { return true; } } if (obType == typeof(System.Numerics.BigInteger) && Runtime.PyInt_Check(value)) { using var pyInt = new PyInt(value); result = pyInt.ToBigInteger(); return true; } return ToPrimitive(value, obType, out result, setError); } /// <remarks> /// Unlike <see cref="ToManaged(BorrowedReference, Type, out object?, bool)"/>, /// this method does not have a <c>setError</c> parameter, because it should /// only be called after <see cref="ToManaged(BorrowedReference, Type, out object?, bool)"/>. /// </remarks> internal static bool ToManagedExplicit(BorrowedReference value, Type obType, out object? result) { result = null; // this method would potentially clean any existing error resulting in information loss Debug.Assert(Runtime.PyErr_Occurred() == null); string? converterName = IsInteger(obType) ? "__int__" : IsFloatingNumber(obType) ? "__float__" : null; if (converterName is null) return false; Debug.Assert(obType.IsPrimitive); using var converter = Runtime.PyObject_GetAttrString(value, converterName); if (converter.IsNull()) { Exceptions.Clear(); return false; } using var explicitlyCoerced = Runtime.PyObject_CallObject(converter.Borrow(), BorrowedReference.Null); if (explicitlyCoerced.IsNull()) { Exceptions.Clear(); return false; } return ToPrimitive(explicitlyCoerced.Borrow(), obType, out result, false); } static object? ToPyObjectSubclass(ConstructorInfo ctor, PyObject instance, bool setError) { try { return ctor.Invoke(new object[] { instance }); } catch (TargetInvocationException ex) { if (setError) { Exceptions.SetError(ex.InnerException); } return null; } catch (SecurityException ex) { if (setError) { Exceptions.SetError(ex); } return null; } } static bool DecodableByUser(Type type) { TypeCode typeCode = Type.GetTypeCode(type); return type.IsEnum || typeCode is TypeCode.Object or TypeCode.Decimal or TypeCode.DateTime; } internal delegate bool TryConvertFromPythonDelegate(BorrowedReference pyObj, out object? result); internal static int ToInt32(BorrowedReference value) { nint num = Runtime.PyLong_AsSignedSize_t(value); if (num == -1 && Exceptions.ErrorOccurred()) { throw PythonException.ThrowLastAsClrException(); } return checked((int)num); } /// <summary> /// Convert a Python value to an instance of a primitive managed type. /// </summary> internal static bool ToPrimitive(BorrowedReference value, Type obType, out object? result, bool setError) { result = null; if (obType.IsEnum) { if (setError) { Exceptions.SetError(Exceptions.TypeError, "since Python.NET 3.0 int can not be converted to Enum implicitly. Use Enum(int_value)"); } return false; } TypeCode tc = Type.GetTypeCode(obType); switch (tc) { case TypeCode.String: string? st = Runtime.GetManagedString(value); if (st == null) { goto type_error; } result = st; return true; case TypeCode.Int32: { // Python3 always use PyLong API nint num = Runtime.PyLong_AsSignedSize_t(value); if (num == -1 && Exceptions.ErrorOccurred()) { goto convert_error; } if (num > Int32.MaxValue || num < Int32.MinValue) { goto overflow; } result = (int)num; return true; } case TypeCode.Boolean: if (value == Runtime.PyTrue) { result = true; return true; } if (value == Runtime.PyFalse) { result = false; return true; } if (setError) { goto type_error; } return false; case TypeCode.Byte: { if (Runtime.PyObject_TypeCheck(value, Runtime.PyBytesType)) { if (Runtime.PyBytes_Size(value) == 1) { IntPtr bytePtr = Runtime.PyBytes_AsString(value); result = (byte)Marshal.ReadByte(bytePtr); return true; } goto type_error; } nint num = Runtime.PyLong_AsSignedSize_t(value); if (num == -1 && Exceptions.ErrorOccurred()) { goto convert_error; } if (num > Byte.MaxValue || num < Byte.MinValue) { goto overflow; } result = (byte)num; return true; } case TypeCode.SByte: { if (Runtime.PyObject_TypeCheck(value, Runtime.PyBytesType)) { if (Runtime.PyBytes_Size(value) == 1) { IntPtr bytePtr = Runtime.PyBytes_AsString(value); result = (sbyte)Marshal.ReadByte(bytePtr); return true; } goto type_error; } nint num = Runtime.PyLong_AsSignedSize_t(value); if (num == -1 && Exceptions.ErrorOccurred()) { goto convert_error; } if (num > SByte.MaxValue || num < SByte.MinValue) { goto overflow; } result = (sbyte)num; return true; } case TypeCode.Char: { if (Runtime.PyObject_TypeCheck(value, Runtime.PyBytesType)) { if (Runtime.PyBytes_Size(value) == 1) { IntPtr bytePtr = Runtime.PyBytes_AsString(value); result = (char)Marshal.ReadByte(bytePtr); return true; } goto type_error; } else if (Runtime.PyObject_TypeCheck(value, Runtime.PyUnicodeType)) { if (Runtime.PyUnicode_GetLength(value) == 1) { int chr = Runtime.PyUnicode_ReadChar(value, 0); result = (Char)chr; return true; } goto type_error; } nint num = Runtime.PyLong_AsSignedSize_t(value); if (num == -1 && Exceptions.ErrorOccurred()) { goto convert_error; } if (num > Char.MaxValue || num < Char.MinValue) { goto overflow; } result = (char)num; return true; } case TypeCode.Int16: { nint num = Runtime.PyLong_AsSignedSize_t(value); if (num == -1 && Exceptions.ErrorOccurred()) { goto convert_error; } if (num > Int16.MaxValue || num < Int16.MinValue) { goto overflow; } result = (short)num; return true; } case TypeCode.Int64: { if (Runtime.Is32Bit) { if (!Runtime.PyInt_Check(value)) { goto type_error; } long? num = Runtime.PyLong_AsLongLong(value); if (num is null) { goto convert_error; } result = num.Value; return true; } else { nint num = Runtime.PyLong_AsSignedSize_t(value); if (num == -1 && Exceptions.ErrorOccurred()) { goto convert_error; } result = (long)num; return true; } } case TypeCode.UInt16: { nint num = Runtime.PyLong_AsSignedSize_t(value); if (num == -1 && Exceptions.ErrorOccurred()) { goto convert_error; } if (num > UInt16.MaxValue || num < UInt16.MinValue) { goto overflow; } result = (ushort)num; return true; } case TypeCode.UInt32: { nuint num = Runtime.PyLong_AsUnsignedSize_t(value); if (num == unchecked((nuint)(-1)) && Exceptions.ErrorOccurred()) { goto convert_error; } if (num > UInt32.MaxValue) { goto overflow; } result = (uint)num; return true; } case TypeCode.UInt64: { ulong? num = Runtime.PyLong_AsUnsignedLongLong(value); if (num is null) { goto convert_error; } result = num.Value; return true; } case TypeCode.Single: { if (!Runtime.PyFloat_Check(value) && !Runtime.PyInt_Check(value)) { goto type_error; } double num = Runtime.PyFloat_AsDouble(value); if (num == -1.0 && Exceptions.ErrorOccurred()) { goto convert_error; } if (num > Single.MaxValue || num < Single.MinValue) { if (!double.IsInfinity(num)) { goto overflow; } } result = (float)num; return true; } case TypeCode.Double: { if (!Runtime.PyFloat_Check(value) && !Runtime.PyInt_Check(value)) { goto type_error; } double num = Runtime.PyFloat_AsDouble(value); if (num == -1.0 && Exceptions.ErrorOccurred()) { goto convert_error; } result = num; return true; } default: goto type_error; } convert_error: if (!setError) { Exceptions.Clear(); } return false; type_error: if (setError) { string tpName = Runtime.PyObject_GetTypeName(value); Exceptions.SetError(Exceptions.TypeError, $"'{tpName}' value cannot be converted to {obType}"); } return false; overflow: // C# level overflow error if (setError) { Exceptions.SetError(Exceptions.OverflowError, "value too large to convert"); } return false; } private static void SetConversionError(BorrowedReference value, Type target) { // PyObject_Repr might clear the error Runtime.PyErr_Fetch(out var causeType, out var causeVal, out var causeTrace); var ob = Runtime.PyObject_Repr(value); string src = "'object has no repr'"; if (ob.IsNull()) { Exceptions.Clear(); } else { src = Runtime.GetManagedString(ob.Borrow()) ?? src; } ob.Dispose(); Runtime.PyErr_Restore(causeType.StealNullable(), causeVal.StealNullable(), causeTrace.StealNullable()); Exceptions.RaiseTypeError($"Cannot convert {src} to {target}"); } /// <summary> /// Convert a Python value to a correctly typed managed array instance. /// The Python value must support the Python iterator protocol or and the /// items in the sequence must be convertible to the target array type. /// </summary> private static bool ToArray(BorrowedReference value, Type obType, out object? result, bool setError) { Type elementType = obType.GetElementType(); result = null; using var IterObject = Runtime.PyObject_GetIter(value); if (IterObject.IsNull()) { if (setError) { SetConversionError(value, obType); } else { // PyObject_GetIter will have set an error Exceptions.Clear(); } return false; } IList list; try { // MakeGenericType can throw because elementType may not be a valid generic argument even though elementType[] is a valid array type. // For example, if elementType is a pointer type. // See https://docs.microsoft.com/en-us/dotnet/api/system.type.makegenerictype#System_Type_MakeGenericType_System_Type var constructedListType = typeof(List<>).MakeGenericType(elementType); bool IsSeqObj = Runtime.PySequence_Check(value); object[] constructorArgs = Array.Empty<object>(); if (IsSeqObj) { var len = Runtime.PySequence_Size(value); if (len >= 0) { if (len <= int.MaxValue) { constructorArgs = new object[] { (int)len }; } } else { // for the sequences, that explicitly deny calling __len__() Exceptions.Clear(); } } // CreateInstance can throw even if MakeGenericType succeeded. // See https://docs.microsoft.com/en-us/dotnet/api/system.activator.createinstance#System_Activator_CreateInstance_System_Type_ list = (IList)Activator.CreateInstance(constructedListType, args: constructorArgs); } catch (Exception e) { if (setError) { Exceptions.SetError(e); SetConversionError(value, obType); } return false; } while (true) { using var item = Runtime.PyIter_Next(IterObject.Borrow()); if (item.IsNull()) break; if (!Converter.ToManaged(item.Borrow(), elementType, out var obj, setError)) { return false; } list.Add(obj); } if (Exceptions.ErrorOccurred()) { if (!setError) Exceptions.Clear(); return false; } Array items = Array.CreateInstance(elementType, list.Count); list.CopyTo(items, 0); result = items; return true; } internal static bool IsFloatingNumber(Type type) => type == typeof(float) || type == typeof(double); internal static bool IsInteger(Type type) => type == typeof(Byte) || type == typeof(SByte) || type == typeof(Int16) || type == typeof(UInt16) || type == typeof(Int32) || type == typeof(UInt32) || type == typeof(Int64) || type == typeof(UInt64); } public static class ConverterExtension { public static PyObject ToPython(this object? o) { if (o is null) return Runtime.None; return Converter.ToPython(o, o.GetType()).MoveToPyObject(); } public static PyObject ToPythonAs<T>(this T? o) { if (o is null) return Runtime.None; return Converter.ToPython(o, typeof(T)).MoveToPyObject(); } } } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |