예제 #1
0
        private object GetSelectedRange()
        {
            IPythonArray arr = _object as IPythonArray;

            if (arr != null)
            {
                return(arr.tostring());
            }

            ByteArray bytearr = _object as ByteArray;

            if (bytearr != null)
            {
                return(new Bytes((IList <byte>)bytearr[GetSlice()]));
            }

            IPythonBufferable pyBuf = _object as IPythonBufferable;

            if (pyBuf != null)
            {
                return(new Bytes(pyBuf.GetBytes(_offset, _size)));
            }

            return(PythonOps.GetIndex(_context, _object, GetSlice()));
        }
예제 #2
0
        private bool InitBufferObject(object o, int offset, int size)
        {
            if (offset < 0)
            {
                throw PythonOps.ValueError("offset must be zero or positive");
            }
            else if (size < -1)
            {
                //  -1 is the way to ask for the default size so we allow -1 as a size
                throw PythonOps.ValueError("size must be zero or positive");
            }

            //  we currently support only buffers, strings and arrays
            //  of primitives, strings, bytes, and bytearray objects.
            int length;

            if (o is PythonBuffer)
            {
                PythonBuffer py = (PythonBuffer)o;
                o      = py._object; // grab the internal object
                length = py._size;
            }
            else if (o is string)
            {
                string strobj = (string)o;
                length = strobj.Length;
            }
            else if (o is Bytes)
            {
                length = ((Bytes)o).Count;
            }
            else if (o is ByteArray)
            {
                length = ((ByteArray)o).Count;
            }
            else if (o is Array || o is IPythonArray)
            {
                Array arr = o as Array;
                if (arr != null)
                {
                    Type t = arr.GetType().GetElementType();
                    if (!t.IsPrimitive && t != typeof(string))
                    {
                        return(false);
                    }
                    length = arr.Length;
                }
                else
                {
                    IPythonArray pa = (IPythonArray)o;
                    length = pa.Count;
                }
            }
            else if (o is IPythonBufferable)
            {
                length  = ((IPythonBufferable)o).Size;
                _object = o;
            }
            else
            {
                return(false);
            }

            // reset the size based on the given buffer's original size
            if (size >= (length - offset) || size == -1)
            {
                _size = length - offset;
            }
            else
            {
                _size = size;
            }

            _object = o;
            _offset = offset;

            return(true);
        }