C# Reference¶
Warning
doxygenclass: Cannot find class “Py” in doxygen xml output for project “pythonnet” from directory: ../doxygen_xml
- class Python.Runtime.PythonEngine : IDisposable¶
This class provides the public interface of the Python runtime.
Public Functions
- delegate void ShutdownHandler ()¶
Called when the engine is shut down.
Shutdown handlers are run in reverse order they were added, so that resources available when running a shutdown handler are the same as what was available when it was added.
Properties
- bool DebugGIL { get; set; } = false¶
Set to
trueto enable GIL debugging assistance.
Public Static Functions
- void SetNoSiteFlag ()¶
Set the NoSiteFlag to disable loading the site module. Must be called before Initialize. https://docs.python.org/3/c-api/init.html#c.Py_NoSiteFlag.
- void Initialize (IEnumerable<string> args, bool setSysArgv = true, bool initSigs = false)¶
Initialize Method.
Initialize the Python runtime. It is safe to call this method more than once, though initialization will only happen on the first call. It is not necessary to hold the Python global interpreter lock (GIL) to call this method. initSigs can be set to 1 to do default python signal configuration. This will override the way signals are handled by the application.
- IntPtr InitExt ()¶
A helper to perform initialization from the context of an active CPython interpreter process - this bootstraps the managed runtime when it is imported by the CLR extension module.
- void Shutdown ()¶
Shutdown and release resources held by the Python runtime. The Python runtime can no longer be used in the current process after calling the Shutdown method.
- void AddShutdownHandler (ShutdownHandler handler)¶
Add a function to be called when the engine is shut down.
Shutdown handlers are executed in the opposite order they were added, so that you can be sure that everything that was initialized when you added the handler is still initialized when you need to shut down.
If the same shutdown handler is added several times, it will be run several times.
Don’t add shutdown handlers while running a shutdown handler.
- void RemoveShutdownHandler (ShutdownHandler handler)¶
Remove a shutdown handler.
If the same shutdown handler is added several times, only the last one is removed.
Don’t remove shutdown handlers while running a shutdown handler.
- unsafe IntPtr BeginAllowThreads ()¶
BeginAllowThreads Method.
Release the Python global interpreter lock to allow other threads to run. This is equivalent to the Py_BEGIN_ALLOW_THREADS macro provided by the C Python API. For more information, see the “Extending and Embedding” section of the Python documentation on www.python.org.
- unsafe void EndAllowThreads (IntPtr ts)¶
EndAllowThreads Method.
Re-aquire the Python global interpreter lock for the current thread. This is equivalent to the Py_END_ALLOW_THREADS macro provided by the C Python API. For more information, see the “Extending and Embedding” section of the Python documentation on www.python.org.
- PyObject Eval (string code, PyDict? globals = null, PyObject? locals = null)¶
Eval Method.
Evaluate a Python expression and returns the result. It’s a subset of Python eval function.
- void Exec (string code, PyDict? globals = null, PyObject? locals = null)¶
Exec Method.
Run a string containing Python code. It’s a subset of Python exec function.
- ulong GetPythonThreadID ()¶
Gets the Python thread ID.
- Return:
The Python thread ID.
- int Interrupt (ulong pythonThreadID)¶
Interrupts the execution of a thread.
- Param pythonThreadID:
The Python thread ID.
- Return:
The number of thread states modified; this is normally one, but will be zero if the thread id is not found.
- class Python.Runtime.PyObject : DynamicObject, IDisposable, ISerializable, IConvertible¶
Represents a generic Python object. The methods of this class are generally equivalent to the Python “abstract object API”. See PY2: https://docs.python.org/2/c-api/object.html PY3: https://docs.python.org/3/c-api/object.html for details.
Subclassed by Python.Runtime.NullOnly, Python.Runtime.PyIter, Python.Runtime.PyIterable, Python.Runtime.PyModule, Python.Runtime.PyNumber, Python.Runtime.PyType
Public Functions
- PyObject NewReference ()¶
Create a new PyObject instance of this object, bumping the reference count.
- object? AsManagedObject (Type t)¶
AsManagedObject Method.
Return a managed object of the given type, based on the value of the Python object.
- T As<T> ()¶
Return a managed object of the given type, based on the value of the Python object.
- void Dispose ()¶
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.
- PyType GetPythonType ()¶
GetPythonType Method.
Returns the Python type of the object. This method is equivalent to the Python expression: type(object).
- bool TypeCheck (PyType typeOrClass)¶
TypeCheck Method.
Returns true if the object o is of type typeOrClass or a subtype of typeOrClass.
- bool HasAttr (string name)¶
HasAttr Method.
Returns true if the object has an attribute with the given name.
- bool HasAttr (PyObject name)
HasAttr Method.
Returns true if the object has an attribute with the given name, where name is a PyObject wrapping a string or unicode object.