AcquireLock() public static method

AcquireLock Method
Acquire the Python global interpreter lock (GIL). Managed code *must* call this method before using any objects or calling any methods on objects in the Python.Runtime namespace. The only exception is PythonEngine.Initialize, which may be called without first calling AcquireLock. Each call to AcquireLock must be matched by a corresponding call to ReleaseLock, passing the token obtained from AcquireLock. For more information, see the "Extending and Embedding" section of the Python documentation on www.python.org.
public static AcquireLock ( ) : IntPtr
return System.IntPtr
Exemplo n.º 1
0
 /// <summary>
 /// Dispose Method
 /// </summary>
 /// <remarks>
 /// The Dispose method provides a way to explicitly release the
 /// Python object represented by a PyObject instance. It is a good
 /// idea to call Dispose on PyObjects that wrap resources that are
 /// limited or need strict lifetime control. Otherwise, references
 /// to Python objects will not be released until a managed garbage
 /// collection occurs.
 /// </remarks>
 protected virtual void Dispose(bool disposing)
 {
     if (!disposed)
     {
         if (Runtime.Py_IsInitialized() > 0 && !Runtime.IsFinalizing)
         {
             IntPtr gs = PythonEngine.AcquireLock();
             Runtime.XDecref(obj);
             obj = IntPtr.Zero;
             PythonEngine.ReleaseLock(gs);
         }
         disposed = true;
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Dispose Method
        /// </summary>
        ///
        /// <remarks>
        /// The Dispose method provides a way to explicitly release the
        /// Python object represented by a PyObject instance. It is a good
        /// idea to call Dispose on PyObjects that wrap resources that are
        /// limited or need strict lifetime control. Otherwise, references
        /// to Python objects will not be released until a managed garbage
        /// collection occurs.
        /// </remarks>

        public void Dispose()
        {
            if (!disposed)
            {
                if (Runtime.Py_IsInitialized() > 0)
                {
                    IntPtr gs = PythonEngine.AcquireLock();
                    Runtime.Decref(obj);
                    obj = IntPtr.Zero;
                    PythonEngine.ReleaseLock(gs);
                }
                GC.SuppressFinalize(this);
                disposed = true;
            }
        }
        public object Dispatch(ArrayList args)
        {
            IntPtr gs = PythonEngine.AcquireLock();
            object ob = null;

            try {
                ob = TrueDispatch(args);
            }
            catch (Exception e) {
                PythonEngine.ReleaseLock(gs);
                throw e;
            }

            PythonEngine.ReleaseLock(gs);
            return(ob);
        }
Exemplo n.º 4
0
        public object Dispatch(object[] args)
        {
            IntPtr gs = PythonEngine.AcquireLock();
            object ob;

            try
            {
                ob = TrueDispatch(args);
            }
            finally
            {
                PythonEngine.ReleaseLock(gs);
            }

            return(ob);
        }
Exemplo n.º 5
0
        public PythonException()
        {
            IntPtr gs = PythonEngine.AcquireLock();

            Runtime.PyErr_Fetch(out _pyType, out _pyValue, out _pyTB);
            if (_pyType != IntPtr.Zero && _pyValue != IntPtr.Zero)
            {
                string type;
                string message;
                Runtime.XIncref(_pyType);
                using (var pyType = new PyObject(_pyType))
                    using (PyObject pyTypeName = pyType.GetAttr("__name__"))
                    {
                        type = pyTypeName.ToString();
                    }

                _pythonTypeName = type;

                // TODO: If pyValue has a __cause__ attribute, then we could set this.InnerException to the equivalent managed exception.
                Runtime.XIncref(_pyValue);
                using (var pyValue = new PyObject(_pyValue))
                {
                    message = pyValue.ToString();
                }
                _message = type + " : " + message;
            }

            if (_pyTB != IntPtr.Zero)
            {
                using var tb_module = PyModule.Import("traceback");

                Runtime.XIncref(_pyTB);
                using var pyTB = new PyObject(_pyTB);

                using var tbList = tb_module.InvokeMethod("format_tb", pyTB);

                var sb = new StringBuilder();
                // Reverse Python's traceback list to match the order used in C#
                // stacktraces
                foreach (var line in tbList.Reverse())
                {
                    sb.Append(line.ToString());
                }
                _tb = sb.ToString();
            }
            PythonEngine.ReleaseLock(gs);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Replaces PyValue with an instance of PyType, if PyValue is not already an instance of PyType.
        /// Often PyValue is a string and this method will replace it with a proper exception object.
        /// Must not be called when an error is set.
        /// </summary>
        public void Normalize()
        {
            IntPtr gs = PythonEngine.AcquireLock();

            try
            {
                if (Exceptions.ErrorOccurred())
                {
                    throw new InvalidOperationException("Cannot normalize when an error is set");
                }
                // If an error is set and this PythonException is unnormalized, the error will be cleared and the PythonException will be replaced by a different error.
                Runtime.PyErr_NormalizeException(ref _pyType, ref _pyValue, ref _pyTB);
            }
            finally
            {
                PythonEngine.ReleaseLock(gs);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Formats this PythonException object into a message as would be printed
        /// out via the Python console. See traceback.format_exception
        /// </summary>
        public string Format()
        {
            string res;
            IntPtr gs = PythonEngine.AcquireLock();

            try
            {
                if (_pyTB != IntPtr.Zero && _pyType != IntPtr.Zero && _pyValue != IntPtr.Zero)
                {
                    IntPtr tb    = _pyTB;
                    IntPtr type  = _pyType;
                    IntPtr value = _pyValue;

                    Runtime.XIncref(type);
                    Runtime.XIncref(value);
                    Runtime.XIncref(tb);
                    Runtime.PyErr_NormalizeException(ref type, ref value, ref tb);

                    using (PyObject pyType = new PyObject(type))
                        using (PyObject pyValue = new PyObject(value))
                            using (PyObject pyTB = new PyObject(tb))
                                using (PyObject tb_mod = PyModule.Import("traceback"))
                                {
                                    var buffer = new StringBuilder();
                                    var values = tb_mod.InvokeMethod("format_exception", pyType, pyValue, pyTB);
                                    foreach (PyObject val in values)
                                    {
                                        buffer.Append(val.ToString());
                                    }
                                    res = buffer.ToString();
                                }
                }
                else
                {
                    res = StackTrace;
                }
            }
            finally
            {
                PythonEngine.ReleaseLock(gs);
            }
            return(res);
        }
Exemplo n.º 8
0
 /// <summary>
 /// Dispose Method
 /// </summary>
 ///
 /// <remarks>
 /// The Dispose method provides a way to explicitly release the
 /// Python objects represented by a PythonException.
 /// </remarks>
 public void Dispose()
 {
     if (!disposed)
     {
         if (Runtime.Py_IsInitialized() > 0)
         {
             IntPtr gs = PythonEngine.AcquireLock();
             Runtime.Decref(_pyType);
             Runtime.Decref(_pyValue);
             // XXX Do we ever get TraceBack? //
             if (_pyTB != IntPtr.Zero)
             {
                 Runtime.Decref(_pyTB);
             }
             PythonEngine.ReleaseLock(gs);
         }
         GC.SuppressFinalize(this);
         disposed = true;
     }
 }
Exemplo n.º 9
0
        public PythonException() : base()
        {
            Runtime.PyErr_Fetch(ref _pyType, ref _pyValue, ref _pyTB);
            Runtime.Incref(_pyType);
            Runtime.Incref(_pyValue);
            Runtime.Incref(_pyTB);
            IntPtr gs = PythonEngine.AcquireLock();

            if ((_pyType != IntPtr.Zero) && (_pyValue != IntPtr.Zero))
            {
                string type    = new PyObject(_pyType).GetAttr("__name__").ToString();
                string message = Runtime.GetManagedString(_pyValue);
                _message = type + " : " + message;
            }
            if (_pyTB != IntPtr.Zero)
            {
                PyObject tb_module = PythonEngine.ImportModule("traceback");
                _tb = tb_module.InvokeMethod("format_tb", new PyObject(_pyTB)).ToString();
            }
            PythonEngine.ReleaseLock(gs);
        }
Exemplo n.º 10
0
        public PythonException()
        {
            IntPtr gs = PythonEngine.AcquireLock();

            Runtime.Interop.PyErr_Fetch(ref _pyType, ref _pyValue, ref _pyTB);
            Runtime.XIncref(_pyType);
            Runtime.XIncref(_pyValue);
            Runtime.XIncref(_pyTB);
            if (_pyType != IntPtr.Zero && _pyValue != IntPtr.Zero)
            {
                string type;
                string message;
                Runtime.XIncref(_pyType);
                using (var pyType = new PyObject(_pyType))
                    using (PyObject pyTypeName = pyType.GetAttr("__name__"))
                    {
                        type = pyTypeName.ToString();
                    }

                _pythonTypeName = type;

                Runtime.XIncref(_pyValue);
                using (var pyValue = new PyObject(_pyValue))
                {
                    message = pyValue.ToString();
                }
                _message = type + " : " + message;
            }
            if (_pyTB != IntPtr.Zero)
            {
                PyObject tb_module = PythonEngine.ImportModule("traceback");
                Runtime.XIncref(_pyTB);
                using (var pyTB = new PyObject(_pyTB))
                {
                    _tb = tb_module.InvokeMethod("format_tb", pyTB).ToString();
                }
            }
            PythonEngine.ReleaseLock(gs);
        }
Exemplo n.º 11
0
 internal GILState()
 {
     state = PythonEngine.AcquireLock();
 }