示例#1
0
        /// Get a member of this module by name.
        public PyObj GetMember(string attrName)
        {
            PyObj result;

            unsafe
            {
                var rawDict = CPython.PyModule_GetDict(obj);
                if (rawDict == null)
                {
                    throw new NullReferenceException("Failed to get dict from module.");
                }
                var rawString = CPython.PyUnicode_FromString(attrName);
                if (rawString == null)
                {
                    throw new NullReferenceException("Failed to create PyObject from string.");
                }
                var rawResult = CPython.PyDict_GetItem(rawDict, rawString);
                if (rawResult == null)
                {
                    throw new NullReferenceException("Failed to get item by key.");
                }
                result = new PyObj(rawResult);
            }
            return(result);
        }
示例#2
0
        /// Double float representation of this PyObject.
        public double ToDouble()
        {
            double result;

            unsafe { result = CPython.PyFloat_AsDouble(obj); }
            return(result);
        }
示例#3
0
        /// Long integer representation of this PyObject.
        public long ToLong()
        {
            long result;

            unsafe { result = CPython.PyLong_AsLong(obj); }
            return(result);
        }
示例#4
0
        /// If this Python object is iterable, get the iterator.
        public PyIter GetIter()
        {
            PyIter result;

            unsafe
            {
                var rawResult = CPython.PyObject_GetIter(obj);
                if (rawResult == null)
                {
                    throw new NullReferenceException("Failed iterate over PyObject.");
                }
                result = new PyIter(rawResult);
            }
            return(result);
        }
示例#5
0
        /// If this Python object supports [] indexing, get an item by its key.
        public PyObj GetItem(PyObj key)
        {
            PyObj result;

            unsafe
            {
                var rawResult = CPython.PyObject_GetItem(obj, key.obj);
                if (rawResult == null)
                {
                    throw new NullReferenceException("Failed to get item by key.");
                }
                result = new PyObj(rawResult);
            }
            return(result);
        }
示例#6
0
        /// Call this Python object with the given arguments.
        public PyObj CallObject(PyObj args)
        {
            PyObj result;

            unsafe
            {
                var rawResult = CPython.PyObject_CallObject(obj, args.obj);
                if (rawResult == null)
                {
                    throw new NullReferenceException("Calling PyObject returned null.");
                }
                result = new PyObj(rawResult);
            }
            return(result);
        }
示例#7
0
        /// Create a PyObject from a long integer.
        public static PyObj FromLong(long value)
        {
            PyObj result;

            unsafe
            {
                var rawResult = CPython.PyLong_FromLong(value);
                if (rawResult == null)
                {
                    throw new NullReferenceException("Failed to create PyObject from long.");
                }
                result = new PyObj(rawResult);
            }
            return(result);
        }
示例#8
0
        /// Create a PyObject from a double.
        public static PyObj FromDouble(double value)
        {
            PyObj result;

            unsafe
            {
                var rawResult = CPython.PyFloat_FromDouble(value);
                if (rawResult == null)
                {
                    throw new NullReferenceException("Failed to create PyObject from double.");
                }
                result = new PyObj(rawResult);
            }
            return(result);
        }
示例#9
0
        /// String representation of this PyObject.
        public override string ToString()
        {
            string str;

            unsafe
            {
                var asStr = CPython.PyObject_Str(obj);
                if (asStr == null)
                {
                    throw new NullReferenceException("Failed to convert PyObject to string.");
                }
                str = CPython.PyUnicode_AsWideCharString(asStr, IntPtr.Zero);
            }
            return(str);
        }
示例#10
0
        /// Create a PyObject from a string.
        public static PyStr FromString(string value)
        {
            PyStr result;

            unsafe
            {
                var rawResult = CPython.PyUnicode_FromString(value);
                if (rawResult == null)
                {
                    throw new NullReferenceException("Failed to create PyObject from string.");
                }
                result = new PyStr(rawResult);
            }
            return(result);
        }
示例#11
0
        public PyObj Next()
        {
            PyObj result;

            unsafe
            {
                var rawResult = CPython.PyIter_Next(obj);
                if (rawResult == null)
                {
                    throw new NullReferenceException("PyIter refused to yield an item.");
                }
                result = new PyObj(rawResult);
            }
            return(result);
        }
示例#12
0
        /// Get an attribute from this PyObject by string name.
        public PyObj GetAttrString(string attrName)
        {
            PyObj result;

            unsafe
            {
                var rawResult = CPython.PyObject_GetAttrString(obj, attrName);
                if (rawResult == null)
                {
                    throw new NullReferenceException(
                              string.Format("Failed to get attribute `{0}` from PyObject.", attrName));
                }
                result = new PyObj(rawResult);
            }
            return(result);
        }
示例#13
0
 internal unsafe PyStr(CPython.PyObject* obj)
     : base(obj)
 {
 }
示例#14
0
 /// These should only be constructed by APIs in this assembly.
 unsafe internal PyObj(CPython.PyObject *obj)
 {
     CPython.Py_IncRef(obj);
     this.obj = obj;
 }
示例#15
0
 /// These should only be constructed by APIs in this assembly.
 internal unsafe PyObj(CPython.PyObject* obj)
 {
     CPython.Py_IncRef(obj);
     this.obj = obj;
 }
示例#16
0
 internal unsafe PyModule(CPython.PyObject* obj)
     : base(obj)
 {
 }