Exemple #1
0
        public int __hash__(CodeContext context)
        {
            if (_storedHash != null)
            {
                return(_storedHash.Value);
            }

            CheckBuffer();
            if (!_isReadOnly)
            {
                throw PythonOps.ValueError("cannot hash writable memoryview object");
            }

            TypecodeOps.DecomposeTypecode(_format, out _, out char typecode);
            if (!TypecodeOps.IsByteCode(typecode))
            {
                throw PythonOps.ValueError("memoryview: hashing is restricted to formats 'B', 'b' or 'c'");
            }

            _storedHash = tobytes().GetHashCode();
            return(_storedHash.Value);
        }
Exemple #2
0
        public MemoryView cast([NotNull] string format, [NotNull, AllowNull] object shape)
        {
            if (!_isCContig)
            {
                throw PythonOps.TypeError("memoryview: casts are restricted to C-contiguous views");
            }

            if (_shape.Contains(0) || _strides.Contains(0))
            {
                throw PythonOps.TypeError("memoryview: cannot cast view with zeros in shape or strides");
            }

            PythonTuple?shapeAsTuple = null;

            if (shape != null)
            {
                if (!(shape is PythonList) && !(shape is PythonTuple))
                {
                    throw PythonOps.TypeError("shape must be a list or a tuple");
                }

                shapeAsTuple = PythonTuple.Make(shape);
                int newNDim = shapeAsTuple.Count;

                if (newNDim > MaximumDimensions)
                {
                    throw PythonOps.TypeError("memoryview: number of dimensions must not exceed {0}", MaximumDimensions);
                }

                if (_numDims != 1 && newNDim != 1)
                {
                    throw PythonOps.TypeError("memoryview: cast must be 1D -> ND or ND -> 1D");
                }
            }

            if (!TypecodeOps.TryDecomposeTypecode(format, out char byteorder, out char typecode) ||
                !IsSupportedTypecode(typecode) ||
                byteorder != '@'
                )
            {
                throw PythonOps.ValueError(
                          "memoryview: destination format must be a native single character format prefixed with an optional '@'");
            }

            if (!TypecodeOps.IsByteCode(typecode))
            {
                TypecodeOps.DecomposeTypecode(_format, out _, out char thisTypecode);
                if (!TypecodeOps.IsByteCode(thisTypecode))
                {
                    throw PythonOps.TypeError("memoryview: cannot cast between two non-byte formats");
                }
            }

            int newItemsize = TypecodeOps.GetTypecodeWidth(typecode);

            if ((_numItems * _itemSize) % newItemsize != 0)
            {
                throw PythonOps.TypeError("memoryview: length is not a multiple of itemsize");
            }

            int newLength = _numItems * _itemSize / newItemsize;

            int[] newShape;
            if (shapeAsTuple != null)
            {
                newShape = new int[shapeAsTuple.Count];
                int lengthGivenShape = 1;
                for (int i = 0; i < shapeAsTuple.Count; i++)
                {
                    newShape[i]       = Converter.ConvertToInt32(shapeAsTuple[i]);
                    lengthGivenShape *= newShape[i];
                }

                if (lengthGivenShape != newLength)
                {
                    throw PythonOps.TypeError("memoryview: product(shape) * itemsize != buffer size");
                }
            }
            else
            {
                newShape = new int[] { newLength };
            }

            return(new MemoryView(this, format, newItemsize, newShape));
        }