Example #1
0
        /// <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__");
        }
Example #2
0
 /// <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__");
 }
Example #3
0
        /// <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__");
        }
Example #4
0
 internal static void Reset()
 {
     Global = new PyScopeManager();
 }
Example #5
0
 /// <summary>Create a scope based on a Python Module.</summary>
 internal PyScope(BorrowedReference reference, PyScopeManager manager)
     : this(reference.DangerousGetAddress(), manager)
 {
     Runtime.XIncref(reference.DangerousGetAddress());
 }
Example #6
0
 /// <summary>Create a scope based on a Python Module.</summary>
 internal PyScope(ref NewReference reference, PyScopeManager manager)
     : this(reference.DangerousMoveToPointer(), manager)
 {
 }