Beispiel #1
0
        public static object __new__(CodeContext/*!*/ context, PythonType cls, object x) {
            if (cls == TypeCache.Double) {
                if (x is string) {
                    return ParseFloat((string)x);
                } else if (x is Extensible<string>) {
                    object res;
                    if (PythonTypeOps.TryInvokeUnaryOperator(context, x, "__float__", out res)) {
                        return res;
                    }
                    return ParseFloat(((Extensible<string>)x).Value);
                } else if (x is char) {
                    return ParseFloat(ScriptingRuntimeHelpers.CharToString((char)x));
                }

                if (x is Complex) {
                    throw PythonOps.TypeError("can't convert complex to float; use abs(z)");
                }

                object d = PythonOps.CallWithContext(context, PythonOps.GetBoundAttr(context, x, "__float__"));
                if (d is double) {
                    return d;
                } else if (d is Extensible<double>) {
                    return ((Extensible<double>)d).Value;
                }

                throw PythonOps.TypeError("__float__ returned non-float (type {0})", PythonTypeOps.GetName(d));
            } else {
                return cls.CreateInstance(context, x);
            }
        }
Beispiel #2
0
        public static object CreateNativeWrapper(PythonType type, object holder) {
            Debug.Assert(holder is MemoryHolder);

            CTypes.CData data = (CTypes.CData)type.CreateInstance(type.Context.SharedContext);
            data._memHolder = (MemoryHolder)holder;
            return data;
        }
Beispiel #3
0
 public static object CreateCData(IntPtr dataAddress, PythonType type) {
     CTypes.INativeType nativeType = (CTypes.INativeType)type;
     CTypes.CData data = (CTypes.CData)type.CreateInstance(type.Context.SharedContext);
     data._memHolder = new MemoryHolder(nativeType.Size);
     data._memHolder.CopyFrom(dataAddress, new IntPtr(nativeType.Size));
     return data;
 }
Beispiel #4
0
        public static object __new__(CodeContext/*!*/ context, PythonType cls, object x) {
            if (cls == TypeCache.Double) {
                if (x is string) {
                    return ParseFloat((string)x);
                } else if (x is Extensible<string>) {
                    object res;
                    if (PythonTypeOps.TryInvokeUnaryOperator(context, x, Symbols.ConvertToFloat, out res)) {
                        return res;
                    }
                    return ParseFloat(((Extensible<string>)x).Value);
                } else if (x is char) {
                    return ParseFloat(ScriptingRuntimeHelpers.CharToString((char)x));
                }

                double doubleVal;
                if (Converter.TryConvertToDouble(x, out doubleVal)) return doubleVal;

                if (x is Complex64) throw PythonOps.TypeError("can't convert complex to float; use abs(z)");

                object d = PythonOps.CallWithContext(context, PythonOps.GetBoundAttr(context, x, Symbols.ConvertToFloat));
                if (d is double) return d;
                throw PythonOps.TypeError("__float__ returned non-float (type %s)", DynamicHelpers.GetPythonType(d));
            } else {
                return cls.CreateInstance(context, x);
            }
        }
Beispiel #5
0
        public static object __new__(CodeContext context, PythonType type, object o) {
            object reversed;
            if (PythonOps.TryGetBoundAttr(context, o, "__reversed__", out reversed)) {
                return PythonCalls.Call(context, reversed);
            }

            object boundFunc;

            PythonTypeSlot getitem;
            PythonType pt = DynamicHelpers.GetPythonType(o);
            if(!pt.TryResolveSlot(context, "__getitem__", out getitem) ||
                !getitem.TryGetValue(context, o, pt, out boundFunc)
                || o is PythonDictionary) {
                throw PythonOps.TypeError("argument to reversed() must be a sequence");
            }

            int length;
            if (!DynamicHelpers.GetPythonType(o).TryGetLength(context, o, out length)) {
                throw PythonOps.TypeError("object of type '{0}' has no len()", DynamicHelpers.GetPythonType(o).Name);
            }

            if (type.UnderlyingSystemType == typeof(ReversedEnumerator)) {
                return new ReversedEnumerator((int)length, boundFunc);
            }

            return type.CreateInstance(context, length, getitem);
        }
Beispiel #6
0
        public static PythonModule/*!*/ __new__(CodeContext/*!*/ context, PythonType/*!*/ cls, params object[]/*!*/ args\u00F8) {
            PythonModule res;
            if (cls == TypeCache.Module) {
                res = new PythonModule();
            } else if (cls.IsSubclassOf(TypeCache.Module)) {
                res = (PythonModule)cls.CreateInstance(context);
            } else {
                throw PythonOps.TypeError("{0} is not a subtype of module", cls.Name);
            }

            return res;
        }
Beispiel #7
0
        public static object __new__(CodeContext context, PythonType cls, string s, int radix) {
            if (radix == 16 || radix == 8 || radix == 2) {
                s = Int32Ops.TrimRadix(s, radix);
            }

            if (cls == TypeCache.BigInteger) {
                return ParseBigIntegerSign(s, radix);
            } else {
                BigInteger res = ParseBigIntegerSign(s, radix);
                return cls.CreateInstance(context, res);
            }
        }
Beispiel #8
0
        public static Scope/*!*/ __new__(CodeContext/*!*/ context, PythonType/*!*/ cls, params object[]/*!*/ args\u00F8) {
            Scope res;
            if (cls == TypeCache.Module) {
                res = new Scope(PythonDictionary.MakeSymbolDictionary());
            } else if (cls.IsSubclassOf(TypeCache.Module)) {
                res = (Scope)cls.CreateInstance(context);
            } else {
                throw PythonOps.TypeError("{0} is not a subtype of module", cls.Name);
            }

            PythonContext.GetContext(context).CreateModule(null, res, null, ModuleOptions.None);
            res.Clear();
            return res;
        }
Beispiel #9
0
        public static object __new__(CodeContext/*!*/ context, PythonType cls, IList<byte> s) {
            if (cls == TypeCache.BigInteger) {
                object value;
                IPythonObject po = s as IPythonObject;
                if (po != null &&
                    PythonTypeOps.TryInvokeUnaryOperator(DefaultContext.Default, po, "__long__", out value)) {
                    return value;
                }

                return ParseBigIntegerSign(s.MakeString(), 10);
            }

            return cls.CreateInstance(context, ParseBigIntegerSign(s.MakeString(), 10));
        }
Beispiel #10
0
        public static object __new__(CodeContext/*!*/ context, PythonType cls, IList<byte> s) {
            if (cls == TypeCache.Double) {
                object value;
                IPythonObject po = s as IPythonObject;
                if (po != null &&
                    PythonTypeOps.TryInvokeUnaryOperator(DefaultContext.Default, po, Symbols.ConvertToFloat, out value)) {
                    return value;
                }

                return ParseFloat(s.MakeString());
            }

            return cls.CreateInstance(context, s);
        }
Beispiel #11
0
        public static object __new__(CodeContext/*!*/ context, PythonType cls, IList<byte> s) {
            if (cls == TypeCache.BigInteger) {
                object value;
                IPythonObject po = s as IPythonObject;
                if (po != null &&
                    PythonTypeOps.TryInvokeUnaryOperator(DefaultContext.Default, po, Symbols.ConvertToLong, out value)) {
                    return value;
                }

                return ParseBigIntegerSign(StringOps.FromByteArray(s), 10);
            }

            return cls.CreateInstance(context, ParseBigIntegerSign(StringOps.FromByteArray(s), 10));
        }
Beispiel #12
0
            public static object __new__(CodeContext context, PythonType cls, object @object) {
                IWeakReferenceable iwr = ConvertToWeakReferenceable(@object);

                if (cls == DynamicHelpers.GetPythonTypeFromType(typeof(@ref))) {
                    WeakRefTracker wrt = iwr.GetWeakRef();
                    if (wrt != null) {
                        for (int i = 0; i < wrt.HandlerCount; i++) {
                            if (wrt.GetHandlerCallback(i) == null && wrt.GetWeakRef(i) is @ref) {
                                return wrt.GetWeakRef(i);
                            }
                        }
                    }

                    return new @ref(@object);
                } else {
                    return cls.CreateInstance(context, @object);
                }
            }
Beispiel #13
0
 public static object __new__(CodeContext context, PythonType cls) {
     if (cls == TypeCache.BigInteger) {
         return BigInteger.Zero;
     } else {
         return cls.CreateInstance(context, BigInteger.Zero);
     }
 }
Beispiel #14
0
        public static object __new__(CodeContext/*!*/ context, PythonType cls, params object[] args\u00F8) {
            if (cls == null) {
                throw PythonOps.TypeError("__new__ expected type object, got {0}", PythonOps.Repr(context, DynamicHelpers.GetPythonType(cls)));
            }

            InstanceOps.CheckInitArgs(context, null, args\u00F8, cls);

            return cls.CreateInstance(context);
        }
Beispiel #15
0
 private static object ReturnObject(CodeContext context, PythonType cls, object value) {
     if (cls == TypeCache.BigInteger) {
         return value;
     } else {
         return cls.CreateInstance(context, value);
     }
 }
Beispiel #16
0
        public static object __new__(CodeContext/*!*/ context, PythonType cls) {
            if (cls == null) {
                throw PythonOps.TypeError("__new__ expected type object, got {0}", PythonOps.Repr(context, DynamicHelpers.GetPythonType(cls)));
            }

            return cls.CreateInstance(context);
        }
Beispiel #17
0
        public static object __new__(CodeContext/*!*/ context, PythonType cls, [ParamDictionary]IDictionary<object, object> kwargs\u00F8, [NotNull]params object[] args\u00F8) {
            if (cls == null) {
                throw PythonOps.TypeError("__new__ expected type object, got {0}", PythonOps.Repr(context, DynamicHelpers.GetPythonType(cls)));
            }

            InstanceOps.CheckNewArgs(context, kwargs\u00F8, args\u00F8, cls);

            return cls.CreateInstance(context);
        }
Beispiel #18
0
        public static object __new__(CodeContext/*!*/ context, PythonType cls) {
            if (cls == TypeCache.Double) return 0.0;

            return cls.CreateInstance(context);
        }
Beispiel #19
0
 public static struct_time __new__(CodeContext context, PythonType cls, int year, int month, int day, int hour, int minute, int second, int dayOfWeek, int dayOfYear, int isDst) {
     if (cls == _StructTimeType) {
         return new struct_time(year, month, day, hour, minute, second, dayOfWeek, dayOfYear, isDst);
     } else {
         struct_time st = cls.CreateInstance(context, year, month, day, hour, minute, second, dayOfWeek, dayOfYear, isDst) as struct_time;
         if (st == null)
             throw PythonOps.TypeError("{0} is not a subclass of time.struct_time", cls);
         return st;
     }
 }
Beispiel #20
0
        public static object __new__(CodeContext/*!*/ context, PythonType cls, object x) {
            if (cls != TypeCache.Single) {
                return cls.CreateInstance(context, x);
            }

            if (x is string) {
                return ParseFloat((string)x);
            } else if (x is Extensible<string>) {
                return ParseFloat(((Extensible<string>)x).Value);
            } else if (x is char) {
                return ParseFloat(ScriptingRuntimeHelpers.CharToString((char)x));
            }

            double doubleVal;
            if (Converter.TryConvertToDouble(x, out doubleVal)) return (float)doubleVal;

            if (x is Complex) throw PythonOps.TypeError("can't convert complex to Single; use abs(z)");

            object d = PythonOps.CallWithContext(context, PythonOps.GetBoundAttr(context, x, "__float__"));
            if (d is double) return (float)(double)d;
            throw PythonOps.TypeError("__float__ returned non-float (type %s)", DynamicHelpers.GetPythonType(d));
        }
Beispiel #21
0
        public static object __new__(CodeContext/*!*/ context, PythonType cls, IList<byte> s) {
            if (cls != TypeCache.Single) {
                return cls.CreateInstance(context, s);
            }

            object value;
            IPythonObject po = s as IPythonObject;
            if (po != null &&
                PythonTypeOps.TryInvokeUnaryOperator(DefaultContext.Default, po, "__float__", out value)) {
                if (value is double) return (float)(double)value;
                return value;
            }

            return ParseFloat(s.MakeString());
        }
Beispiel #22
0
 public static struct_time __new__(CodeContext context, PythonType cls, [NotNull]PythonTuple sequence) {
     if (sequence.__len__() != 9) {
         throw PythonOps.TypeError("time.struct_time() takes a 9-sequence ({0}-sequence given)", sequence.__len__());
     }
     if (cls == _StructTimeType) {
         return new struct_time(sequence);
     } else {
         struct_time st = cls.CreateInstance(context, sequence) as struct_time;
         if (st == null) {
             throw PythonOps.TypeError("{0} is not a subclass of time.struct_time", cls);
         }
         return st;
     }
 }
Beispiel #23
0
 public static object CreateCFunction(IntPtr address, PythonType type) {
     return type.CreateInstance(type.Context.SharedContext, address);
 }
Beispiel #24
0
 public static object __new__(CodeContext context, PythonType cls, Extensible<double> o) {
     object value;
     // always succeeds as float defines __int__
     PythonTypeOps.TryInvokeUnaryOperator(context, o, "__int__", out value);
     if (cls == TypeCache.Int32) {
         return (int)value;
     } else {
         return cls.CreateInstance(context, value);
     }
 }
Beispiel #25
0
        public static object __new__(CodeContext context, PythonType cls, object x) {
            object value = FastNew(context, x);
            if (cls == TypeCache.Int32) {
                return value;
            } else {
                ValidateType(cls);

                // derived int creation...
                return cls.CreateInstance(context, value);
            }
        }
Beispiel #26
0
        public static object __new__(CodeContext/*!*/ context, PythonType cls, IList<byte> s) {
            if (cls == TypeCache.Int32) {
                object value;
                IPythonObject po = s as IPythonObject;
                if (po != null &&
                    PythonTypeOps.TryInvokeUnaryOperator(DefaultContext.Default, po, "__int__", out value)) {
                    return value;
                }

                return FastNew(context, s.MakeString());
            }

            ValidateType(cls);

            // derived int creation...
            return cls.CreateInstance(context, FastNew(context, s.MakeString()));
        }
Beispiel #27
0
        public static object __new__(CodeContext context, PythonType cls) {
            if (cls == TypeCache.Int32) return 0;

            return cls.CreateInstance(context);
        }
Beispiel #28
0
        public static object __new__(CodeContext context, PythonType cls, object x) {
            if (cls == TypeCache.Int32) return FastNew(context, x); // TODO: Call site?

            ValidateType(cls);

            // derived int creation...
            return cls.CreateInstance(context, x);
        }
Beispiel #29
0
 public static timedelta __new__(CodeContext context, PythonType cls,
     [DefaultParameterValue(0D)] double days,
     [DefaultParameterValue(0D)] double seconds,
     [DefaultParameterValue(0D)] double microseconds,
     [DefaultParameterValue(0D)] double milliseconds,
     [DefaultParameterValue(0D)] double minutes,
     [DefaultParameterValue(0D)] double hours,
     [DefaultParameterValue(0D)] double weeks) {
     if (cls == DynamicHelpers.GetPythonTypeFromType(typeof(timedelta))) {
         return new timedelta(days, seconds, microseconds, milliseconds, minutes, hours, weeks);
     } else {
         timedelta delta = cls.CreateInstance(context, days, seconds, microseconds, milliseconds, minutes, hours, weeks) as timedelta;
         if (delta == null) throw PythonOps.TypeError("{0} is not a subclass of datetime.timedelta", cls);
         return delta;
     }
 }
Beispiel #30
0
 public static object __new__(CodeContext context, PythonType cls, object @object, object callback) {
     if (callback == null) return __new__(context, cls, @object);
     if (cls == DynamicHelpers.GetPythonTypeFromType(typeof(@ref))) {
         return new @ref(@object, callback);
     } else {
         return cls.CreateInstance(context, @object, callback);
     }
 }