/// <summary>
        /// Returns the Python exception associated with a CLR exception
        /// creating a new Python exception if necessary.
        /// </summary>
        public static object ToPython(Exception clrException)
        {
            if (clrException.Data.Contains(pythonExceptionKey))
            {
                // this is already associated w/ a CLR exception.
                return(clrException.Data[pythonExceptionKey]);
            }

            ThreadAbortException ta = clrException as ThreadAbortException;

            if (ta != null)
            {
                // transform TA w/ our reason into a KeyboardInterrupt exception.
                PythonKeyboardInterrupt reason = ta.ExceptionState as PythonKeyboardInterrupt;
                if (reason != null)
                {
                    return(ToPython(reason));
                }
            }

            object res;
            ICustomExceptionConversion ice = clrException as ICustomExceptionConversion;

            if (ice != null)
            {
                res = ice.ToPythonException();
            }
            else
            {
                // this is an exception raised from CLR space crossing
                // into Python space.  We need to create a new Python
                // exception.
                DynamicType pythonType = GetPythonTypeFromCLR(clrException.GetType());

                // create new instance of Python type and save it (we do this directly
                // as we're calling during low-stack situations and don't want to invoke
                // a python method that would do a stack-check).
                res = new OldInstance((OldClass)pythonType);

                Ops.SetAttr(DefaultContext.Default, res, SymbolTable.ExceptionMessage, clrException.Message);
                if (clrException.Message != null)
                {
                    Ops.SetAttr(DefaultContext.Default, res, SymbolTable.Arguments, Tuple.MakeTuple(clrException.Message));
                }
                else
                {
                    Ops.SetAttr(DefaultContext.Default, res, SymbolTable.Arguments, Tuple.MakeTuple());
                }
                //'filename', 'lineno','offset', 'print_file_and_line', 'text'
            }

            OldInstance exRes = res as OldInstance;

            if (exRes != null)
            {
                AssociateExceptions(clrException, exRes);
            }

            return(res);
        }
Esempio n. 2
0
        public object Call(object[] args, string[] names)
        {
            OldInstance inst = new OldInstance(this);
            object      meth;

            if (Ops.TryGetAttr(inst, SymbolTable.Init, out meth))
            {
                Ops.Call(meth, args, names);
            }
            else
            {
                Debug.Assert(names.Length != 0);
                throw Ops.TypeError("this constructor takes no arguments");
            }
            return(inst);
        }
Esempio n. 3
0
        public object Call(params object[] args)
        {
            OldInstance inst = new OldInstance(this);
            object      value;

            // lookup the slot directly - we don't go through __getattr__
            // until after the instance is created.
            if (TryLookupSlot(SymbolTable.Init, out value))
            {
                Ops.Call(Ops.GetDescriptor(value, inst, null), args);
            }
            else if (args.Length > 0)
            {
                throw Ops.TypeError("this constructor takes no arguments");
            }
            return(inst);
        }
Esempio n. 4
0
        public object CompareTo(object other)
        {
            OldInstance     oiOther = other as OldInstance;
            IRichComparable irc;
            object          res;

            // try __cmp__ first if we're comparing two old classes (even if they're different types)
            if (oiOther != null)
            {
                res = InternalCompare(SymbolTable.Cmp, other);
                if (res != Ops.NotImplemented)
                {
                    return(res);
                }

                res = oiOther.InternalCompare(SymbolTable.Cmp, this);
                if (res != Ops.NotImplemented)
                {
                    return(((int)res) * -1);
                }

                irc = oiOther;
            }
            else
            {
                irc = other as IRichComparable;
            }

            // next try equals, return 0 if we match.
            res = RichEquals(other);
            if (res != Ops.NotImplemented)
            {
                if (Ops.IsTrue(res))
                {
                    return(0);
                }
            }
            else if (irc != null)
            {
                res = irc.RichEquals(this);
                if (res != Ops.NotImplemented && Ops.IsTrue(res))
                {
                    return(0);
                }
            }

            // next try less than
            res = LessThan(other);
            if (res != Ops.NotImplemented)
            {
                if (Ops.IsTrue(res))
                {
                    return(-1);
                }
            }
            else if (irc != null)
            {
                res = irc.GreaterThan(this);
                if (res != Ops.NotImplemented && Ops.IsTrue(res))
                {
                    return(-1);
                }
            }

            // finally try greater than
            res = GreaterThan(other);
            if (res != Ops.NotImplemented)
            {
                if (Ops.IsTrue(res))
                {
                    return(1);
                }
            }
            else if (irc != null)
            {
                res = irc.LessThan(this);
                if (res != Ops.NotImplemented && Ops.IsTrue(res))
                {
                    return(1);
                }
            }

            if (oiOther == null)
            {
                // finally try __cmp__ if our types are different
                res = InternalCompare(SymbolTable.Cmp, other);
                if (res != Ops.NotImplemented)
                {
                    return(res);
                }

                // try the other side...
                res = Ops.GetDynamicType(other).CompareTo(other, this);
                if (res != Ops.NotImplemented)
                {
                    return(((int)res) * -1);
                }
            }

            return(Ops.NotImplemented);
        }
Esempio n. 5
0
        public OldInstanceFinalizer(OldInstance inst)
        {
            Debug.Assert(inst != null);

            instance = inst;
        }
Esempio n. 6
0
        public override ICollection <MemberDoc> GetMembers(object value)
        {
            List <MemberDoc> res = new List <MemberDoc>();

            PythonModule mod = value as PythonModule;

            if (mod != null)
            {
                foreach (var kvp in mod.__dict__)
                {
                    AddMember(res, kvp, false);
                }
                return(res);
            }

            NamespaceTracker ns = value as NamespaceTracker;

            if (ns != null)
            {
                foreach (var v in ns)
                {
                    AddMember(
                        res,
                        new KeyValuePair <object, object>(
                            v.Key,
                            Importer.MemberTrackerToPython(_context.SharedClsContext, v.Value)
                            ),
                        false
                        );
                }
            }
            else
            {
                OldInstance oi = value as OldInstance;
                if (oi != null)
                {
                    foreach (var member in oi.Dictionary)
                    {
                        AddMember(res, member, false);
                    }

                    AddOldClassMembers(res, oi._class);
                }
                else
                {
                    PythonType pt = value as PythonType;
                    if (pt != null)
                    {
                        foreach (PythonType type in pt.ResolutionOrder)
                        {
                            foreach (var member in type.GetMemberDictionary(_context.SharedContext))
                            {
                                AddMember(res, member, true);
                            }
                        }
                    }
                    else
                    {
                        OldClass oc = value as OldClass;
                        if (oc != null)
                        {
                            AddOldClassMembers(res, oc);
                        }
                        else
                        {
                            pt = DynamicHelpers.GetPythonType(value);
                            foreach (var member in pt.GetMemberDictionary(_context.SharedContext))
                            {
                                AddMember(res, member, true);
                            }
                        }
                    }

                    IPythonObject ipo = value as IPythonObject;
                    if (ipo != null && ipo.Dict != null)
                    {
                        foreach (var member in ipo.Dict)
                        {
                            AddMember(res, member, false);
                        }
                    }
                }
            }

            return(res.ToArray());
        }