Esempio n. 1
0
        public object ToPythonException()
        {
            DynamicType exType = ExceptionConverter.GetPythonExceptionByName(PythonExceptionName);
            object      inst   = Ops.Call(exType);

            Ops.SetAttr(DefaultContext.Default, inst, SymbolTable.ExceptionMessage, base.Message);

            Ops.SetAttr(DefaultContext.Default, inst, SymbolTable.Arguments, Tuple.MakeTuple(
                            base.Message,
                            Tuple.MakeTuple(
                                file,
                                lineNo,
                                columnNo == 0 ? null : (object)columnNo,
                                lineText
                                )
                            ));

            Ops.SetAttr(DefaultContext.Default, inst, SymbolTable.ExceptionFilename, file);
            Ops.SetAttr(DefaultContext.Default, inst, SymbolTable.ExceptionLineNumber, lineNo);
            if (columnNo != 0)
            {
                Ops.SetAttr(DefaultContext.Default, inst, SymbolTable.ExceptionOffset, columnNo);
            }
            else
            {
                Ops.SetAttr(DefaultContext.Default, inst, SymbolTable.ExceptionOffset, null);
            }

            Ops.SetAttr(DefaultContext.Default, inst, SymbolTable.Text, lineText);
            // print_file_and_line
            return(inst);
        }
Esempio n. 2
0
        public int GetExitCode(ICallerContext context)
        {
            object pyObj = ExceptionConverter.ToPython(this);

            object args;

            if (!Ops.TryGetAttr(pyObj, SymbolTable.Arguments, out args))
            {
                return(0);
            }
            Tuple t = args as Tuple;

            if (t == null || t.Count == 0)
            {
                return(0);
            }

            try {
                return(Converter.ConvertToInt32(t[0]));
            } catch {
            }

            try {
                Ops.PrintWithDest(context.SystemState, context.SystemState.stderr, t[0]);
            } catch {
            }

            return(1);
        }
Esempio n. 3
0
 public object Call(params object[] args)
 {
     try {
         object ret = obj.GetType().InvokeMember(
             name,
             System.Reflection.BindingFlags.InvokeMethod,
             Type.DefaultBinder,
             obj,
             args
             );
         return(Ops.ToPython(ret));
     } catch (Exception e) {
         if (e.InnerException != null)
         {
             throw ExceptionConverter.UpdateForRethrow(e.InnerException);
         }
         throw;
     }
 }
        /// <summary>
        /// Creates a new throwable exception of type type.
        /// </summary>
        public static Exception CreateThrowable(object type, object value)
        {
            object pyEx;

            if (Builtin.IsInstance(value, type))
            {
                pyEx = value;
            }
            else if (value is Tuple)
            {
                pyEx = Ops.CallWithArgsTuple(type, new object[0], value);
            }
            else
            {
                pyEx = Ops.Call(type, value);
            }

            return(ExceptionConverter.ToClr(pyEx));
        }
Esempio n. 5
0
        public Tuple exc_info()
        {
            if (RawException == null)
            {
                return(Tuple.MakeTuple(null, null, null));
            }
            object pyExcep = ExceptionConverter.ToPython(RawException);

            if (Options.TracebackSupport && RawTraceBack != null)
            {
                RawTraceBack.UpdateFromStackTrace(new System.Diagnostics.StackTrace(RawException, true));
            }

            exc_traceback = RawTraceBack;

            if (pyExcep is StringException)
            {
                // string exceptions are special...  there tuple looks
                // like string, argument, traceback instead of
                //      type,   instance, traceback
                StringException se = RawException as StringException;
                Debug.Assert(se != null);

                exc_type  = pyExcep;
                exc_value = se.Value;

                return(Ops.MakeTuple(
                           pyExcep,
                           se.Value,
                           RawTraceBack));
            }
            else
            {
                object excType = Ops.GetAttr(DefaultContext.Default, pyExcep, SymbolTable.Class);
                exc_type  = excType;
                exc_value = pyExcep;

                return(Ops.MakeTuple(
                           excType,
                           pyExcep,
                           RawTraceBack));
            }
        }
Esempio n. 6
0
        internal void SetAttr(ICallerContext context, SymbolId name, object value)
        {
            Initialize(context);

            if (HaveInterfaces)
            {
                foreach (DynamicType type in interfaces)
                {
                    try {
                        type.SetAttr(context, obj, name, value);
                        return;
                    } catch {
                    }
                }
                throw Ops.AttributeErrorForMissingAttribute(ComType.MakeDynamicType().__name__.ToString(), name);
            }
            else
            {
                try {
                    Obj.GetType().InvokeMember(
                        (string)SymbolTable.IdToString(name),
                        System.Reflection.BindingFlags.SetProperty |
                        System.Reflection.BindingFlags.SetField,
                        Type.DefaultBinder,
                        Obj,
                        new object[1] {
                        value
                    }
                        );
                } catch (Exception e) {
                    if (e.InnerException != null)
                    {
                        throw ExceptionConverter.UpdateForRethrow(e.InnerException);
                    }
                    throw;
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Performs late-bound invocation - used by ReflectedMethod's and
        /// for keyword argument calls.
        /// </summary>
        protected static object Invoke(MethodBinding binding)
        {
            object result;

            try {
                if (binding.method is ConstructorInfo)
                {
                    result = ((ConstructorInfo)binding.method).Invoke(binding.arguments);
                }
                else
                {
                    result = binding.method.Invoke(binding.instance, binding.arguments);
                }
            } catch (TargetInvocationException tie) {
                throw ExceptionConverter.UpdateForRethrow(tie.InnerException);
            }

            MethodBase info = binding.method;

            ParameterInfo[] parameters = info.GetParameters();
            int             results    = 0;

            for (int parm = 0; parm < parameters.Length; parm++)
            {
                if (parameters[parm].ParameterType.IsByRef)
                {
                    results++;
                }
            }
            if (results == 0)
            {
                return(Ops.ToPython(CompilerHelpers.GetReturnType(info), result));
            }

            object[] retValues;
            int      retValueIndex = 0;

            if (info is MethodInfo && ((MethodInfo)info).ReturnType != typeof(void))
            {
                retValues = new object[results + 1];
                retValues[retValueIndex++] = Ops.ToPython(CompilerHelpers.GetReturnType(info), result);
            }
            else
            {
                retValues = new object[results];
            }

            for (int parm = 0; parm < parameters.Length; parm++)
            {
                Type parmType = parameters[parm].ParameterType;
                if (parmType.IsByRef)
                {
                    retValues[retValueIndex++] = Ops.ToPython(parmType, binding.arguments[parm]);
                }
            }
            if (retValues.Length == 1)
            {
                return(retValues[0]);
            }
            else
            {
                return(Tuple.MakeTuple(retValues));
            }
        }
        /// <summary>
        /// Creates a CLR exception for the given type
        /// </summary>
        public static Exception CreateThrowable(object type)
        {
            object pyEx = Ops.Call(type);

            return(ExceptionConverter.ToClr(pyEx));
        }
Esempio n. 9
0
 public void exit(object code)
 {
     // throw as a python exception here to get the args set.
     throw ExceptionConverter.CreateThrowable(ExceptionConverter.GetPythonExceptionByName("SystemExit"), code);
 }
Esempio n. 10
0
 public void exit()
 {
     throw ExceptionConverter.CreateThrowable(ExceptionConverter.GetPythonExceptionByName("SystemExit"));
 }