ToString() public method

ToString Method
Return the string representation of the object. This method is the managed equivalent of the Python expression "str(object)".
public ToString ( ) : string
return string
Beispiel #1
0
    public PythonException() : base()
    {
        IntPtr gs = PythonEngine.AcquireLock();
        Runtime.PyErr_Fetch(ref _pyType, ref _pyValue, ref _pyTB);
        Runtime.Incref(_pyType);
        Runtime.Incref(_pyValue);
        Runtime.Incref(_pyTB);
        if ((_pyType != IntPtr.Zero) && (_pyValue != IntPtr.Zero))
        {
            string type;
            string message;
            Runtime.Incref(_pyType);
            using (PyObject pyType = new PyObject(_pyType))
            using (PyObject pyTypeName = pyType.GetAttr("__name__"))
            {
                    type = pyTypeName.ToString();
            }

            Runtime.Incref(_pyValue);
            using (PyObject pyValue = new PyObject(_pyValue)) 
            {
                message = pyValue.ToString(); 
            }
            _message = type + " : " + message;
        }
        if (_pyTB != IntPtr.Zero)
        {
            PyObject tb_module = PythonEngine.ImportModule("traceback");
            Runtime.Incref(_pyTB);
            using (PyObject pyTB = new PyObject(_pyTB)) {
                _tb = tb_module.InvokeMethod("format_tb", pyTB).ToString();
            }
        }
        PythonEngine.ReleaseLock(gs);
    }
Beispiel #2
0
        public PythonException()
        {
            IntPtr gs = PythonEngine.AcquireLock();

            Runtime.PyErr_Fetch(out _pyType, out _pyValue, out _pyTB);
            if (_pyType != IntPtr.Zero && _pyValue != IntPtr.Zero)
            {
                string type;
                string message;
                Runtime.XIncref(_pyType);
                using (var pyType = new PyObject(_pyType))
                    using (PyObject pyTypeName = pyType.GetAttr("__name__"))
                    {
                        type = pyTypeName.ToString();
                    }

                _pythonTypeName = type;

                // TODO: If pyValue has a __cause__ attribute, then we could set this.InnerException to the equivalent managed exception.
                Runtime.XIncref(_pyValue);
                using (var pyValue = new PyObject(_pyValue))
                {
                    message = pyValue.ToString();
                }
                _message = type + " : " + message;
            }

            if (_pyTB != IntPtr.Zero)
            {
                using var tb_module = PyModule.Import("traceback");

                Runtime.XIncref(_pyTB);
                using var pyTB = new PyObject(_pyTB);

                using var tbList = tb_module.InvokeMethod("format_tb", pyTB);

                var sb = new StringBuilder();
                // Reverse Python's traceback list to match the order used in C#
                // stacktraces
                foreach (var line in tbList.Reverse())
                {
                    sb.Append(line.ToString());
                }
                _tb = sb.ToString();
            }
            PythonEngine.ReleaseLock(gs);
        }
Beispiel #3
0
        public PythonException()
        {
            IntPtr gs = PythonEngine.AcquireLock();

            Runtime.Interop.PyErr_Fetch(ref _pyType, ref _pyValue, ref _pyTB);
            Runtime.XIncref(_pyType);
            Runtime.XIncref(_pyValue);
            Runtime.XIncref(_pyTB);
            if (_pyType != IntPtr.Zero && _pyValue != IntPtr.Zero)
            {
                string type;
                string message;
                Runtime.XIncref(_pyType);
                using (var pyType = new PyObject(_pyType))
                    using (PyObject pyTypeName = pyType.GetAttr("__name__"))
                    {
                        type = pyTypeName.ToString();
                    }

                _pythonTypeName = type;

                Runtime.XIncref(_pyValue);
                using (var pyValue = new PyObject(_pyValue))
                {
                    message = pyValue.ToString();
                }
                _message = type + " : " + message;
            }
            if (_pyTB != IntPtr.Zero)
            {
                PyObject tb_module = PythonEngine.ImportModule("traceback");
                Runtime.XIncref(_pyTB);
                using (var pyTB = new PyObject(_pyTB))
                {
                    _tb = tb_module.InvokeMethod("format_tb", pyTB).ToString();
                }
            }
            PythonEngine.ReleaseLock(gs);
        }
        public object ToClr(PyObject obj, Type t = null)
        {
            if (obj == null)
            {
                return(null);
            }
            PyObject type = obj.GetPythonType();
            Dictionary <Type, PyClrTypeBase> converters;
            var state = PythonConverters.TryGetValue(type.Handle, out converters);

            if (!state)
            {
                throw new Exception($"Type {type.ToString()} not recognized");
            }
            if (t == null || !converters.ContainsKey(t))
            {
                return(converters.Values.First().ToClr(obj));
            }
            else
            {
                return(converters[t].ToClr(obj));
            }
        }
Beispiel #5
0
        /// <summary>
        /// Python method may have the following function attributes set to control how they're exposed:
        /// - _clr_return_type_    - method return type (required)
        /// - _clr_arg_types_      - list of method argument types (required)
        /// - _clr_method_name_    - method name, if different from the python method name (optional)
        /// </summary>
        /// <param name="methodName">Method name to add to the type</param>
        /// <param name="func">Python callable object</param>
        /// <param name="typeBuilder">TypeBuilder for the new type the method/property is to be added to</param>
        private static void AddPythonMethod(string methodName, PyObject func, TypeBuilder typeBuilder)
        {
            if (func.HasAttr("_clr_method_name_"))
            {
                using (PyObject pyMethodName = func.GetAttr("_clr_method_name_"))
                {
                    methodName = pyMethodName.ToString();
                }
            }

            using (PyObject pyReturnType = func.GetAttr("_clr_return_type_"))
                using (PyObject pyArgTypes = func.GetAttr("_clr_arg_types_"))
                {
                    var returnType = pyReturnType.AsManagedObject(typeof(Type)) as Type;
                    if (returnType == null)
                    {
                        returnType = typeof(void);
                    }

                    if (!pyArgTypes.IsIterable())
                    {
                        throw new ArgumentException("_clr_arg_types_ must be a list or tuple of CLR types");
                    }

                    var argTypes = new List <Type>();
                    foreach (PyObject pyArgType in pyArgTypes)
                    {
                        var argType = pyArgType.AsManagedObject(typeof(Type)) as Type;
                        if (argType == null)
                        {
                            throw new ArgumentException("_clr_arg_types_ must be a list or tuple of CLR types");
                        }
                        argTypes.Add(argType);
                    }

                    // add the method to call back into python
                    MethodAttributes methodAttribs = MethodAttributes.Public |
                                                     MethodAttributes.Virtual |
                                                     MethodAttributes.ReuseSlot |
                                                     MethodAttributes.HideBySig;

                    MethodBuilder methodBuilder = typeBuilder.DefineMethod(methodName,
                                                                           methodAttribs,
                                                                           returnType,
                                                                           argTypes.ToArray());

                    ILGenerator il = methodBuilder.GetILGenerator();
                    il.DeclareLocal(typeof(object[]));
                    il.Emit(OpCodes.Ldarg_0);
                    il.Emit(OpCodes.Ldstr, methodName);
                    il.Emit(OpCodes.Ldnull); // don't fall back to the base type's method
                    il.Emit(OpCodes.Ldc_I4, argTypes.Count);
                    il.Emit(OpCodes.Newarr, typeof(object));
                    il.Emit(OpCodes.Stloc_0);
                    for (var i = 0; i < argTypes.Count; ++i)
                    {
                        il.Emit(OpCodes.Ldloc_0);
                        il.Emit(OpCodes.Ldc_I4, i);
                        il.Emit(OpCodes.Ldarg, i + 1);
                        if (argTypes[i].IsValueType)
                        {
                            il.Emit(OpCodes.Box, argTypes[i]);
                        }
                        il.Emit(OpCodes.Stelem, typeof(object));
                    }
                    il.Emit(OpCodes.Ldloc_0);
                    if (returnType == typeof(void))
                    {
                        il.Emit(OpCodes.Call, typeof(PythonDerivedType).GetMethod("InvokeMethodVoid"));
                    }
                    else
                    {
                        il.Emit(OpCodes.Call,
                                typeof(PythonDerivedType).GetMethod("InvokeMethod").MakeGenericMethod(returnType));
                    }
                    il.Emit(OpCodes.Ret);
                }
        }