示例#1
0
        PyString_AsStringAndSize(IntPtr strPtr, IntPtr dataPtrPtr, IntPtr sizePtr)
        {
            try
            {
                if (CPyMarshal.ReadPtrField(strPtr, typeof(PyObject), "ob_type") != this.PyString_Type)
                {
                    throw PythonOps.TypeError("PyString_AsStringAndSize: not a string");
                }

                IntPtr dataPtr = CPyMarshal.GetField(strPtr, typeof(PyStringObject), "ob_sval");
                CPyMarshal.WritePtr(dataPtrPtr, dataPtr);

                int length = CPyMarshal.ReadIntField(strPtr, typeof(PyStringObject), "ob_size");
                if (sizePtr == IntPtr.Zero)
                {
                    for (int i = 0; i < length; ++i)
                    {
                        if (CPyMarshal.ReadByte(CPyMarshal.Offset(dataPtr, i)) == 0)
                        {
                            throw PythonOps.TypeError("PyString_AsStringAndSize: string contains embedded 0s, but sizePtr is null");
                        }
                    }
                }
                else
                {
                    CPyMarshal.WriteInt(sizePtr, length);
                }
                return(0);
            }
            catch (Exception e)
            {
                this.LastException = e;
                return(-1);
            }
        }
示例#2
0
        PyString_FromString(IntPtr stringData)
        {
            IntPtr      current   = stringData;
            List <byte> bytesList = new List <byte>();

            while (CPyMarshal.ReadByte(current) != 0)
            {
                bytesList.Add(CPyMarshal.ReadByte(current));
                current = CPyMarshal.Offset(current, 1);
            }
            byte[] bytes = new byte[bytesList.Count];
            bytesList.CopyTo(bytes);
            // note: NOT Associate
            // couldn't figure out to test this directly
            // without this, h5py tests get horribly screwy in PHIL contextmanager
            return(this.Store(this.StringFromBytes(bytes)));
        }