/// <summary> /// Constructor /// </summary> /// <remarks> /// Create a scope based on a Python Module. /// </remarks> internal PyScope(IntPtr ptr, PyScopeManager manager) { if (!Runtime.Interop.PyType_IsSubtype(Runtime.PyObject_TYPE(ptr), Runtime.PyModuleType)) { throw new PyScopeException("object is not a module"); } Manager = manager ?? PyScopeManager.Global; obj = ptr; //Refcount of the variables not increase variables = Runtime.Interop.PyModule_GetDict(obj); Runtime.CheckExceptionOccurred(); Runtime.Interop.PyDict_SetItemString( variables, "__builtins__", Runtime.Interop.PyEval_GetBuiltins() ); this.Name = this.Get <string>("__name__"); }
/// <summary> /// Constructor /// </summary> /// <remarks> /// Create a scope based on a Python Module. /// </remarks> internal PyScope(IntPtr ptr, PyScopeManager manager) { if (Runtime.PyObject_Type(ptr) != Runtime.PyModuleType) { throw new PyScopeException("object is not a module"); } Manager = manager ?? PyScopeManager.Global; obj = ptr; //Refcount of the variables not increase variables = Runtime.PyModule_GetDict(obj); if (variables == IntPtr.Zero) { throw new PythonException(); } Runtime.PyDict_SetItemString( variables, "__builtins__", Runtime.PyEval_GetBuiltins() ); this.Name = this.Get <string>("__name__"); }
/// <summary> /// Constructor /// </summary> /// <remarks> /// Create a scope based on a Python Module. /// </remarks> internal PyScope(ref NewReference ptr, PyScopeManager manager) { if (!Runtime.PyType_IsSubtype(Runtime.PyObject_TYPE(ptr), Runtime.PyModuleType)) { throw new PyScopeException("object is not a module"); } Manager = manager ?? PyScopeManager.Global; obj = ptr.MoveToPyObject(); //Refcount of the variables not increase variables = Runtime.PyModule_GetDict(Reference).DangerousGetAddress(); PythonException.ThrowIfIsNull(variables); int res = Runtime.PyDict_SetItem( VarsRef, PyIdentifier.__builtins__, Runtime.PyEval_GetBuiltins() ); PythonException.ThrowIfIsNotZero(res); this.Name = this.Get <string>("__name__"); }
internal static void Reset() { Global = new PyScopeManager(); }
/// <summary>Create a scope based on a Python Module.</summary> internal PyScope(BorrowedReference reference, PyScopeManager manager) : this(reference.DangerousGetAddress(), manager) { Runtime.XIncref(reference.DangerousGetAddress()); }
/// <summary>Create a scope based on a Python Module.</summary> internal PyScope(ref NewReference reference, PyScopeManager manager) : this(reference.DangerousMoveToPointer(), manager) { }