Example #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);
        }
Example #2
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;
 }
Example #3
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;
 }
Example #4
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;
 }
Example #5
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;
 }
Example #6
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;
 }
Example #7
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;
 }
Example #8
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);
        }
Example #9
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);
        }
Example #10
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);
        }
Example #11
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);
        }
Example #12
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;
 }
Example #13
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);
        }
Example #14
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);
        }