Represents a Python tuple object. See the documentation at http://www.python.org/doc/current/api/tupleObjects.html for details.
Inheritance: PySequence
Esempio n. 1
0
        /// <summary>
        /// InvokeMethod Method
        /// </summary>
        /// <remarks>
        /// Invoke the named method of the object with the given arguments.
        /// A PythonException is raised if the invokation is unsuccessful.
        /// </remarks>
        public PyObject InvokeMethod(string name, PyTuple args)
        {
            PyObject method = GetAttr(name);
            PyObject result = method.Invoke(args);

            method.Dispose();
            return(result);
        }
Esempio n. 2
0
        //===================================================================
        // Return the clr python module (new reference)
        //===================================================================
        public static IntPtr GetCLRModule(IntPtr?fromList = null)
        {
            root.InitializePreload();
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35)
            // update the module dictionary with the contents of the root dictionary
            root.LoadNames();
            IntPtr py_mod_dict = Runtime.PyModule_GetDict(py_clr_module);
            IntPtr clr_dict    = Runtime._PyObject_GetDictPtr(root.pyHandle); // PyObject**
            clr_dict = (IntPtr)Marshal.PtrToStructure(clr_dict, typeof(IntPtr));
            Runtime.PyDict_Update(py_mod_dict, clr_dict);

            // find any items from the fromlist and get them from the root if they're not
            // aleady in the module dictionary
            if (fromList != null && fromList != IntPtr.Zero)
            {
                if (Runtime.PyTuple_Check(fromList.GetValueOrDefault()))
                {
                    Runtime.Incref(py_mod_dict);
                    using (PyDict mod_dict = new PyDict(py_mod_dict)) {
                        Runtime.Incref(fromList.GetValueOrDefault());
                        using (PyTuple from = new PyTuple(fromList.GetValueOrDefault())) {
                            foreach (PyObject item in from)
                            {
                                if (mod_dict.HasKey(item))
                                {
                                    continue;
                                }

                                string s = item.AsManagedObject(typeof(string)) as string;
                                if (null == s)
                                {
                                    continue;
                                }

                                ManagedType attr = root.GetAttribute(s, true);
                                if (null == attr)
                                {
                                    continue;
                                }

                                Runtime.Incref(attr.pyHandle);
                                using (PyObject obj = new PyObject(attr.pyHandle)) {
                                    mod_dict.SetItem(s, obj);
                                }
                            }
                        }
                    }
                }
            }

            Runtime.Incref(py_clr_module);
            return(py_clr_module);
#else
            Runtime.Incref(root.pyHandle);
            return(root.pyHandle);
#endif
        }
Esempio n. 3
0
        /// <summary>
        /// Invoke Method
        /// </summary>
        /// <remarks>
        /// Invoke the callable object with the given arguments, passed as a
        /// Python tuple. A PythonException is raised if the invokation fails.
        /// </remarks>
        public PyObject Invoke(PyTuple args)
        {
            IntPtr r = Runtime.PyObject_Call(obj, args.obj, IntPtr.Zero);

            if (r == IntPtr.Zero)
            {
                throw new PythonException();
            }
            return(new PyObject(r));
        }
Esempio n. 4
0
        /// <summary>
        /// Invoke Method
        /// </summary>
        /// <remarks>
        /// Invoke the callable object with the given positional and keyword
        /// arguments. A PythonException is raised if the invokation fails.
        /// </remarks>
        public PyObject Invoke(PyTuple args, PyDict kw)
        {
            IntPtr r = Runtime.PyObject_Call(obj, args.obj, kw != null ? kw.obj : IntPtr.Zero);

            if (r == IntPtr.Zero)
            {
                throw new PythonException();
            }
            return(new PyObject(r));
        }
Esempio n. 5
0
        /// <summary>
        /// Invoke Method
        /// </summary>
        /// <remarks>
        /// Invoke the callable object with the given arguments, passed as a
        /// PyObject[]. A PythonException is raised if the invokation fails.
        /// </remarks>
        public PyObject Invoke(params PyObject[] args)
        {
            var    t = new PyTuple(args);
            IntPtr r = Runtime.PyObject_Call(obj, t.obj, IntPtr.Zero);

            t.Dispose();
            if (r == IntPtr.Zero)
            {
                throw new PythonException();
            }
            return(new PyObject(r));
        }
Esempio n. 6
0
        /// <summary>
        /// Invoke Method
        /// </summary>
        /// <remarks>
        /// Invoke the callable object with the given positional and keyword
        /// arguments. A PythonException is raised if the invokation fails.
        /// </remarks>
        public PyObject Invoke(PyObject[] args, PyDict kw)
        {
            var    t = new PyTuple(args);
            IntPtr r = Runtime.PyObject_Call(obj, t.obj, kw != null ? kw.obj : IntPtr.Zero);

            t.Dispose();
            if (r == IntPtr.Zero)
            {
                throw new PythonException();
            }
            return(new PyObject(r));
        }
Esempio n. 7
0
        /// <summary>
        /// Return the clr python module (new reference)
        /// </summary>
        public static unsafe NewReference GetCLRModule(BorrowedReference fromList = default)
        {
            root.InitializePreload();

            // update the module dictionary with the contents of the root dictionary
            root.LoadNames();
            BorrowedReference py_mod_dict = Runtime.PyModule_GetDict(ClrModuleReference);

            using (var clr_dict = Runtime.PyObject_GenericGetDict(root.ObjectReference))
            {
                Runtime.PyDict_Update(py_mod_dict, clr_dict);
            }

            // find any items from the from list and get them from the root if they're not
            // already in the module dictionary
            if (fromList != null)
            {
                if (Runtime.PyTuple_Check(fromList))
                {
                    using var mod_dict = new PyDict(py_mod_dict);
                    using var from     = new PyTuple(fromList);
                    foreach (PyObject item in from)
                    {
                        if (mod_dict.HasKey(item))
                        {
                            continue;
                        }

                        var s = item.AsManagedObject(typeof(string)) as string;
                        if (s == null)
                        {
                            continue;
                        }

                        ManagedType attr = root.GetAttribute(s, true);
                        if (attr == null)
                        {
                            continue;
                        }

                        Runtime.XIncref(attr.pyHandle);
                        using (var obj = new PyObject(attr.pyHandle))
                        {
                            mod_dict.SetItem(s, obj);
                        }
                    }
                }
            }
            Runtime.XIncref(py_clr_module);
            return(NewReference.DangerousFromPointer(py_clr_module));
        }
Esempio n. 8
0
        //===================================================================
        // Return the clr python module (new reference)
        //===================================================================
        public static IntPtr GetCLRModule(IntPtr? fromList = null)
        {
            root.InitializePreload();
            #if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35)
            // update the module dictionary with the contents of the root dictionary
            root.LoadNames();
            IntPtr py_mod_dict = Runtime.PyModule_GetDict(py_clr_module);
            IntPtr clr_dict = Runtime._PyObject_GetDictPtr(root.pyHandle); // PyObject**
            clr_dict = (IntPtr)Marshal.PtrToStructure(clr_dict, typeof(IntPtr));
            Runtime.PyDict_Update(py_mod_dict, clr_dict);

            // find any items from the fromlist and get them from the root if they're not
            // aleady in the module dictionary
            if (fromList != null && fromList != IntPtr.Zero) {
                if (Runtime.PyTuple_Check(fromList.GetValueOrDefault()))
                {
                    Runtime.XIncref(py_mod_dict);
                    using(PyDict mod_dict = new PyDict(py_mod_dict)) {
                        Runtime.XIncref(fromList.GetValueOrDefault());
                        using (PyTuple from = new PyTuple(fromList.GetValueOrDefault())) {
                            foreach (PyObject item in from) {
                                if (mod_dict.HasKey(item))
                                    continue;

                                string s = item.AsManagedObject(typeof(string)) as string;
                                if (null == s)
                                    continue;

                                ManagedType attr = root.GetAttribute(s, true);
                                if (null == attr)
                                    continue;

                                Runtime.XIncref(attr.pyHandle);
                                using (PyObject obj = new PyObject(attr.pyHandle)) {
                                    mod_dict.SetItem(s, obj);
                                }
                            }
                        }
                    }
                }
            }

            Runtime.XIncref(py_clr_module);
            return py_clr_module;
            #else
            Runtime.XIncref(root.pyHandle);
            return root.pyHandle;
            #endif
        }
Esempio n. 9
0
        /// <summary>
        /// Invoke Method
        /// </summary>
        /// <remarks>
        /// Invoke the callable object with the given positional and keyword
        /// arguments. A PythonException is raised if the invocation fails.
        /// </remarks>
        public PyObject Invoke(PyTuple args, PyDict kw)
        {
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            IntPtr r = Runtime.PyObject_Call(obj, args.obj, kw?.obj ?? IntPtr.Zero);

            if (r == IntPtr.Zero)
            {
                throw new PythonException();
            }
            return(new PyObject(r));
        }
Esempio n. 10
0
        /// <summary>
        /// InvokeMethod Method
        /// </summary>
        /// <remarks>
        /// Invoke the named method of the object with the given arguments
        /// and keyword arguments. Keyword args are passed as a PyDict object.
        /// A PythonException is raised if the invocation is unsuccessful.
        /// </remarks>
        public PyObject InvokeMethod(string name, PyTuple args, PyDict kw)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            PyObject method = GetAttr(name);
            PyObject result = method.Invoke(args, kw);

            method.Dispose();
            return(result);
        }
Esempio n. 11
0
        private void GetArgs(object[] inargs, out PyTuple args, out PyDict kwargs)
        {
            int arg_count;

            for (arg_count = 0; arg_count < inargs.Length && !(inargs[arg_count] is Py.KeywordArguments); ++arg_count)
            {
                ;
            }
            IntPtr argtuple = Runtime.PyTuple_New(arg_count);

            for (int i = 0; i < arg_count; i++)
            {
                IntPtr ptr;
                if (inargs[i] is PyObject)
                {
                    ptr = ((PyObject)inargs[i]).Handle;
                    Runtime.Incref(ptr);
                }
                else
                {
                    ptr = Converter.ToPython(inargs[i], inargs[i].GetType());
                }
                if (Runtime.PyTuple_SetItem(argtuple, i, ptr) < 0)
                {
                    throw new PythonException();
                }
            }
            args   = new PyTuple(argtuple);
            kwargs = null;
            for (int i = arg_count; i < inargs.Length; i++)
            {
                if (!(inargs[i] is Py.KeywordArguments))
                {
                    throw new ArgumentException("Keyword arguments must come after normal arguments.");
                }
                if (kwargs == null)
                {
                    kwargs = (Py.KeywordArguments)inargs[i];
                }
                else
                {
                    kwargs.Update((Py.KeywordArguments)inargs[i]);
                }
            }
        }
Esempio n. 12
0
        /// <summary>
        /// Invoke Method
        /// </summary>
        /// <remarks>
        /// Invoke the callable object with the given positional and keyword
        /// arguments. A PythonException is raised if the invocation fails.
        /// </remarks>
        public PyObject Invoke(PyObject[] args, PyDict kw)
        {
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }
            if (args.Contains(null))
            {
                throw new ArgumentNullException();
            }

            var    t = new PyTuple(args);
            IntPtr r = Runtime.PyObject_Call(obj, t.obj, kw?.obj ?? IntPtr.Zero);

            t.Dispose();
            if (r == IntPtr.Zero)
            {
                throw new PythonException();
            }
            return(new PyObject(r));
        }
Esempio n. 13
0
 /// <summary>
 /// InvokeMethod Method
 /// </summary>
 ///
 /// <remarks>
 /// Invoke the named method of the object with the given arguments 
 /// and keyword arguments. Keyword args are passed as a PyDict object.
 /// A PythonException is raised if the invokation is unsuccessful.
 /// </remarks>
 public PyObject InvokeMethod(string name, PyTuple args, PyDict kw)
 {
     PyObject method = GetAttr(name);
     PyObject result = method.Invoke(args, kw);
     method.Dispose();
     return result;
 }
Esempio n. 14
0
 private void GetArgs(object[] inargs, out PyTuple args, out PyDict kwargs)
 {
     int arg_count;
     for (arg_count = 0; arg_count < inargs.Length && !(inargs[arg_count] is Py.KeywordArguments); ++arg_count);
     IntPtr argtuple = Runtime.PyTuple_New(arg_count);
     for (int i = 0; i < arg_count; i++)
     {
     IntPtr ptr;
     if (inargs[i] is PyObject)
     {
         ptr = ((PyObject)inargs[i]).Handle;
         Runtime.Incref(ptr);
     }
     else
     {
         ptr = Converter.ToPython(inargs[i], inargs[i].GetType());
     }
     if (Runtime.PyTuple_SetItem(argtuple, i, ptr) < 0)
         throw new PythonException();
     }
     args = new PyTuple(argtuple);
     kwargs = null;
     for (int i = arg_count; i < inargs.Length; i++)
     {
     if (!(inargs[i] is Py.KeywordArguments))
         throw new ArgumentException("Keyword arguments must come after normal arguments.");
     if (kwargs == null)
         kwargs = (Py.KeywordArguments)inargs[i];
     else
         kwargs.Update((Py.KeywordArguments)inargs[i]);
     }
 }
Esempio n. 15
0
 /// <summary>
 /// Invoke Method
 /// </summary>
 ///
 /// <remarks>
 /// Invoke the callable object with the given arguments, passed as a
 /// PyObject[]. A PythonException is raised if the invokation fails.
 /// </remarks>
 public PyObject Invoke(params PyObject[] args)
 {
     PyTuple t = new PyTuple(args);
     IntPtr r = Runtime.PyObject_Call(obj, t.obj, IntPtr.Zero);
     t.Dispose();
     if (r == IntPtr.Zero) {
     throw new PythonException();
     }
     return new PyObject(r);
 }
Esempio n. 16
0
 /// <summary>
 /// Invoke Method
 /// </summary>
 ///
 /// <remarks>
 /// Invoke the callable object with the given arguments, passed as a
 /// Python tuple. A PythonException is raised if the invokation fails.
 /// </remarks>
 public PyObject Invoke(PyTuple args)
 {
     IntPtr r = Runtime.PyObject_Call(obj, args.obj, IntPtr.Zero);
     if (r == IntPtr.Zero) {
     throw new PythonException();
     }
     return new PyObject(r);
 }
Esempio n. 17
0
 /// <summary>
 /// Invoke Method
 /// </summary>
 ///
 /// <remarks>
 /// Invoke the callable object with the given positional and keyword
 /// arguments. A PythonException is raised if the invokation fails.
 /// </remarks>
 public PyObject Invoke(PyObject[] args, PyDict kw)
 {
     PyTuple t = new PyTuple(args);
     IntPtr r = Runtime.PyObject_Call(obj, t.obj, kw != null ? kw.obj : IntPtr.Zero);
     t.Dispose();
     if (r == IntPtr.Zero) {
     throw new PythonException();
     }
     return new PyObject(r);
 }
Esempio n. 18
0
 /// <summary>
 /// Invoke Method
 /// </summary>
 ///
 /// <remarks>
 /// Invoke the callable object with the given positional and keyword
 /// arguments. A PythonException is raised if the invokation fails.
 /// </remarks>
 public PyObject Invoke(PyTuple args, PyDict kw)
 {
     IntPtr r = Runtime.PyObject_Call(obj, args.obj, kw != null ? kw.obj : IntPtr.Zero);
     if (r == IntPtr.Zero) {
     throw new PythonException();
     }
     return new PyObject(r);
 }