SetArgsAndCause() static private method

Set the 'args' slot on a python exception object that wraps a CLR exception. This is needed for pickling CLR exceptions as BaseException_reduce will only check the slots, bypassing the __getattr__ implementation, and thus dereferencing a NULL pointer.
static private SetArgsAndCause ( IntPtr ob ) : void
ob System.IntPtr The python object wrapping
return void
Example #1
0
        internal CLRObject(object ob, IntPtr tp)
        {
            IntPtr py = Runtime.PyType_GenericAlloc(tp, 0);

            long flags = Util.ReadCLong(tp, TypeOffset.tp_flags);

            if ((flags & TypeFlags.Subclass) != 0)
            {
                IntPtr dict = Marshal.ReadIntPtr(py, ObjectOffset.TypeDictOffset(tp));
                if (dict == IntPtr.Zero)
                {
                    dict = Runtime.PyDict_New();
                    Marshal.WriteIntPtr(py, ObjectOffset.TypeDictOffset(tp), dict);
                }
            }

            GCHandle gc = GCHandle.Alloc(this);

            Marshal.WriteIntPtr(py, ObjectOffset.magic(tp), (IntPtr)gc);
            tpHandle = tp;
            pyHandle = py;
            gcHandle = gc;
            inst     = ob;

            // for performance before calling SetArgsAndCause() lets check if we are an exception
            if (inst is Exception)
            {
                // Fix the BaseException args (and __cause__ in case of Python 3)
                // slot if wrapping a CLR exception
                Exceptions.SetArgsAndCause(py);
            }
        }
Example #2
0
        internal CLRObject(Object ob, IntPtr tp) : base()
        {
            IntPtr py = Runtime.PyType_GenericAlloc(tp, 0);

            int flags = (int)Marshal.ReadIntPtr(tp, TypeOffset.tp_flags);

            if ((flags & TypeFlags.Subclass) != 0)
            {
                IntPtr dict = Marshal.ReadIntPtr(py, ObjectOffset.DictOffset(tp));
                if (dict == IntPtr.Zero)
                {
                    dict = Runtime.PyDict_New();
                    Marshal.WriteIntPtr(py, ObjectOffset.DictOffset(tp), dict);
                }
            }

            GCHandle gc = GCHandle.Alloc(this);

            Marshal.WriteIntPtr(py, ObjectOffset.magic(tp), (IntPtr)gc);
            this.tpHandle = tp;
            this.pyHandle = py;
            this.gcHandle = gc;
            inst          = ob;

            // Fix the BaseException args (and __cause__ in case of Python 3)
            // slot if wrapping a CLR exception
            Exceptions.SetArgsAndCause(py);
        }
Example #3
0
        internal CLRObject(object ob, IntPtr tp)
        {
            System.Diagnostics.Debug.Assert(tp != IntPtr.Zero);
            IntPtr py = Runtime.PyType_GenericAlloc(tp, 0);

            long flags = Util.ReadCLong(tp, TypeOffset.tp_flags);

            if ((flags & TypeFlags.Subclass) != 0)
            {
                IntPtr dict = Marshal.ReadIntPtr(py, ObjectOffset.TypeDictOffset(tp));
                if (dict == IntPtr.Zero)
                {
                    dict = Runtime.PyDict_New();
                    Marshal.WriteIntPtr(py, ObjectOffset.TypeDictOffset(tp), dict);
                }
            }

            GCHandle gc = AllocGCHandle(TrackTypes.Wrapper);

            Marshal.WriteIntPtr(py, ObjectOffset.magic(tp), (IntPtr)gc);
            tpHandle = tp;
            pyHandle = py;
            inst     = ob;

            // Fix the BaseException args (and __cause__ in case of Python 3)
            // slot if wrapping a CLR exception
            Exceptions.SetArgsAndCause(py);
        }
Example #4
0
        internal CLRObject(object ob, IntPtr tp)
        {
            System.Diagnostics.Debug.Assert(tp != IntPtr.Zero);
            IntPtr py = Runtime.PyType_GenericAlloc(tp, 0);

            tpHandle = tp;
            pyHandle = py;
            inst     = ob;

            GCHandle gc = AllocGCHandle(TrackTypes.Wrapper);

            InitGCHandle(ObjectReference, type: TypeReference, gc);

            // Fix the BaseException args (and __cause__ in case of Python 3)
            // slot if wrapping a CLR exception
            if (ob is Exception e)
            {
                Exceptions.SetArgsAndCause(e, py);
            }
        }