public static object __new__(CodeContext context, [NotNull] PythonType cls, [NotNull] IBufferProtocol source) { if (cls == TypeCache.Bytes) { if (source.GetType() == typeof(Bytes)) { return(source); } else if (TryInvokeBytesOperator(context, source, out Bytes? res)) { return(res); } else if (Converter.TryConvertToIndex(source, throwOverflowError: true, out int size)) { if (size < 0) { throw PythonOps.ValueError("negative count"); } return(new Bytes(new byte[size])); } else { return(new Bytes(source)); } } else { return(cls.CreateInstance(context, __new__(context, TypeCache.Bytes, source))); } }
public Bytes compress([NotNull] IBufferProtocol data) { using var buffer = data.GetBuffer(); byte[] input = buffer.AsUnsafeArray() ?? buffer.ToArray(); byte[] output = new byte[ZlibModule.DEFAULTALLOC]; long start_total_out = zst.total_out; zst.next_in = input; zst.next_in_index = 0; zst.avail_in = input.Length; zst.next_out = output; zst.next_out_index = 0; zst.avail_out = output.Length; int err = zst.deflate(Z_NO_FLUSH); while (err == Z_OK && zst.avail_out == 0) { int length = output.Length; Array.Resize(ref output, output.Length * 2); zst.next_out = output; zst.avail_out = length; err = zst.deflate(Z_NO_FLUSH); } if (err != Z_OK && err != Z_BUF_ERROR) { throw ZlibModule.zlib_error(this.zst, err, "while compressing"); } return(GetBytes(output, 0, (int)(zst.total_out - start_total_out))); }
public static Bytes a2b_uu(CodeContext /*!*/ context, [NotNone] IBufferProtocol data) { using var buffer = data.GetBufferNoThrow(); if (buffer is null) { throw PythonOps.TypeError($"argument should be bytes, buffer or ASCII string, not '{PythonOps.GetPythonTypeName(data)}'"); } return(a2b_uu_impl(context, buffer.AsReadOnlySpan()));
public void update([NotNone] IBufferProtocol data) { using var buffer = data.GetBuffer(); byte[] bytes = buffer.ToArray(); lock (_hasher) { _hasher.TransformBlock(bytes, 0, bytes.Length, bytes, 0); } }
public static unsafe int send(int handle, [NotNone] IBufferProtocol data) { using var buffer = data.GetBuffer(); var span = buffer.AsReadOnlySpan(); fixed(byte *ptr = &MemoryMarshal.GetReference(span)) return(send(new IntPtr(handle), new IntPtr(ptr), span.Length, 0)); }
internal static IPythonBuffer?GetBufferNoThrow(this IBufferProtocol bufferProtocol, BufferFlags flags = BufferFlags.Simple) { try { return(bufferProtocol.GetBuffer(flags)); } catch (BufferException) { return(null); } }
public MemoryView(IBufferProtocol obj) { _buffer = obj; _step = 1; _format = _buffer.Format; _itemsize = (int)_buffer.ItemSize; _matchesBuffer = true; }
public Bytes compress([NotNull] IBufferProtocol data) { using var buffer = data.GetBuffer(); byte[] bytes = buffer.AsUnsafeArray() ?? buffer.ToArray(); this.bz2Output.Write(bytes, 0, bytes.Length); return(Bytes.Make(this.GetLatestData())); }
public static Bytes decompress([NotNone] IBufferProtocol data, int wbits = MAX_WBITS, int bufsize = DEFAULTALLOC) { using var buffer = data.GetBuffer(); var bytes = Decompress(buffer.AsUnsafeArray() ?? buffer.ToArray(), wbits, bufsize); return(Bytes.Make(bytes)); }
public void __init__(IBufferProtocol initial_bytes = null) { _pos = _length = 0; if (initial_bytes != null) { DoWrite(initial_bytes); _pos = 0; } }
public Bytes decompress([NotNone] IBufferProtocol data, int max_length = 0) { if (max_length < 0) { throw new ArgumentException("max_length must be greater than zero"); } using var buffer = data.GetBuffer(); byte[] input = buffer.AsUnsafeArray() ?? buffer.ToArray(); byte[] output = new byte[max_length > 0 && ZlibModule.DEFAULTALLOC > max_length ? max_length : ZlibModule.DEFAULTALLOC]; long start_total_out = zst.total_out; zst.next_in = input; zst.next_in_index = 0; zst.avail_in = input.Length; zst.next_out = output; zst.next_out_index = 0; zst.avail_out = output.Length; int err = zst.inflate(FlushStrategy.Z_SYNC_FLUSH); while (err == Z_OK && zst.avail_out == 0) { if (max_length > 0 && output.Length >= max_length) { break; } int old_length = output.Length; Array.Resize(ref output, output.Length * 2); zst.next_out = output; zst.avail_out = old_length; err = zst.inflate(FlushStrategy.Z_SYNC_FLUSH); } if (max_length > 0) { unconsumed_tail = GetBytes(zst.next_in, zst.next_in_index, zst.avail_in); } if (err == Z_STREAM_END) { unused_data += GetBytes(zst.next_in, zst.next_in_index, zst.avail_in); eof = true; } else if (err != Z_OK && err != Z_BUF_ERROR) { throw ZlibModule.zlib_error(this.zst, err, "while decompressing"); } return(GetBytes(output, 0, (int)(zst.total_out - start_total_out))); }
private void WriteBufferProtocol(IBufferProtocol b) { using var buffer = b.GetBufferNoThrow(); if (buffer is null) { throw PythonOps.ValueError("unmarshallable object"); } var span = buffer.AsReadOnlySpan(); _bytes.Add((byte)'s'); WriteInt32(span.Length); _bytes.AddRange(span.ToArray()); }
public static Bytes compress([NotNone] IBufferProtocol data, int level = Z_DEFAULT_COMPRESSION) { using var buffer = data.GetBuffer(); byte[] input = buffer.AsUnsafeArray() ?? buffer.ToArray(); byte[] output = new byte[input.Length + input.Length / 1000 + 12 + 1]; ZStream zst = new ZStream(); zst.next_in = input; zst.avail_in = input.Length; zst.next_out = output; zst.avail_out = output.Length; int err = zst.DeflateInit(level); switch (err) { case (Z_OK): break; case (Z_STREAM_ERROR): throw PythonOps.CreateThrowable(error, "Bad compression level"); default: zst.deflateEnd(); zlib_error(zst, err, "while compressing data"); return(null); } err = zst.deflate(FlushStrategy.Z_FINISH); if (err != Z_STREAM_END) { zst.deflateEnd(); throw zlib_error(zst, err, "while compressing data"); } err = zst.deflateEnd(); if (err == Z_OK) { var res = new byte[(int)zst.total_out]; Array.Copy(output, res, res.Length); return(Bytes.Make(res)); } throw zlib_error(zst, err, "while finishing compression"); }
public void __init__(IBufferProtocol initial_bytes = null) { if (Object.ReferenceEquals(_data, null)) { _data = new byte[DEFAULT_BUF_SIZE]; } _pos = _length = 0; if (initial_bytes != null) { DoWrite(initial_bytes); _pos = 0; } }
public BigInteger readinto([NotNull] IBufferProtocol buffer) { using var pythonBuffer = buffer.GetBufferNoThrow(BufferFlags.Writable) ?? throw PythonOps.TypeError("readinto() argument must be read-write bytes-like object, not {0}", PythonTypeOps.GetName(buffer)); _checkClosed(); var span = pythonBuffer.AsSpan(); int len = Math.Min(_length - _pos, span.Length); _data.AsSpan(_pos, len).CopyTo(span); _pos += len; return(len); }
public MemoryView(IBufferProtocol @object) { _buffer = @object; _step = 1; _format = _buffer.Format; _itemsize = (int)_buffer.ItemSize; _matchesBuffer = true; var shape = _buffer.GetShape(_start, _end); if (shape == null) { _shape = null; } _shape = new PythonTuple(shape); }
private int DoWrite(IBufferProtocol bufferProtocol) { using var buffer = bufferProtocol.GetBuffer(); var bytes = buffer.AsReadOnlySpan(); if (bytes.Length == 0) { return(0); } EnsureSizeSetLength(_pos + bytes.Length); bytes.CopyTo(_data.AsSpan(_pos, bytes.Length)); _pos += bytes.Length; return(bytes.Length); }
internal MemoryView(IBufferProtocol @object, int start, int?end, int step, string format, PythonTuple shape) { _buffer = @object; _format = format; _shape = shape; _start = start; _end = end; _step = step; if (!TypecodeOps.TryGetTypecodeWidth(format, out _itemsize)) { _itemsize = (int)_buffer.ItemSize; } _matchesBuffer = _format == _buffer.Format && _start % itemsize == 0; }
public MemoryView([NotNone] MemoryView @object) { _exporter = @object._exporter; _flags = BufferFlags.RecordsRO; _buffer = _exporter.GetBuffer(_flags); _offset = @object._offset; _isReadOnly = @object._isReadOnly; _numDims = @object._numDims; _format = @object._format; _itemSize = @object._itemSize; _shape = @object._shape; _strides = @object._strides; _isCContig = @object._isCContig; _isFContig = @object._isFContig; _numItems = @object._numItems; }
public Bytes decompress([NotNull] IBufferProtocol data) { if (_finished) { throw PythonOps.EofError("End of stream was already found"); } using var buffer = data.GetBuffer(); byte[] bytes = buffer.AsUnsafeArray() ?? buffer.ToArray(); if (!InitializeMemoryStream(bytes)) { AddData(bytes); } List <byte> output = new List <byte>(); if (InitializeBZ2Stream()) { long memoryPosition = this.input.Position; object state = this.bz2Input.DumpState(); try { // this is the same as what Read() does, so it's unlikely to be // any slower. However, using blocks would require fewer state saves, // which would probably be faster. int b; while ((b = this.bz2Input.ReadByte()) != -1) { output.Add((byte)b); memoryPosition = this.input.Position; state = this.bz2Input.DumpState(); } this.lastSuccessfulPosition = this.input.Position; this._finished = true; } catch (IOException) { // rewind the decompressor and the memory buffer to try again when // more data arrives this.input.Position = memoryPosition; this.bz2Input.RestoreState(state); } } return(new Bytes(output)); }
public static void PlaySound(CodeContext /*!*/ context, [NotNone] IBufferProtocol sound, int flags) { if (((flags & SND_ASYNC) == SND_ASYNC) && ((flags & SND_MEMORY) == SND_MEMORY)) { throw PythonOps.RuntimeError("Cannot play asynchronously from memory"); } if ((flags & SND_MEMORY) == 0) { throw PythonOps.TypeError($"'{nameof(sound)}' must be str or None, not '{PythonOps.GetPythonTypeName(sound)}'"); } using var buffer = sound.GetBuffer(); if (!PlaySound(buffer.ToArray(), IntPtr.Zero, flags)) { throw PythonOps.RuntimeError("Failed to play sound"); } }
public MemoryView([NotNull] IBufferProtocol @object) { _buffer = @object; _step = 1; _format = _buffer.Format; _isReadOnly = _buffer.ReadOnly; _itemsize = (int)_buffer.ItemSize; _matchesBuffer = true; var shape = _buffer.GetShape(_start, _end); if (shape != null) { _shape = new PythonTuple(shape); } else { _shape = PythonTuple.MakeTuple(_buffer.ItemCount); } }
public static bool _compare_digest(object a, object b) { if (a is string && b is string) { string aStr = a as string; string bStr = b as string; return(CompareBytes(aStr.MakeByteArray(), bStr.MakeByteArray())); } else if (a is IBufferProtocol && b is IBufferProtocol) { IBufferProtocol aBuf = a as IBufferProtocol; IBufferProtocol bBuf = b as IBufferProtocol; if (aBuf.NumberDimensions > 1 || bBuf.NumberDimensions > 1) { throw PythonOps.BufferError("Buffer must be single dimension"); } return(CompareBytes(aBuf.ToBytes(0, null), bBuf.ToBytes(0, null))); } throw PythonOps.TypeError("unsupported operand types(s) or combination of types: '{0}' and '{1}", PythonOps.GetPythonTypeName(a), PythonOps.GetPythonTypeName(b)); }
public static Bytes byteswap(CodeContext /*!*/ context, [NotNone] IBufferProtocol fragment, int width) { if (width < 1 || width > 4) { throw PythonExceptions.CreateThrowable(error(context), "Size should be 1, 2, 3 or 4"); } using var buffer = fragment.GetBuffer(); if (buffer.NumBytes() % width != 0) { throw PythonExceptions.CreateThrowable(error(context), "not a whole number of frames"); } var array = buffer.ToArray(); if (width == 2) { for (var i = 0; i < array.Length; i += width) { array.ByteSwap(i, i + 1); } } else if (width == 3) { for (var i = 0; i < array.Length; i += width) { array.ByteSwap(i, i + 2); } } else if (width == 4) { for (var i = 0; i < array.Length; i += width) { array.ByteSwap(i, i + 3); array.ByteSwap(i + 1, i + 2); } } return(Bytes.Make(array)); }
public BigInteger readinto([NotNull] IBufferProtocol buffer) { EnsureReadable(); using var pythonBuffer = buffer.GetBufferNoThrow(BufferFlags.Writable) ?? throw PythonOps.TypeError("readinto() argument must be read-write bytes-like object, not {0}", PythonTypeOps.GetName(buffer)); _checkClosed(); var span = pythonBuffer.AsSpan(); for (int i = 0; i < span.Length; i++) { int b = _readStream.ReadByte(); if (b == -1) { return(i); } span[i] = (byte)b; } return(span.Length); }
public bool __ne__(CodeContext /*!*/ context, [NotNull] IBufferProtocol value) => !__eq__(context, value);
public Bytes([NotNull] IBufferProtocol source) { using IPythonBuffer buffer = source.GetBuffer(BufferFlags.FullRO); _bytes = buffer.ToArray(); }
public MemoryView(IBufferProtocol @object) { _buffer = @object; }
private MemoryView(IBufferProtocol @object, int start, int? end) { _buffer =@object; _start = start; _end = end; }
public MemoryView([NotNone] IBufferProtocol @object) { _exporter = @object; // MemoryView should support all possible buffer exports (BufferFlags.FullRO) // but handling of suboffsets (BufferFlags.Indirect) is not implemented yet. // Hence the request is for BufferFlags.RecordsRO _flags = BufferFlags.RecordsRO; _buffer = @object.GetBuffer(_flags); // doublecheck that we don't have to deal with suboffsets if (_buffer.SubOffsets != null) { throw PythonOps.NotImplementedError("memoryview: indirect buffers are not supported"); } ReadOnlySpan <byte> memblock = _buffer.AsReadOnlySpan(); if ((_buffer.ItemCount != 0 && !VerifyStructure(memblock.Length, _buffer.ItemSize, _buffer.NumOfDims, _buffer.Shape, _buffer.Strides, _buffer.Offset)) || (_buffer.Shape == null && (_buffer.Offset != 0 || _buffer.NumBytes() != memblock.Length)) ) { throw PythonOps.BufferError("memoryview: invalid buffer exported from object of type {0}", PythonOps.GetPythonTypeName(@object)); } _offset = _buffer.Offset; _isReadOnly = _buffer.IsReadOnly; _numDims = _buffer.NumOfDims; // in flags we requested format be provided, check that the exporter complied if (_buffer.Format == null) { throw PythonOps.BufferError("memoryview: object of type {0} did not report its format", PythonOps.GetPythonTypeName(@object)); } _format = _buffer.Format; _itemSize = _buffer.ItemSize; // for convenience _shape and _strides are never null, even if _numDims == 0 or _flags indicate no _shape or _strides _shape = _buffer.Shape ?? (_numDims > 0 ? new int[] { _buffer.ItemCount } : Array.Empty <int>()); if (_shape.Count == 0) { _strides = _shape; // TODO: use a static singleton _isCContig = true; } else if (_buffer.Strides != null) { _strides = _buffer.Strides; _isCContig = true; for (int i = _strides.Count - 1, curStride = _itemSize; i >= 0 && _isCContig; i--) { _isCContig &= _strides[i] == curStride; curStride *= _shape[i]; } } else { _strides = GetContiguousStrides(_shape, _itemSize); _isCContig = true; } // invariants _numItems = _buffer.ItemCount; _isFContig = _isCContig && _numDims <= 1; // TODO: support for ND Fortran arrays not implemented // sanity check Debug.Assert(_numItems == 0 || VerifyStructure(memblock.Length, _itemSize, _numDims, _numDims > 0 ? _shape : null, _numDims > 0 ? _strides : null, _offset)); }
internal MemoryView(IBufferProtocol @object, bool readOnly) : this(@object) { _isReadOnly = _isReadOnly || readOnly; }
public void release(CodeContext /*!*/ context) { _buffer = null; }