private void SetItem(int offset, object?value) { if (value == null) { throw PythonOps.TypeError("memoryview: invalid type for format '{0}'", format); } switch (_format) { case "d": // double case "f": // float double convertedValueDouble = 0; if (!Converter.TryConvertToDouble(value, out convertedValueDouble)) { throw PythonOps.TypeError("memoryview: invalid type for format '{0}'", format); } value = convertedValueDouble; break; case "c": // char case "b": // signed byte case "B": // unsigned byte case "u": // unicode char case "h": // signed short case "H": // unsigned short case "i": // signed int case "I": // unsigned int case "l": // signed long case "L": // unsigned long case "q": // signed long long case "P": // pointer case "Q": // unsigned long long if (!PythonOps.IsNumericObject(value)) { throw PythonOps.TypeError("memoryview: invalid type for format '{0}'", format); } if (TypecodeOps.CausesOverflow(value, format)) { throw PythonOps.ValueError("memoryview: invalid value for format '{0}'", format); } if (format == "Q") { value = Converter.ConvertToUInt64(value); } else { value = Converter.ConvertToInt64(value); } break; default: break; // This could be a variety of types, let the UnpackBytes decide } unpackBytes(format, value, _buffer !.AsSpan().Slice(offset, _itemSize)); }
private void SetItem(int offset, object?value) { if (value == null) { throw PythonOps.TypeError("memoryview: invalid type for format '{0}'", _format); } TypecodeOps.DecomposeTypecode(_format, out char byteorder, out char typecode); switch (typecode) { case 'd': // double case 'f': // float if (!Converter.TryConvertToDouble(value, out double convertedValueDouble)) { throw PythonOps.TypeError("memoryview: invalid type for format '{0}'", _format); } value = convertedValueDouble; break; case 'c': // bytechar if (!(value is Bytes b)) { throw PythonOps.TypeError("memoryview: invalid type for format '{0}'", _format); } if (b.Count != 1) { throw PythonOps.ValueError("memoryview: invalid value for format '{0}'", _format); } break; case 'b': // signed byte case 'B': // unsigned byte case 'h': // signed short case 'H': // unsigned short case 'i': // signed int case 'I': // unsigned int case 'l': // signed long case 'L': // unsigned long case 'n': // signed index case 'N': // unsigned index case 'q': // signed long long case 'Q': // unsigned long long if (!PythonOps.IsNumericObject(value)) { throw PythonOps.TypeError("memoryview: invalid type for format '{0}'", _format); } if (TypecodeOps.CausesOverflow(value, typecode)) { throw PythonOps.ValueError("memoryview: invalid value for format '{0}'", _format); } if (typecode == 'Q') { value = Converter.ConvertToUInt64(value); } else { value = Converter.ConvertToInt64(value); } break; case 'P': // void pointer if (!PythonOps.IsNumericObject(value)) { throw PythonOps.TypeError("memoryview: invalid type for format '{0}'", _format); } var bi = Converter.ConvertToBigInteger(value); if (TypecodeOps.CausesOverflow(bi, typecode)) { throw PythonOps.ValueError("memoryview: invalid value for format '{0}'", _format); } value = bi; break; case 'r': // .NET signed pointer case 'R': // .NET unsigned pointer if (value is UIntPtr uptr) { if (typecode == 'r') { value = new IntPtr(unchecked ((Int64)uptr.ToUInt64())); } break; } if (value is IntPtr iptr) { if (typecode == 'R') { value = new UIntPtr(unchecked ((UInt64)iptr.ToInt64())); } break; } throw PythonOps.TypeError("memoryview: invalid type for format '{0}'", _format); default: break; // This could be a variety of types, let the UnpackBytes decide } UnpackBytes(typecode, value, _buffer !.AsSpan().Slice(offset, _itemSize)); }