PyObject_SetAttr() private method

private PyObject_SetAttr ( IntPtr pointer, IntPtr name, IntPtr value ) : int
pointer IntPtr
name IntPtr
value IntPtr
return int
Beispiel #1
0
        /// <summary>
        /// SetAttr Method
        /// </summary>
        /// <remarks>
        /// Set an attribute of the object with the given name and value,
        /// where the name is a Python string or unicode object. This method
        /// throws a PythonException if the attribute set fails.
        /// </remarks>
        public void SetAttr(PyObject name, PyObject value)
        {
            int r = Runtime.PyObject_SetAttr(obj, name.obj, value.obj);

            if (r < 0)
            {
                throw new PythonException();
            }
        }
Beispiel #2
0
        /// <summary>
        /// DelAttr Method
        /// </summary>
        /// <remarks>
        /// Delete the named attribute of the Python object, where name is a
        /// PyObject wrapping a Python string or unicode object. This method
        /// throws a PythonException if the attribute set fails.
        /// </remarks>
        public void DelAttr(PyObject name)
        {
            int r = Runtime.PyObject_SetAttr(obj, name.obj, IntPtr.Zero);

            if (r < 0)
            {
                throw new PythonException();
            }
        }
Beispiel #3
0
        /// <summary>
        /// DelAttr Method
        /// </summary>
        /// <remarks>
        /// Delete the named attribute of the Python object, where name is a
        /// PyObject wrapping a Python string or unicode object. This method
        /// throws a PythonException if the attribute set fails.
        /// </remarks>
        public void DelAttr(PyObject name)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            int r = Runtime.PyObject_SetAttr(obj, name.obj, IntPtr.Zero);

            if (r < 0)
            {
                throw new PythonException();
            }
        }
Beispiel #4
0
        /// <summary>
        /// Restore the __import__ hook.
        /// </summary>
        static void RestoreImport()
        {
            IntPtr builtins = Runtime.GetBuiltins();

            int res = Runtime.PyObject_SetAttr(builtins, PyIdentifier.__import__, py_import);

            PythonException.ThrowIfIsNotZero(res);
            Runtime.XDecref(py_import);
            py_import = IntPtr.Zero;

            hook.Release();
            hook = null;

            Runtime.XDecref(builtins);
        }
Beispiel #5
0
        /// <summary>
        /// Initialize just the __import__ hook itself.
        /// </summary>
        static void InitImport()
        {
            // We replace the built-in Python __import__ with our own: first
            // look in CLR modules, then if we don't find any call the default
            // Python __import__.
            IntPtr builtins = Runtime.GetBuiltins();

            py_import = Runtime.PyObject_GetAttr(builtins, PyIdentifier.__import__);
            PythonException.ThrowIfIsNull(py_import);

            hook = new MethodWrapper(typeof(ImportHook), "__import__", "TernaryFunc");
            int res = Runtime.PyObject_SetAttr(builtins, PyIdentifier.__import__, hook.ptr);

            PythonException.ThrowIfIsNotZero(res);

            Runtime.XDecref(builtins);
        }
Beispiel #6
0
        /// <summary>
        /// SetAttr Method
        /// </summary>
        /// <remarks>
        /// Set an attribute of the object with the given name and value,
        /// where the name is a Python string or unicode object. This method
        /// throws a PythonException if the attribute set fails.
        /// </remarks>
        public void SetAttr(PyObject name, PyObject value)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            int r = Runtime.PyObject_SetAttr(obj, name.obj, value.obj);

            if (r < 0)
            {
                throw new PythonException();
            }
        }
Beispiel #7
0
        /// <summary>
        /// Restore the __import__ hook.
        /// </summary>
        static void RestoreImport()
        {
            IntPtr builtins = Runtime.GetBuiltins();

            IntPtr existing = Runtime.PyObject_GetAttr(builtins, PyIdentifier.__import__);

            Runtime.XDecref(existing);
            if (existing != hook.ptr)
            {
                throw new NotSupportedException("Unable to restore original __import__.");
            }

            int res = Runtime.PyObject_SetAttr(builtins, PyIdentifier.__import__, py_import);

            PythonException.ThrowIfIsNotZero(res);
            Runtime.XDecref(py_import);
            py_import = IntPtr.Zero;

            hook.Release();
            hook = null;

            Runtime.XDecref(builtins);
        }