Exemple #1
0
        public int __cmp__([NotNull] PythonBuffer other)
        {
            if (Object.ReferenceEquals(this, other))
            {
                return(0);
            }

            return(PythonOps.Compare(ToString(), other.ToString()));
        }
Exemple #2
0
        public override bool Equals(object obj)
        {
            PythonBuffer b = obj as PythonBuffer;

            if (b == null)
            {
                return(false);
            }

            return(__cmp__(b) == 0);
        }
Exemple #3
0
 private void WriteBuffer (PythonBuffer b) {
     _bytes.Add ((byte)'s');
     List<byte> newBytes = new List<byte> ();
     for (int i = 0; i < b.Size; i++) {
         if (b[i] is string) {
             string str = b[i] as string;
             byte[] utfBytes = Encoding.UTF8.GetBytes (str);
             if (utfBytes.Length != str.Length) {
                 newBytes.AddRange (utfBytes);
             } else {
                 byte[] strBytes = PythonAsciiEncoding.Instance.GetBytes (str);
                 newBytes.AddRange (strBytes);
             }
         } else {
             newBytes.Add ((byte)b[i]);
         }
     }
     WriteInt32 (newBytes.Count);
     _bytes.AddRange (newBytes);
 }
Exemple #4
0
 public MemoryView(PythonBuffer obj)
 {
     _buffer = new IListOfBytesBufferProtocol(obj);
 }
Exemple #5
0
 public int send(PythonBuffer data, [DefaultParameterValue(0)] int flags) {
     byte[] buffer = data.ToString().MakeByteArray();
     try {
         return _socket.Send(buffer, (SocketFlags)flags);
     } catch (Exception e) {
         throw MakeException(_context, e);
     }
 }
Exemple #6
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);
        }
Exemple #7
0
 public void update(PythonBuffer newData) {
     update(newData.ToString().MakeByteArray());
 }
Exemple #8
0
 internal Sha256Object(PythonBuffer initialBuffer) {
     _bytes = new byte[0];
     update(initialBuffer);
 }
Exemple #9
0
 public new void write(PythonBuffer data) {
     ThrowIfClosed();
     base.write(data);
 }
Exemple #10
0
            private object fetchOneRow(CodeContext context)
            {
                int numcols = Sqlite3.sqlite3_data_count(this.statement.st);
                object[] row = new object[numcols];
                object converter = null;

                for(int i = 0; i < numcols; ++i)
                {
                    object converted = null;

                    if(this.connection.detect_types != 0)
                    {
                        converter = row_cast_map[i];
                    }
                    else
                    {
                        converter = null;
                    }

                    if(converter != null)
                    {
                        byte[] val = Sqlite3.sqlite3_column_blob(this.statement.st, i);
                        if(val == null)
                        {
                            converted = null;
                        }
                        else
                        {
                            string item = Latin1.GetString(val, 0, val.Length);
                            converted = PythonCalls.Call(context, converter, item);
                        }
                    }
                    else
                    {
                        int coltype = Sqlite3.sqlite3_column_type(this.statement.st, i);

                        switch(coltype)
                        {
                            case Sqlite3.SQLITE_NULL:
                                converted = null;
                                break;

                            case Sqlite3.SQLITE_INTEGER:
                                long l = Sqlite3.sqlite3_column_int64(this.statement.st, i);
                                if(l < int.MinValue || l > int.MaxValue)
                                    converted = l;
                                else
                                    converted = (int)l;

                                break;

                            case Sqlite3.SQLITE_FLOAT:
                                converted = Sqlite3.sqlite3_column_double(this.statement.st, i);
                                break;

                            case Sqlite3.SQLITE_TEXT:
                                converted = Sqlite3.sqlite3_column_text(this.statement.st, i);
                                break;

                            case Sqlite3.SQLITE_BLOB:
                            default:
                                byte[] blob = Sqlite3.sqlite3_column_blob(this.statement.st, i);
                                PythonBuffer buffer = new PythonBuffer(context, blob);
                                converted = buffer;
                                break;
                        }
                    }

                    row[i] = converted;
                }

                return new PythonTuple(row);
            }
Exemple #11
0
 public void update(PythonBuffer buffer) {
     update((IList<byte>)buffer);
 }
Exemple #12
0
            private static object[] buildPyParams(CodeContext context, Sqlite3.sqlite3_context ctx, int argc, sqlite3_value[] argv)
            {
                object[] args = new object[argc];

                for(int i = 0; i < argc; ++i)
                {
                    sqlite3_value cur_value = argv[i];
                    object cur_py_value = null;

                    switch(Sqlite3.sqlite3_value_type(cur_value))
                    {
                        case Sqlite3.SQLITE_INTEGER:
                            cur_py_value = (int)Sqlite3.sqlite3_value_int64(cur_value);
                            break;

                        case Sqlite3.SQLITE_FLOAT:
                            cur_py_value = Sqlite3.sqlite3_value_double(cur_value);
                            break;

                        case Sqlite3.SQLITE_TEXT:
                            cur_py_value = Sqlite3.sqlite3_value_text(cur_value);
                            break;

                        case Sqlite3.SQLITE_BLOB:
                            byte[] result = Sqlite3.sqlite3_value_blob(cur_value);
                            PythonBuffer buffer = new PythonBuffer(context, result);
                            cur_py_value = buffer;
                            break;

                        case Sqlite3.SQLITE_NULL:
                        default:
                            cur_py_value = null;
                            break;
                    }

                    args[i] = cur_py_value;
                }

                return args;
            }
Exemple #13
0
 internal sha(PythonBuffer initialBuffer) {
     _bytes = new byte[0];
     update(initialBuffer);
 }
Exemple #14
0
 public void update(PythonBuffer newData) {
     update((IList<byte>)newData);
 }
Exemple #15
0
 public void update(PythonBuffer buffer) {
     update(buffer.ToString().MakeByteArray());
 }
Exemple #16
0
 public static Sha256Object sha256(PythonBuffer data) {
     return new Sha256Object(data);
 }
Exemple #17
0
 public static Sha512Object sha512(PythonBuffer data) {
     return new Sha512Object(data);
 }
Exemple #18
0
 internal MD5Type(PythonBuffer initialBuffer) {
     _bytes = new byte[0];
     update(initialBuffer);
 }
Exemple #19
0
 public static Sha384Object sha384(PythonBuffer data) {
     return new Sha384Object(data);
 }
Exemple #20
0
 public static MD5Type @new(PythonBuffer data) {
     return new MD5Type(data);
 }
Exemple #21
0
 public void sendall(PythonBuffer data, [DefaultParameterValue(0)] int flags) {
     sendallWorker(data.byteCache, flags);
 }
Exemple #22
0
 public void update(PythonBuffer newBytes) {
     update((IList<byte>)newBytes);
 }
Exemple #23
0
 public static sha @new(PythonBuffer data) {
     return new sha(data);
 }
Exemple #24
0
 public PythonTuple recvfrom_into(PythonBuffer buffer, [DefaultParameterValue(0)]int nbytes, [DefaultParameterValue(0)]int flags) {
     if (nbytes < 0) {
         throw PythonOps.ValueError("negative buffersize in recvfrom_into");
     }
     throw PythonOps.TypeError("buffer is read-only");
 }
Exemple #25
0
 public static Sha384Object sha384(PythonBuffer data) {
     return new Sha384Object((IList<byte>)data);
 }
Exemple #26
0
 public void sendall(PythonBuffer data, [DefaultParameterValue(0)] int flags) {
     byte[] buffer = data.ToString().MakeByteArray();
     try {
         int bytesTotal = buffer.Length;
         int bytesRemaining = bytesTotal;
         while (bytesRemaining > 0) {
             bytesRemaining -= _socket.Send(buffer, bytesTotal - bytesRemaining, bytesRemaining, (SocketFlags)flags);
         }
     } catch (Exception e) {
         throw MakeException(_context, e);
     }
 }
        private bool InitBufferObject(object o, int offset, int size)
        {
            //  we currently support only buffers, strings and arrays
            //  of primitives and strings
            if (o == null || (!(isbuffer = o is PythonBuffer) && !(isstring = o is string) && !(isarray = o is Array)))
            {
                return(false);
            }
            if (offset < 0)
            {
                throw Ops.ValueError("offset must be zero or positive");
            }
            //  -1 is the way to ask for the default size so we allow -1 as a size
            if (size < -1)
            {
                throw Ops.ValueError("size must be zero or positive");
            }
            if (isbuffer)
            {
                PythonBuffer py = (PythonBuffer)o;
                o      = py.@object;         // grab the internal object
                offset = py.offset + offset; // reset the offset based on the given buffer's original offset
                // reset the size based on the given buffer's original size
                if (size >= py.size - offset || size == -1)
                {
                    this.size = py.size - offset;
                }
                else
                {
                    this.size = size;
                }
            }
            else if (isstring)
            {
                string strobj = ((string)o);
                if (size >= strobj.Length || size == -1)
                {
                    this.size = strobj.Length;
                }
                else
                {
                    this.size = size;
                }
            }
            else     // has to be an array at this point
            {
                Array arr = (Array)o;
                Type  t   = arr.GetType().GetElementType();
                if (!t.IsPrimitive && t != typeof(string))
                {
                    return(false);
                }
                if (size >= arr.Length || size == -1)
                {
                    this.size = arr.Length;
                }
                else
                {
                    this.size = size;
                }
            }
            this.@object = o;
            this.offset  = offset;

            return(true);
        }