Exemple #1
0
 public static string ToStringReturnHelper(object o)
 {
     if (o is string && o != null)
     {
         return((string)o);
     }
     throw PythonOps.TypeError("__str__ returned non-string type ({0})", PythonOps.GetPythonTypeName(o));
 }
Exemple #2
0
 public static void SetPropertyHelper(object prop, object instance, object newValue, string name)
 {
     if (!(prop is PythonTypeSlot desc))
     {
         throw PythonOps.TypeError("Expected settable property for {0}, but found {1}",
                                   name.ToString(), PythonOps.GetPythonTypeName(prop));
     }
     desc.TrySetValue(DefaultContext.Default, instance, DynamicHelpers.GetPythonType(instance), newValue);
 }
Exemple #3
0
        public static object GetPropertyHelper(object prop, object instance, string name)
        {
            if (!(prop is PythonTypeSlot desc))
            {
                throw PythonOps.TypeError("Expected property for {0}, but found {1}",
                                          name.ToString(), PythonOps.GetPythonTypeName(prop));
            }
            object value;

            desc.TryGetValue(DefaultContext.Default, instance, DynamicHelpers.GetPythonType(instance), out value);
            return(value);
        }
Exemple #4
0
 internal static IList <byte> CoerceBytes(object?obj)
 {
     if (obj is IList <byte> ret)
     {
         return(ret);
     }
     if (obj is IBufferProtocol bp)
     {
         using (IPythonBuffer buf = bp.GetBuffer()) {
             return(buf.AsReadOnlySpan().ToArray());
         }
     }
     throw PythonOps.TypeError("a bytes-like object is required, not '{0}'", PythonOps.GetPythonTypeName(obj));
 }
Exemple #5
0
        public static void AddRemoveEventHelper(object method, IPythonObject instance, object eventValue, string name)
        {
            object callable = method;

            // TODO: dt gives us a PythonContext which we should use
            PythonType dt = instance.PythonType;

            if (method is PythonTypeSlot dts)
            {
                if (!dts.TryGetValue(DefaultContext.Default, instance, dt, out callable))
                {
                    throw PythonOps.AttributeErrorForMissingAttribute(dt.Name, name);
                }
            }

            if (!PythonOps.IsCallable(DefaultContext.Default, callable))
            {
                throw PythonOps.TypeError("Expected callable value for {0}, but found {1}", name.ToString(),
                                          PythonOps.GetPythonTypeName(method));
            }

            PythonCalls.Call(callable, eventValue);
        }
Exemple #6
0
        internal static void AppendJoin(object value, int index, List <byte> byteList)
        {
            IList <byte> bytesValue;
            string       strValue;

            if ((bytesValue = value as IList <byte>) != null)
            {
                byteList.AddRange(bytesValue);
            }
            else if ((strValue = value as string) != null)
            {
                byteList.AddRange(strValue.MakeByteArray());
            }
            else
            {
                throw PythonOps.TypeError("sequence item {0}: expected bytes or byte array, {1} found", index.ToString(), PythonOps.GetPythonTypeName(value));
            }
        }
Exemple #7
0
 internal static void AppendJoin(object?value, int index, List <byte> byteList)
 {
     if (value is IList <byte> bytesValue)
     {
         byteList.AddRange(bytesValue);
     }
     else if (value is IBufferProtocol bp)
     {
         using (IPythonBuffer buf = bp.GetBuffer()) {
             byteList.AddRange(buf.AsReadOnlySpan().ToArray());
         }
     }
     else
     {
         throw PythonOps.TypeError("sequence item {0}: expected bytes or byte array, {1} found", index.ToString(), PythonOps.GetPythonTypeName(value));
     }
 }
        private static object FastNew(CodeContext /*!*/ context, object?o, int @base = 10)
        {
            object?result;

            switch (o)
            {
            case int _:
                return(o);

            case BigInteger val:
                return(val.IsInt32() ? (int)val : o);

            case double d:
                return(DoubleOps.__int__(d));

            case bool b:
                return(BoolOps.__int__(b));

            case Extensible <BigInteger> ebi:
                return(TryInvokeInt(context, o, out result) ? result : ebi.Value.IsInt32() ? (object)(int)ebi.Value : ebi.Value);

            case float f:
                return(DoubleOps.__int__(f));

            case sbyte val:
                return((int)val);

            case byte val:
                return((int)val);

            case short val:
                return((int)val);

            case ushort val:
                return((int)val);

            case long val:
                return(int.MinValue <= val && val <= int.MaxValue ? (object)(int)val : (BigInteger)val);

            case uint val:
                return(val <= int.MaxValue ? (object)(int)val : (BigInteger)val);

            case ulong val:
                return(val <= int.MaxValue ? (object)(int)val : (BigInteger)val);

            case decimal val:
                return(int.MinValue <= val && val <= int.MaxValue ? (object)(int)val : (BigInteger)val);

            case Enum e:
                var ic = (IConvertible)e;
                switch (ic.GetTypeCode())
                {
                case TypeCode.Byte:
                case TypeCode.SByte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                    return(ic.ToInt32(null));

                case TypeCode.Int64:
                    return((BigInteger)ic.ToInt64(null));

                case TypeCode.UInt32:
                case TypeCode.UInt64:
                    return((BigInteger)ic.ToUInt64(null));

                default:
                    throw new InvalidOperationException();         // unreachable
                }

            case string s:
                return(LiteralParser.ParseIntegerSign(s, @base, FindStart(s, @base)));

            case Extensible <string> es:
                return(TryInvokeInt(context, o, out result) ? result : LiteralParser.ParseIntegerSign(es.Value, @base, FindStart(es.Value, @base)));

            default:
                break;
            }

            if (TryInvokeInt(context, o, out result))
            {
                return(result);
            }
            else if (PythonTypeOps.TryInvokeUnaryOperator(context, o, "__trunc__", out result))
            {
                switch (result)
                {
                case int _:
                    return(result);

                case BigInteger bi:
                    return(bi.IsInt32() ? (object)(int)bi : result);

                case bool b:
                    return(BoolOps.__int__(b));    // Python 3.6: return the int value

                case Extensible <BigInteger> ebi:
                    return(ebi.Value.IsInt32() ? (object)(int)ebi.Value : ebi.Value);    // Python 3.6: return the int value

                default: {
                    if (TryInvokeInt(context, result, out var intResult))
                    {
                        return(intResult);
                    }
                    throw PythonOps.TypeError("__trunc__ returned non-Integral (type {0})", PythonOps.GetPythonTypeName(result));
                }
                }
            }

            throw PythonOps.TypeError("int() argument must be a string, a bytes-like object or a number, not '{0}'", PythonOps.GetPythonTypeName(o));
 static void Warn(CodeContext context, object result)
 {
     PythonOps.Warn(context, PythonExceptions.DeprecationWarning, $"__int__ returned non-int (type {PythonOps.GetPythonTypeName(result)}).  The ability to return an instance of a strict subclass of int is deprecated, and may be removed in a future version of Python.");
 }
            static bool TryInvokeInt(CodeContext context, object?o, [NotNullWhen(true)] out object?result)
            {
                if (PythonTypeOps.TryInvokeUnaryOperator(context, o, "__int__", out result))
                {
                    switch (result)
                    {
                    case int _:
                        return(true);

                    case BigInteger bi:
                        if (bi.IsInt32())
                        {
                            result = (int)bi;
                        }
                        return(true);

                    case bool b:
                        Warn(context, result);
                        result = BoolOps.__int__(b);     // Python 3.6: return the int value
                        return(true);

                    case Extensible <BigInteger> ebi:
                        Warn(context, result);
                        result = ebi.Value.IsInt32() ? (object)(int)ebi.Value : ebi.Value;     // Python 3.6: return the int value
                        return(true);

                    default:
                        throw PythonOps.TypeError("__int__ returned non-int (type {0})", PythonOps.GetPythonTypeName(result));
                    }
Exemple #11
0
        public static string __format__(CodeContext /*!*/ context, object self, [NotNone] string /*!*/ formatSpec)
        {
            if (formatSpec != string.Empty)
            {
                throw PythonOps.TypeError("unsupported format string passed to {0}.__format__", PythonOps.GetPythonTypeName(self));
            }

            return(PythonOps.ToString(context, self));
        }
Exemple #12
0
 public static string __repr__(object self)
 {
     return($"<{PythonOps.GetPythonTypeName(self)} object at {PythonOps.HexId(self)}>");
 }
 internal static void AppendJoin(object value, int index, List <byte> byteList)
 {
     if (value is IList <byte> bytesValue)
     {
         byteList.AddRange(bytesValue);
     }
     else
     {
         throw PythonOps.TypeError("sequence item {0}: expected bytes or byte array, {1} found", index.ToString(), PythonOps.GetPythonTypeName(value));
     }
 }
Exemple #14
0
            static bool TryInvokeFloat(CodeContext context, object /*?*/ o, out double result)
            {
                if (PythonTypeOps.TryInvokeUnaryOperator(context, o, "__float__", out object retobj))
                {
                    switch (retobj)
                    {
                    case double d:
                        result = d;
                        return(true);

                    case Extensible <double> ed:
                        Warn(context, retobj);
                        result = ed.Value;     // Python 3.6: return the int value
                        return(true);

                    default:
                        throw PythonOps.TypeError("__float__ returned non-float (type {0})", PythonOps.GetPythonTypeName(retobj));
                    }
Exemple #15
0
 public static string SimpleRepr(object self)
 {
     return(String.Format("<{0} object at {1}>",
                          PythonOps.GetPythonTypeName(self),
                          PythonOps.HexId(self)));
 }